一,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. VS2010-MFC(MFC常用类:CTime类和CTimeSpan类)

    转自:http://www.jizhuomi.com/software/230.html 上一节讲了MFC常用类CString类的用法,本节继续讲另外两个MFC常用类-日期和时间类CTime类和CTi ...

  2. getResourceAsStream的几种路径配置(properties文件的路径问题)

    文章目录 getResourceAsStream有以下几种: getResourceAsStream有以下几种: Class.getResourceAsStream(String path) : pa ...

  3. 4_7.springboot2.x嵌入式servlet容器自动配置原理

    概述 Spring Boot对所支持的Servlet Web服务器实现做了建模抽象: Servlet容器类型  WebServer模型接口 WebServer工厂实现类 Tomcat    Tomca ...

  4. C++自己用模板减少工作量

    今天写代码,遇到这样一个类似如下的需求: auto componentClassSavedByPLC = std::make_shared<ComponentClassSavedByPLC> ...

  5. 嘴巴题9 Codeforces 453A. Little Pony and Expected Maximum

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  6. 【默默努力】h5-game-heroVSmonster

    先放下作者大大的项目地址:https://github.com/yangyunhe369/h5-game-heroVSmonster 然后游戏的效果为 截动图的按键与游戏按键应该冲突,我就截几张图片了 ...

  7. 小程序跳转传参参数值为url时参数丢失

    通过先encodeURIComponent,取到值以后再decodeURIComponent,拼接参数正常传递 A页面 switch: function (e) { var aa = 'UNNZVUf ...

  8. MVVM test

    示例代码 public class RegisterUserViewModel { public UserInfo userInfo { get; set; } public ICommand Cli ...

  9. 动态设置背景图片,class,style

    1.动态设置背景图片<div v-if='img ' :style="{background: 'url('+ img +')',backgroundSize:cover }" ...

  10. js '' ""的嵌套使用

    1.我需要拼接一个字符串,但是其中 单引号内包含了双引号,双引号内又包含了单引号变量 这时我们想到了可以用到HTML特殊转义字符 2.如下拼接 return '<input type=" ...