java写入文件中断_java写入文件的几种方法

一,FileWritter写入文件

FileWritter, 字符流写入字符到文件。默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函数的第二个参数,它会保留现有的内容,并追加新内容在文件的末尾。

1. 替换所有现有的内容与新的内容。

new FileWriter(file);2. 保留现有的内容和附加在该文件的末尾的新内容。

new FileWriter(file,true);

追加文件示例

一个文本文件,命名为“javaio-appendfile.txt”,并包含以下内容。

ABC Hello追加新内容 new FileWriter(file,true)

packagecom.yiibai.file;importjava.io.File;importjava.io.FileWriter;importjava.io.BufferedWriter;importjava.io.IOException;public classAppendToFileExample

{public static voidmain( String[] args )

{try{

String data= " This content will append to the end of the file";

File file=new File("javaio-appendfile.txt");//if file doesnt exists, then create it

if(!file.exists()){

file.createNewFile();

}//true = append file

FileWriter fileWritter = new FileWriter(file.getName(),true);

BufferedWriter bufferWritter= newBufferedWriter(fileWritter);

bufferWritter.write(data);

bufferWritter.close();

System.out.println("Done");

}catch(IOException e){

e.printStackTrace();

}

}

}

结果

现在,文本文件“javaio-appendfile.txt”内容更新如下:

ABC Hello This content will append to the end of the file

二,BufferedWriter写入文件

缓冲字符(BufferedWriter )是一个字符流类来处理字符数据。不同于字节流(数据转换成字节),你可以直接写字符串,数组或字符数据保存到文件。

packagecom.yiibai.iofile;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;public classWriteToFileExample {public static voidmain(String[] args) {try{

String content= "This is the content to write into file";

File file= new File("/users/mkyong/filename.txt");//if file doesnt exists, then create it

if (!file.exists()) {

file.createNewFile();

}

FileWriter fw= newFileWriter(file.getAbsoluteFile());

BufferedWriter bw= newBufferedWriter(fw);

bw.write(content);

bw.close();

System.out.println("Done");

}catch(IOException e) {

e.printStackTrace();

}

}

}

三,FileOutputStream写入文件

文件输出流是一种用于处理原始二进制数据的字节流类。为了将数据写入到文件中,必须将数据转换为字节,并保存到文件。请参阅下面的完整的例子。

packagecom.yiibai.io;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;public classWriteFileExample {public static voidmain(String[] args) {

FileOutputStream fop= null;

File file;

String content= "This is the text content";try{

file= new File("c:/newfile.txt");

fop= newFileOutputStream(file);//if file doesnt exists, then create it

if (!file.exists()) {

file.createNewFile();

}//get the content in bytes

byte[] contentInBytes =content.getBytes();

fop.write(contentInBytes);

fop.flush();

fop.close();

System.out.println("Done");

}catch(IOException e) {

e.printStackTrace();

}finally{try{if (fop != null) {

fop.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

}//更新的JDK7例如,使用新的“尝试资源关闭”的方法来轻松处理文件。

packagecom.yiibai.io;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;public classWriteFileExample {public static voidmain(String[] args) {

File file= new File("c:/newfile.txt");

String content= "This is the text content";try (FileOutputStream fop = newFileOutputStream(file)) {//if file doesn't exists, then create it

if (!file.exists()) {

file.createNewFile();

}//get the content in bytes

byte[] contentInBytes =content.getBytes();

fop.write(contentInBytes);

fop.flush();

fop.close();

System.out.println("Done");

}catch(IOException e) {

e.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值