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写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结的更多相关文章
- 用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载
用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载,将一个完整的项目进行展示,主要有以下几个部分: 1.servlet部分 Export 2.工具类:TxtFileU ...
- 安装Ruby、Sass在WebStrom添加Watcher实现编辑scss文件时自动生成.map和压缩后的.css文件
前言 这段时间一直在看Bootstrap,V3官方直接提供了Less版本的源码,就先将Less学完了,很简单的语法,学习写Demo都是在Webstorm里写的,配置了Watcher自动编译(详见< ...
- 合并BIN文件的两种方法(转)
源:http://blog.chinaunix.net/uid-20745340-id-1878803.html 合并BIN文件的两种方法 在单片机的开发过程中,经常需要将两个单独的BIN文件合并成一 ...
- Python修改文件的两种方法
目录: 一.以占用内存的方式修改文件 二.以占用硬盘的方式修改文件 引言 文件修改的方法从操作方式上大致可以分为两类,一种是以占用电脑内存的方式,将文件读取到内存中修改再存回硬盘:第二种方法是分别打开 ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- MySQL命令执行sql文件的两种方法
MySQL命令执行sql文件的两种方法 摘要:和其他数据库一样,MySQL也提供了命令执行sql脚本文件,方便地进行数据库.表以及数据等各种操作.下面笔者讲解MySQL执行sql文件命令的两种方法,希 ...
- C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- 自制按钮图标的两种方法: image sprite和svg字体文件
用image sprite和svg字体文件这两种方法,都能够极大地减少小图形文件的数量, 从而减少服务器请求和带宽需求.提高网页的响应速度. 一.建立SVG字体文件 iconmoon 是一个在线工具, ...
- elf格式转换为hex格式文件的两种方法
这周工作终于不太忙了,可以写点笔记总结一下了. 之前的文章如何在Keil-MDK开发环境生成Bin格式文件,介绍了如何在Keil开发环境使用fromelf软件,将生成的axf文件转换为bin文件,这次 ...
随机推荐
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...
- java——数组
数组是多个同样数据类型数组组合,当中数据类型是不论什么数据类型. 数组变量是引用类型变量,数组能够作为对象,数组中的每个元素相当于对象的成员变量,所以数组元素能够默认初始化.(博客java--变量分类 ...
- [Angular] Design API for show / hide components based on Auth
Simple Auth service: import { Injectable } from '@angular/core'; import {HttpClient} from '@angular/ ...
- [React Intl] Format a Date Relative to the Current Date Using react-intl FormattedRelative
Given a date, we’ll use the react-intl FormattedRelative component to render a date in a human reada ...
- apache与IIS共用80端口冲突解决方法
如果同一台电脑安装了apache和iis,会提示80端口冲突,如何解决apache与iis 80端口冲突的问题呢,并且同时使用apache和iis 将apache设为使用80端口,IIS使用其它端口, ...
- PHP回调函数--call_user_func_array
我这是抄的 感谢 https://www.cnblogs.com/zzl-21086595/p/4547519.html 全局函数的回调 这里的全局函数的意思,是直接使用function定义的函数,它 ...
- DOCKER学习心得
原文:DOCKER学习心得 前言: Docker的主要学习心得来源于<docker技术入门与实战> --2019.1.1->2019.1.5 la 着重从基础部分--实例分析-- ...
- 【微信小程序】自定义模态框实例
原文链接:https://mp.weixin.qq.com/s/23wPVFUGY-lsTiQBtUdhXA 1 概述 由于官方API提供的显示模态弹窗,只能简单地显示文字内容,不能对对话框内容进行自 ...
- 如何在 Linux 中统计一个进程的线程数
编译自:http://ask.xmodulo.com/number-of-threads-process-linux.html作者: Dan Nanni原创:LCTT https://linux.cn ...
- 【转】HTML5移动端最新兼容问题解决方案
1.安卓浏览器看背景图片,有些设备会模糊. 用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率来显 ...