java 将数据写到文本文件末尾三种方法
2013-10-24 18:52
288 查看
import java.io.FileOutputStream;
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表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void appendMethodC(String fileName, String content) {
try {
FileOutputStream fStream = new FileOutputStream(fileName, true);
fStream.write(content.getBytes());
fStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "E:/newTemp.dat";
String content = "new append!";
// 按方法A追加文件
appendMethodA(fileName, content);
appendMethodA(fileName, "append end.\n");
// 按方法B追加文件
appendMethodB(fileName, content);
appendMethodB(fileName, "append end.\n");
// 按方法C追加文件
appendMethodC(fileName, content);
appendMethodC(fileName, "append end.\n");
/*
* 打开文件"newTemp.dat"可以看到
new append!append end.
new append!append end.
new append!append end.
*/
}
}
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表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void appendMethodC(String fileName, String content) {
try {
FileOutputStream fStream = new FileOutputStream(fileName, true);
fStream.write(content.getBytes());
fStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "E:/newTemp.dat";
String content = "new append!";
// 按方法A追加文件
appendMethodA(fileName, content);
appendMethodA(fileName, "append end.\n");
// 按方法B追加文件
appendMethodB(fileName, content);
appendMethodB(fileName, "append end.\n");
// 按方法C追加文件
appendMethodC(fileName, content);
appendMethodC(fileName, "append end.\n");
/*
* 打开文件"newTemp.dat"可以看到
new append!append end.
new append!append end.
new append!append end.
*/
}
}
相关文章推荐
- 关于java的转义字符
- java 读取文件路径空格和中文的处理
- java:日期相关Date,Calendar,SimpleDateFormat
- 深入java SocketInputStream看read具体如何操作
- 【java源码】jdbc实现增查删改<Mysql>
- MyEclipse开发WebService教程
- xml与java双向映射
- java读取项目中的文件
- 如何在Eclipse CDT中编译含有多个main函数的项目
- 如何在Eclipse CDT中编译含有多个main函数的项目
- struts2+dao实现增删查改
- JDK自带线程池使用配置
- eclipse中设置代码字体
- java正则表达式测试
- Spring MVC学习--依赖注入
- JAVA解析JSON相关
- 5分钟学会十进制转换成二进制的快速口算方法 & Java输出二进制的代码
- java中的Class.forName的作用
- 搭建SpringSide示例showcase的eclipse运行环境
- Eclipse setting Java code style and codetemplate