当前位置: 技术问答>java相关
新手上路问题,java如何进行文件操作?
来源: 互联网 发布时间:2015-04-10
本文导语: 假设现有一个文件a.txt,现要读出它的所有内容。然后再在它的开始和结尾分别加入"begin"和"end".(请举个例) 请各位大虾帮帮忙!thanks. | import java.io.*; import java.util.zip.*; import java.util.*; public cla...
假设现有一个文件a.txt,现要读出它的所有内容。然后再在它的开始和结尾分别加入"begin"和"end".(请举个例)
请各位大虾帮帮忙!thanks.
请各位大虾帮帮忙!thanks.
|
import java.io.*;
import java.util.zip.*;
import java.util.*;
public class FileTool
{
public void appendToFile(String str, String filename) throws Exception
{
FileOutputStream stream;//provides file access
OutputStreamWriter writer;//writes to the file
try
{
stream = new FileOutputStream(filename, true);
writer = new OutputStreamWriter(stream);
writer.write(str);
writer.close();
stream.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//appendToFile
public void prependToFile(String str, String filename) throws Exception
{
// Read the file, then prepend str to the file text and write back
// to the file.
String newStr;//final string to be written
try
{
newStr = str + readFile(filename);
writeFile(newStr, filename);
}//try
catch(Exception e)
{
throw e;
}//catch
}//prependToFile
public String readFile(String filename) throws Exception
{
//Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
try
{
breader = new BufferedReader(new FileReader(filename));//header
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readFile
public List readFileToList(String filename) throws Exception
{
//Read the file into a List, then return.
BufferedReader breader;//reader for the template files
List list;//target vector
String line;//line from file
list = new ArrayList();
try
{
breader = new BufferedReader(new FileReader(filename));//header
while((line = breader.readLine()) != null)
list.add(line);
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return list;
}//readFileToVector
public void writeFile(String str, String filename) throws Exception
{
// Open a writer to the file, then write the string.
BufferedWriter bwriter;//writer to the file
String fullfilepath;//path for the output file
try
{
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
bwriter.write(str);
bwriter.flush();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//writeFile
public void copyFile(String sourcename, String targetname) throws Exception
{
// Open up a reader from sourcename and a writer to targetname.
// Write each character from sourcename to targetname, then close.
BufferedReader breader;//reader from source
BufferedWriter bwriter;//writer to target
try
{
breader = new BufferedReader(new FileReader(sourcename));
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetname)));
while(breader.ready())
bwriter.write(breader.read());
breader.close();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//copyFile
public String readZippedFile(String textFilename, String zipFilename) throws Exception
{
// Open a stream from the entry in the zip file for the file.
// Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
ZipFile zipFile;//zip file
ZipEntry zipEntry;//entry in zip
InputStream iStream;//input stream of file
try
{
zipFile = new ZipFile(zipFilename);
zipEntry = zipFile.getEntry(textFilename);
iStream = zipFile.getInputStream(zipEntry);
breader = new BufferedReader(new InputStreamReader(iStream));
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readZippedFile
public String readConsole() throws Exception
{
// Create a buffered reader with System.in, then
// read a line from it.
BufferedReader breader;
breader = new BufferedReader(new InputStreamReader(System.in));
return breader.readLine();
}//readConsole
public Properties loadProperties(String propsFile) throws Exception
{
// Open up an input stream from the file, then read it into
// a properties hashtable and return.
FileInputStream instream;//the input stream
Properties props;//props object
instream = null;
props = new Properties();
try
{
instream = new FileInputStream(propsFile);
props.load(instream);
}//try
finally
{
try
{
instream.close();
}//try
catch(Exception e1)
{
}
}//finally
return props;
}//loadProperties
//______________________________________________________________
}
import java.util.zip.*;
import java.util.*;
public class FileTool
{
public void appendToFile(String str, String filename) throws Exception
{
FileOutputStream stream;//provides file access
OutputStreamWriter writer;//writes to the file
try
{
stream = new FileOutputStream(filename, true);
writer = new OutputStreamWriter(stream);
writer.write(str);
writer.close();
stream.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//appendToFile
public void prependToFile(String str, String filename) throws Exception
{
// Read the file, then prepend str to the file text and write back
// to the file.
String newStr;//final string to be written
try
{
newStr = str + readFile(filename);
writeFile(newStr, filename);
}//try
catch(Exception e)
{
throw e;
}//catch
}//prependToFile
public String readFile(String filename) throws Exception
{
//Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
try
{
breader = new BufferedReader(new FileReader(filename));//header
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readFile
public List readFileToList(String filename) throws Exception
{
//Read the file into a List, then return.
BufferedReader breader;//reader for the template files
List list;//target vector
String line;//line from file
list = new ArrayList();
try
{
breader = new BufferedReader(new FileReader(filename));//header
while((line = breader.readLine()) != null)
list.add(line);
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return list;
}//readFileToVector
public void writeFile(String str, String filename) throws Exception
{
// Open a writer to the file, then write the string.
BufferedWriter bwriter;//writer to the file
String fullfilepath;//path for the output file
try
{
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
bwriter.write(str);
bwriter.flush();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//writeFile
public void copyFile(String sourcename, String targetname) throws Exception
{
// Open up a reader from sourcename and a writer to targetname.
// Write each character from sourcename to targetname, then close.
BufferedReader breader;//reader from source
BufferedWriter bwriter;//writer to target
try
{
breader = new BufferedReader(new FileReader(sourcename));
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetname)));
while(breader.ready())
bwriter.write(breader.read());
breader.close();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//copyFile
public String readZippedFile(String textFilename, String zipFilename) throws Exception
{
// Open a stream from the entry in the zip file for the file.
// Read the file into a string buffer, then return as a string.
StringBuffer buf;//the intermediary, mutable buffer
BufferedReader breader;//reader for the template files
ZipFile zipFile;//zip file
ZipEntry zipEntry;//entry in zip
InputStream iStream;//input stream of file
try
{
zipFile = new ZipFile(zipFilename);
zipEntry = zipFile.getEntry(textFilename);
iStream = zipFile.getInputStream(zipEntry);
breader = new BufferedReader(new InputStreamReader(iStream));
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
throw e;
}//catch
return buf.toString();
}//readZippedFile
public String readConsole() throws Exception
{
// Create a buffered reader with System.in, then
// read a line from it.
BufferedReader breader;
breader = new BufferedReader(new InputStreamReader(System.in));
return breader.readLine();
}//readConsole
public Properties loadProperties(String propsFile) throws Exception
{
// Open up an input stream from the file, then read it into
// a properties hashtable and return.
FileInputStream instream;//the input stream
Properties props;//props object
instream = null;
props = new Properties();
try
{
instream = new FileInputStream(propsFile);
props.load(instream);
}//try
finally
{
try
{
instream.close();
}//try
catch(Exception e1)
{
}
}//finally
return props;
}//loadProperties
//______________________________________________________________
}
|
import java.io.*;
import java.util.*;
import corejava.*;
public class DataFileTest
{ static void writeData(Employee[] e, PrintWriter out)
throws IOException
{ out.println(e.length);
int i;
for (i = 0; i