java 文件的读写_Java读写文件的几种方式

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值