一,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写入文件

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

复制代码代码如下:
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例如,使用新的“尝试资源关闭”的方法来轻松处理文件。

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

Java文件写入的更多相关文章

  1. JAVA文件写入FileWriter

    JAVA文件写入FileWriter 导包import java.io.FileWriter创建构造方法public FileWrite(String filename),参数是文件的路径及文件名(默 ...

  2. Java文件写入,换行

    import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOExce ...

  3. 面试题之——将文件夹下java文件写入到新的文件夹,并修改扩展名

    题目:将d:/code/java文件夹下的所有.java文件复制到d:/code/java/jad文件夹下并且将原来的文件的扩展名.java改为.jad 源代码: package com.zyh.in ...

  4. Java文件写入与读取实例求最大子数组

    出现bug的点:输入数组无限大: 输入的整数,量大: 解决方案:向文件中输入随机数组,大小范围与量都可以控制. 源代码: import java.io.BufferedReader; import j ...

  5. java文件写入和读出的序列化

    文件的写入入与读出都有它们自己的格式,不便于读入和取出,implement Serializable接口,实现任何个事文件的写入和读取取:

  6. Java文件写入时是否覆盖

    这个是和服务器读数据结合着来的,是向服务器文件写数据,这就碰到了是否覆盖以前写的数据的问题,看FileWriter();的参数后面的参数名叫append,用词典查是附加的意思,灵机一动,改成false ...

  7. Java 通过 BufferReader 实现 文件 写入读取 示例

    package com.javatest.techzero.gui; import java.io.BufferedReader; import java.io.File; import java.i ...

  8. Java学习-018-EXCEL 文件写入实例源代码

    众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...

  9. Java学习-015-CSV 文件写入实例源代码

    在日常的自动化测试脚本编写的过程中,有时要将获取的测试结果或者测试数据存放在数据文件中,以用作后续的参数化测试.常用的文件文件类型无非 txt.csv.xls.properties.xml 这五种文件 ...

随机推荐

  1. 【Vue】组件的基础与组件间通信

    转载:https://segmentfault.com/a/1190000016409329 Vue.js 最核心的功能就是组件(Component),从组件的构建.注册到组件间通信,Vue .x 提 ...

  2. 04_Mybatis输入\出映射

    1. 输入映射 ​ 通过paramterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类. 1.1 传递pojo的包装对象 1.需求 ​ 完成用户信息的综合查询,需要传 ...

  3. python 对excel操作

    在python中,对excel表格读,写,追加数据,用以下三个模块:1.wlrd 读取excel表中的数据2.xlwt 创建一个全新的excel文件,然后对这个文件进行写入内容以及保存.3.xluti ...

  4. Spring 切面优先级(5)

    之前我们提过的应用场景,一个原始对象可能会需要插入多个切面,如果我们按前几篇博客文章介绍的方法完成切面及其通知的注解声明,那么它的执行顺序是怎么样的呢? 本文将介绍AspectJ的切面如何划分优先级 ...

  5. ThinkPHP无限分类模块设计

    public function catelist(){ $cate=D('Cate'); //var_dump($cate->gettree());exit; $cateres=$cate-&g ...

  6. Powerdesigner 生成数据字典

    https://www.jianshu.com/p/f491d0d3c503http://blog.csdn.net/adparking/article/details/50402980http:// ...

  7. ThinkPHP 数据更新

    ThinkPHP的数据更新操作包括更新数据和更新字段方法. 直线电机厂家 更新数据 更新数据使用save方法,例如: $User = M("User"); // 实例化User对象 ...

  8. 莫烦pytorch学习笔记(二)——variable

    .简介 torch.autograd.Variable是Autograd的核心类,它封装了Tensor,并整合了反向传播的相关实现 Variable和tensor的区别和联系 Variable是篮子, ...

  9. React中的Ajax

    React中的Ajax 组件的数据来源,通常是通过Ajax请求从服务器获取,可以使用componentDidMount方法设置Ajax请求,等到请求成功,再用this.setState方法重新渲染UI ...

  10. PAT甲级——A1006 Sign In and Sign Out

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...