java写入文件不覆盖写入

Java provides several ways to write to file. We can use FileWriter, BufferedWriter, java 7 Files and FileOutputStream to write a file in Java.

Java提供了几种写入文件的方法。 我们可以使用FileWriter , BufferedWriter ,java 7 Files和FileOutputStream用Java编写文件。

Java写入文件 (Java Write to File)

Let’s have a brief look at four options we have for java write to file operation.

让我们简要地看一下java写入文件操作的四个选项。

FileWriter: FileWriter is the simplest way to write a file in Java. It provides overloaded write method to write int, byte array, and String to the File. You can also write part of the String or byte array using FileWriter. FileWriter writes directly into Files and should be used only when the number of writes is less.

FileWriter :FileWriter是用Java编写文件的最简单方法。 它提供了重载的write方法,可以将int,字节数组和String写入File。 您也可以使用FileWriter编写String或byte数组的一部分。 FileWriter直接写入文件,仅在写入次数较少时才应使用。

BufferedWriter: BufferedWriter is almost similar to FileWriter but it uses internal buffer to write data into File. So if the number of write operations is more, the actual IO operations are less and performance is better. You should use BufferedWriter when the number of write operations is more.

BufferedWriter :BufferedWriter与FileWriter几乎相似,但是它使用内部缓冲区将数据写入File。 因此,如果写入操作的数量更多,则实际的IO操作会更少,而性能会更好。 当写入操作的数量更多时,应使用BufferedWriter。

FileOutputStream: FileWriter and BufferedWriter are meant to write text to the file but when you need raw stream data to be written into file, you should use FileOutputStream to write file in java.

FileOutputStream :FileWriter和BufferedWriter旨在将文本写入文件,但是当您需要将原始流数据写入文件时,应使用FileOutputStream在Java中写入文件。

Files: Java 7 introduced Files utility class and we can write a file using its write function. Internally it’s using OutputStream to write byte array into file.

Files :Java 7引入了Files实用程序类,我们可以使用其write函数编写文件。 在内部,它使用OutputStream将字节数组写入文件。

Java写入文件示例 (Java Write to File Example)

Here is the example showing how we can write a file in java using FileWriter, BufferedWriter, FileOutputStream, and Files in java.

这是显示如何在Java中使用FileWriter,BufferedWriter,FileOutputStream和Files编写文件的示例。

WriteFile.java

WriteFile.java

package com.journaldev.files;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.OutputStream;

import java.nio.file.Files;

import java.nio.file.Paths;

public class WriteFile {

/**

* This class shows how to write file in java

* @param args

* @throws IOException

*/

public static void main(String[] args) {

String data = "I will write this String to File in Java";

int noOfLines = 10000;

writeUsingFileWriter(data);

writeUsingBufferedWriter(data, noOfLines);

writeUsingFiles(data);

writeUsingOutputStream(data);

System.out.println("DONE");

}

/**

* Use Streams when you are dealing with raw data

* @param data

*/

private static void writeUsingOutputStream(String data) {

OutputStream os = null;

try {

os = new FileOutputStream(new File("/Users/pankaj/os.txt"));

os.write(data.getBytes(), 0, data.length());

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* Use Files class from Java 1.7 to write files, internally uses OutputStream

* @param data

*/

private static void writeUsingFiles(String data) {

try {

Files.write(Paths.get("/Users/pankaj/files.txt"), data.getBytes());

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* Use BufferedWriter when number of write operations are more

* It uses internal buffer to reduce real IO operations and saves time

* @param data

* @param noOfLines

*/

private static void writeUsingBufferedWriter(String data, int noOfLines) {

File file = new File("/Users/pankaj/BufferedWriter.txt");

FileWriter fr = null;

BufferedWriter br = null;

String dataWithNewLine=data+System.getProperty("line.separator");

try{

fr = new FileWriter(file);

br = new BufferedWriter(fr);

for(int i = noOfLines; i>0; i--){

br.write(dataWithNewLine);

}

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

br.close();

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* Use FileWriter when number of write operations are less

* @param data

*/

private static void writeUsingFileWriter(String data) {

File file = new File("/Users/pankaj/FileWriter.txt");

FileWriter fr = null;

try {

fr = new FileWriter(file);

fr.write(data);

} catch (IOException e) {

e.printStackTrace();

}finally{

//close resources

try {

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

These are the standard methods to write a file in java and you should choose any one of these based on your project requirements. That’s all for Java write to file example.

这些是用Java编写文件的标准方法,您应根据项目要求选择其中一种。 这就是Java写入文件示例的全部内容。

GitHub Repository. GitHub存储库中签出更多Java IO示例。

翻译自: https://www.journaldev.com/878/java-write-to-file

java写入文件不覆盖写入

java文件写入不覆盖_java写入文件不覆盖写入_Java写入文件–用Java写入文件的4种方法...相关推荐

  1. python中文件的存储类型_关于python中数据存储大总结,涵盖文件系统和数据库存储两种方法-文件系统类型...

    存储数据是python必不可免的话题,数据的存储类型也多种多样,文件系统存储(.txt..csv..json.多媒体存储).关系型数据库存储(MySQL等).非关系型数据库存储(MongoDB).今天 ...

  2. shell 获取命令执行结果_java高并发系列 第31天:获取线程执行结果,这6种方法你都知道?...

    这是java高并发系列第31篇. 环境:jdk1.8. java高并发系列已经学了不少东西了,本篇文章,我们用前面学的知识来实现一个需求: 在一个线程中需要获取其他线程的执行结果,能想到几种方式?各有 ...

  3. 超全总结:Go 读文件的 10 种方法

    大家好,我是明哥. Go 中对文件内容读写的方法,非常地多,其中大多数是基于 syscall 或者  os 库的高级封装,不同的库,适用的场景又不太一样,为免新手在这块上裁跟头,我花了点时间把这些内容 ...

  4. 判断文件是否被占用的两种方法

    今天开发产线测试Tool时发现日志文件会几率性的被占用,上网浏览找到最简单的代码(API或者FileStream),在这里抛砖引玉下. 第一种方法:API 1 using System.IO; 2 u ...

  5. [C#.Net]判断文件是否被占用的两种方法

    第一种方法:API 1 using System.IO; 2 using System.Runtime.InteropServices; 3 4 [DllImport("kernel32.d ...

  6. linux下打包deb文件,制作deb包的三种方法

    制作deb包的三种方法 发布时间:2008-06-10 16:05:39来源:红联作者:Mhynan 我所知道的制作deb包有三种方法,一种是将现有的文件打包,安装该包就像将打包的文件释放到某个目录: ...

  7. 零基础学 Go 语言(41):Go 读文件的七种方法

    查看本系列教程目录,请点击 零基础小白入门 Go语言 系列教程 Go 中对文件内容读写的方法,非常地多,其中大多数是基于 os 库的高级封装,不同的库,适用的场景又不太一样,为免新手在这块上裁跟头,我 ...

  8. Java学习笔记(笔记来源:B站UP主遇见狂神说的Java课程总结)

    该笔记是我看完B站UP主:遇见狂神说的Java部分课程后总结出来的内容,视频指路--->[[狂神说Java]Java零基础学习视频通俗易懂-哔哩哔哩 目录 Java基础(12节) Java基础0 ...

  9. Java中的string定义的两种方法和区别

    java中的String定义的两种方法和区别 第一种:new方式 String s1 = new String("hello world"); String s2 = new St ...

  10. java写入文件不覆盖写入_Java写入文件–用Java写入文件的4种方法

    java写入文件不覆盖写入 Java provides several ways to write to file. We can use FileWriter, BufferedWriter, ja ...

最新文章

  1. IT培训“来offer”获得数千万融资
  2. 设计一个成绩分类 输入成绩 输出优秀_BenQ 明基 PD2720U 27寸 Thunderbolt3.0 专业设计显示器开箱评测...
  3. LoadRunner 8.1 学习笔记4
  4. mysql now str,数据库学习之MySQL (十)—— 日期和时间函数 NOW STR_TO_DATE DATE_FORMAT 流程控制函数 IF IFNULL CASE...
  5. 从运维的角度理解Iaas、Paas、Saas云计算
  6. springboot 项目依赖
  7. SAP云平台CloudFoundry环境试用帐号过期了怎么办
  8. oracle不同session共享变量,SpringMVC使用oracle配置session共享
  9. UVA 12898 - And Or 与和或 (思路题)
  10. 一程序员反应职场怪现象
  11. linux 定时任务 crond 服务介绍
  12. 科大讯飞独家Founding赞助国际语音顶会,14篇论文被收录
  13. Python入门到精通(一):入门必备知识
  14. 数据结构Java第四版pdf_数据结构与抽象:Java语言描述(原书第4版) 中文pdf完整版[131MB]...
  15. 最新2022亲测可用的免费google翻译api
  16. 卫星通信术语名词备忘记录
  17. 电子沙盘 数字沙盘 开发教程
  18. 花生棒虚拟服务器,花生棒 开服务器
  19. Form表单之get提交与post提交
  20. 键盘控制盒子移动案例

热门文章

  1. Android小程序-涂鸦板
  2. 企业该如何用短信推广
  3. 这3款浏览器体积小,好用速度快,堪比夸克
  4. the info.plist in the package must contain the CFBundleShortVersionString key.
  5. 2023年开网店还能赚钱吗?去哪里找货源?
  6. 911报警电话数据分析
  7. curl使用用户名密码
  8. sql Sever 2012安装失败解决办法!
  9. Netty里面的Boss和Worker【Server篇】
  10. Android DoraemonKit 教程和简介