importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.FileWriter;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.Reader;public classFileUtil {/*** 按行读取文件*/
public static voidReadFileByLine(String filename) {
File file= newFile(filename);
InputStream is= null;
Reader reader= null;
BufferedReader bufferedReader= null;try{
is= newFileInputStream(file);
reader= newInputStreamReader(is);
bufferedReader= newBufferedReader(reader);
String line= null;while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{try{if (null !=bufferedReader)
bufferedReader.close();if (null !=reader)
reader.close();if (null !=is)
is.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}/*** 按字节读取文件
*
*@paramfilename*/
public static voidReadFileByBytes(String filename) {
File file= newFile(filename);
InputStream is= null;try{
is= newFileInputStream(file);int index = 0;while (-1 != (index =is.read())) {
System.out.write(index);
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{try{if (null !=is)
is.close();
}catch(IOException e) {
e.printStackTrace();
}
}
System.out.println("-----------------------------------");try{
is= newFileInputStream(file);byte[] tempbyte = new byte[1000];int index = 0;while (-1 != (index =is.read(tempbyte))) {
System.out.write(tempbyte,0, index);
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{try{if (null !=is)
is.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}/*** 按字符读取文件
*
*@paramfilename*/
public static voidReadFileByChar(String filename) {
File file= newFile(filename);
InputStream is= null;
Reader isr= null;try{
is= newFileInputStream(file);
isr= newInputStreamReader(is);int index = 0;while (-1 != (index =isr.read())) {
System.out.print((char) index);
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{try{if (null !=is)
is.close();if (null !=isr)
isr.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}/*** 通过OutputStreamWriter写文件
*
*@paramfilename*/
public static voidWrite2FileByOutputStream(String filename) {
File file= newFile(filename);
FileOutputStream fos= null;//BufferedOutputStream bos = null;
OutputStreamWriter osw = null;try{if (!file.exists()) {
file.createNewFile();
}
fos= newFileOutputStream(file);
osw= newOutputStreamWriter(fos);
osw.write("Write2FileByOutputStream");//bos = new BufferedOutputStream(fos);//bos.write("Write2FileByOutputStream".getBytes());
} catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{if (null !=osw) {try{
osw.close();
}catch(IOException e) {
e.printStackTrace();
}
}if (null !=fos) {try{
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 通过BufferedWriter写文件
*
*@paramfilename*/
public static voidWrite2FileByBuffered(String filename) {
File file= newFile(filename);
FileOutputStream fos= null;
OutputStreamWriter osw= null;
BufferedWriter bw= null;try{if (!file.exists()) {
file.createNewFile();
}
fos= newFileOutputStream(file);
osw= newOutputStreamWriter(fos);
bw= newBufferedWriter(osw);
bw.write("Write2FileByBuffered");
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{if (null !=bw) {try{
bw.close();
}catch(IOException e) {
e.printStackTrace();
}
}if (null !=osw) {try{
osw.close();
}catch(IOException e) {
e.printStackTrace();
}
}if (null !=fos) {try{
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 通过FileWriter写文件
*
*@paramfilename*/
public static voidWrite2FileByFileWriter(String filename) {
File file= newFile(filename);
FileWriter fw= null;try{if (!file.exists()) {
file.createNewFile();
}
fw= newFileWriter(file);
fw.write("Write2FileByFileWriter");
}catch(IOException e) {
e.printStackTrace();
}finally{if (null !=fw) {try{
fw.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}public static voidmain(String[] args) {
String filename= "D:/testfile.txt";//ReadFileByLine(filename);//ReadFileByBytes(filename);//ReadFileByChar(filename);
String writeFile = "javawrite2file.txt";//Write2FileByOutputStream(writeFile);//Write2FileByBuffered(writeFile);
Write2FileByFileWriter(writeFile);
}
}