java 将字符串追加到文件已有内容后面的操作

我就废话不多说了,大家还是直接看代码吧~

/**
  * 将字符串追加到文件已有内容后面
  *
  * @param fileFullPath 文件完整地址:D:/test.txt
  * @param content 需要写入的
  */
 public static void writeFile(String fileFullPath,String content) {
  FileOutputStream fos = null;
  try {
   //true不覆盖已有内容
   fos = new FileOutputStream(fileFullPath, true);
   //写入
   fos.write(content.getBytes());
   // 写入一个换行
   fos.write("\r\n".getBytes());

  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if(fos != null){
    try {
     fos.flush();
     fos.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

补充知识:java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结

代码如下:

import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class AppendToFile {
 /**
  * A方法追加文件:使用RandomAccessFile
  */
 public static void appendMethodA(String fileName, String content) {
  try {
   // 打开一个随机访问文件流,按读写方式
   RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
   // 文件长度,字节数
   long fileLength = randomFile.length();
   //将写文件指针移到文件尾。在该位置发生下一个读取或写入操作。
   randomFile.seek(fileLength);
   //按字节序列将该字符串写入该文件。
   randomFile.writeBytes(content);
   //关闭此随机访问文件流并释放与该流关联的所有系统资源。
   randomFile.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * B方法追加文件:使用FileWriter
  */
 public static void appendMethodB(String fileName, String content) {
  try {
   //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件,如果为 true,则将字节写入文件末尾处,而不是写入文件开始处
   FileWriter writer = new FileWriter(fileName, true);
   writer.write(content);
   writer.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  String fileName = "C:/Temp.txt";
  String content = "new append!";
  //按方法A追加文件
  AppendToFile.appendMethodA(fileName, content);
  AppendToFile.appendMethodA(fileName, "append end. \n");
  //显示文件内容
  ReadFromFile.readFileByLines(fileName);
  //按方法B追加文件
  AppendToFile.appendMethodB(fileName, content);
  AppendToFile.appendMethodB(fileName, "append end. \n");
  //显示文件内容
  ReadFromFile.readFileByLines(fileName);
 }
}

java控制台输出结果如下:

++++++readFileByLines:++++++

以行为单位读取文件内容,一次读一整行:

line 1: Sun Yat-sen(November 12, 1866–March 12, 1925) was a Chinese revolutionary and political leader who is often referred to as the "father of modern China". Sun played an instrumental and leadership role in the eventual overthrow of the Qing Dynasty in 1911. He was the first provisional president when the Republic of China was founded in 1912. He later co-founded the Kuomintang (KMT) where he served as its first leader. new append!append end.

++++++readFileByLines:++++++

以行为单位读取文件内容,一次读一整行:

line 1: Sun Yat-sen(November 12, 1866–March 12, 1925) was a Chinese revolutionary and political leader who is often referred to as the "father of modern China". Sun played an instrumental and leadership role in the eventual overthrow of the Qing Dynasty in 1911. He was the first provisional president when the Republic of China was founded in 1912. He later co-founded the Kuomintang (KMT) where he served as its first leader. new append!append end. line 2: new append!append end.

以上这篇java 将字符串追加到文件已有内容后面的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • java中concat()方法的使用说明

    concat()方法介绍: 将几个字符串连接到一起. 例如: s = s.concat(str1);//将字符串str1接到字符串s后面 s = s.concat(str2);//将字符串str1接到字符串s后面 代码: public class Test { public static void main(String[] args){ String s = "厉害了,"; String str1 = "我的"; String str2 = "国!&qu

  • java在原字符中插入新字符或字符串实例

    插入字符代码: public class Test { /**在原字符中插入新字符**/ public static void main(String[] args){ StringBuffer sb = new StringBuffer("田田是一个女生!");//建立一个字符缓存区,缓存区中的内容为"田田是一个女生!" System.out.println("原字符缓存区中的内容为:"+sb);//输出原字符缓存区中的内容 System.ou

  • java中构造方法和普通方法的区别说明

    1.普通方法: 定义:简单的说方法就是完成特定功能的代码块. 普通方法定义格式: 修饰符 返回值类型 方法名 (参数类型 参数名1,参数类型 参数名2,.........) { 函数体: return 返回值: } 返回值类型用于限定返回值的数据类型. 普通方法分为:有明确返回值的方法和没有明确返回值的方法. A.有明确返回值的方法的调用 可以单独调用(无意义).输出调用.赋值调用(推荐). public static int sum (int a , int b) { int c =a+b;

  • Java获取文件ContentType案例

    源码如下: package com.oysept; import java.io.File; import java.io.IOException; import java.net.FileNameMap; import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.activation.Mimetype

  • Java读写文件,在文件中搜索内容,并输出含有该内容的所有行方式

    1.问题描述 在一个目录及子目录下查找 TXT或Java文件,从中搜索所有"对象"字样的行. 在D盘中的所有文件中搜索含有"对象"的行. 2.解题思路 先找出D盘下所有文件 再对每个文件中的每行内容进行,进行查找,若含有"对象"两字,输出该行. 3.程序代码 import java.io.File; import java.io.IOException; import java.util.Scanner; public class B { sta

  • java 将字符串追加到文件已有内容后面的操作

    我就废话不多说了,大家还是直接看代码吧~ /** * 将字符串追加到文件已有内容后面 * * @param fileFullPath 文件完整地址:D:/test.txt * @param content 需要写入的 */ public static void writeFile(String fileFullPath,String content) { FileOutputStream fos = null; try { //true不覆盖已有内容 fos = new FileOutputSt

  • java实现合并2个文件中的内容到新文件中

    编写一个程序 将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中 a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔. 复制代码 代码如下: package javase.arithmetic; import com.google.common.base.Charsets; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.

  • java读取resource目录下文件的方法示例

    本文主要介绍的是java读取resource目录下文件的方法,比如这是你的src目录的结构 ├── main │ ├── java │ │ └── com │ │ └── test │ │ └── core │ │ ├── bean │ │ ├── Test.java │ └── resources │ └── test │ ├── test.txt └── test └── java 我们希望在Test.java中读取test.txt文件中的内容,那么我们可以借助Guava库的Resource

  • java 将字符串、list 写入到文件,并读取内容的案例

    我就废话不多说了,大家还是直接看代码吧~ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import ja

  • java把字符串写入文件里的简单方法分享

    这个示例可以很简单的把字符串写入到文件,大家可以试试哟,这是跟一个外国朋友学的代码,大家可以学习一下了 复制代码 代码如下: import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths; public class StringToFile {    public static void main(String[] args) throws IOException {        String

  • Java操作文件输出为字符串以及字符串输出为文件的方法

    文件输出为字符串示例代码: /** * 读取文件为字符串 * * @return */ public static String readString() { String str = ""; File file = new File("C:/Users/wan7/Desktop/表单/粗集料试验/粗集料冲击值试验(T0322-2000).html"); try { FileInputStream in = new FileInputStream(file); //

  • java正则匹配读取txt文件提取特定开头和结尾的字符串

    目录 前言 一.使用FileInputStream处理 二.使用正则开始匹配 1.匹配规则书写 2.pattern 代码案例 总结 前言 前天刚入职的算法同事,过来问我怎么提取txt文件中的数据,我一看这还不简单,结果…搞了好久. 正则不用真的会忘记,写篇博客增加一下记忆吧. 需求:提取txt文件中,有特定开头(双引号) ,特定结尾(双引号) 的中间的数据,打印出来 一.使用FileInputStream处理 FileInputStream:是java中的字节输入流,就是通过字节的形式进行读取

  • Java Socket+mysql实现简易文件上传器的代码

    最近跟着某网站学习了一个小项目,因为白天有课,所以都是晚上写的,今天把它完成了. 项目主要是实现一个文件上传器,通过客户端的登陆,把本地文件上传到服务器的数据库(本地的). 首先建两个表如下: 一个文件信息表 CREATE TABLE `fileinfo` ( `Fname` char(50) NOT NULL, `FInfo` blob NOT NULL, `FId` int(10) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`FId`) ) ENGINE=I

  • java开发之读写txt文件操作的实现

    项目结构: 运行效果: ======================================================== 下面是代码部分: ======================================================== /Text/src/com/b510/txt/MyFile.java 复制代码 代码如下: package com.b510.txt; import java.io.BufferedReader; import java.io.F

  • java异步写日志到文件中实现代码

    java异步写日志到文件中详解 实现代码: package com.tydic.ESUtil; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Properties; public class LogWriter { // 日志的配置文件 publi

随机推荐