一,FileWritter写入文件

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

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

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

代码如下 复制代码
new FileWriter(file,true);

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

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

代码如下 复制代码
package com.yiibai.file;
 
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
 
public class AppendToFileExample
{
    public static void main( 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 = new BufferedWriter(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 )是一个字符流类来处理字符数据。不同于字节流(数据转换成字节),你可以直接写字符串,数组或字符数据保存到文件。

代码如下 复制代码
package com.yiibai.iofile;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class WriteToFileExample {
 public static void main(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 = new FileWriter(file.getAbsoluteFile());
   BufferedWriter bw = new BufferedWriter(fw);
   bw.write(content);
   bw.close();
 
   System.out.println("Done");
 
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

三,FileOutputStream写入文件

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

代码如下 复制代码 http://www.111cn.net/jsp/Java/57796.htm
package com.yiibai.io;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class WriteFileExample {
 public static void main(String[] args) {
 
  FileOutputStream fop = null;
  File file;
  String content = "This is the text content";
 
  try {
 
   file = new File("c:/newfile.txt");
   fop = new FileOutputStream(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例如,www.111cn.net使用新的“尝试资源关闭”的方法来轻松处理文件。

package com.yiibai.io;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class WriteFileExample {
 public static void main(String[] args) {
 
  File file = new File("c:/newfile.txt");
  String content = "This is the text content";
 
  try (FileOutputStream fop = new FileOutputStream(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();
  }
 }
}

更多详细内容请查看:http://www.111cn.net/jsp/Java/57796.htm

java写入文件的几种方法小结的更多相关文章

  1. java写入文件的几种方法分享

    转自:http://www.jb51.net/article/47062.htm 一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所 ...

  2. [测试]java IO写入文件效率——几种方法比较

    各类写入方法 /** *1 按字节写入 FileOutputStream * * @param count 写入循环次数 * @param str 写入字符串 */ public void outpu ...

  3. Java读写文件的几种方法

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java ...

  4. iOS开发小技巧--边接受数据边写入文件的两种方法

    一.NSFileHanle 使用注意点:在往文件写入数据时,必须创建一个空的文件 指定文件写入的方式 -- 覆盖还是追加 最后记得关闭 <1>代码是在大文件传输的练习中截取的.写入数据之前 ...

  5. Android 追加写入文件的三种方法

    一.使用FileOutputStream 使用FileOutputStream,在构造FileOutputStream时,把第二个参数设为true public static void method1 ...

  6. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  7. IO流----操作文件的9种方法代码实现

    IO流----操作文件的9种方法代码实现: 1:使用字节流读写数据: 四种方式: method1:          每次读写一个字节,边读边写: /* * 复制文本文件. * * 数据源:从哪里来 ...

  8. [转载]C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...

  9. java解析xml的三种方法

    java解析XML的三种方法 1.SAX事件解析 package com.wzh.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXE ...

随机推荐

  1. [转]iOS应用性能调优的25个建议和技巧

    写在前面 本文来自iOS Tutorial Team 的 Marcelo Fabri,他是Movile的一名 iOS 程序员.这是他的个人网站:http://www.marcelofabri.com/ ...

  2. Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力

    A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...

  3. Codeforces Round #309 (Div. 1) C. Love Triangles dfs

    C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...

  4. 搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)

    整合Spring和SpringMVC 之前已经整合了spring和mybatis,现在在此基础上整合SSM. 项目目录: 思路:SpringMVC的配置文件独立,然后在web.xml中配置整合. (1 ...

  5. Yeoman+Express+Angular在Linux上开发配置方法

    $mkdir ExpressWithAngularTest $cd ExpressWithAngularTest choose needed components you'd like to add ...

  6. android复制数据库到SD卡(网上搜集,未经验证)

    android中使用sqlite.复制assets下的数据库到SD卡.支持大于1M的文件 如果使用SD卡,需要在AndroidManifest.xml中设置权限 <uses-permission ...

  7. Getting NHibernate to generate a HiLo string ID

    We've got a large system that's loosely bound to its data source (Navision) via Unity - we're gettin ...

  8. 15+ tar command usages with examples – Unix/Linux--reference

    reference :http://crybit.com/tar-command-usages-with-examples/ The ‘tar’ saves many files together i ...

  9. windows下 定时删除tomcat日志和缓存。可以保留天数

    forfiles /p "e:\Program Files\Tomcat 7.0\logs" /s /m *.log /d -5 /c "cmd /c del @path ...

  10. Dispatcher中Invoke与BeginInvoke

    [同步]Invoke Application.Current.Dispatcher.Invoke(AutoIncreaseNumber); [异步]BeginInvoke Application.Cu ...