写txt文件
来源:互联网 发布:mac软件更新不了 编辑:程序博客网 时间:2024/06/05 08:58
java通过代码写入txt文件:
// 方法1:
public void method1() {
FileWriter fw = null;
try {
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f=new File("E:\\dd.txt");
fw = new FileWriter(f, true);
}
catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw);
pw.println("追加内容");
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
// 方法2:
public static void method2(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
out.write(conent + "\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//方法3:
public static void method3(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content+"\r\n");
randomFile.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
0 0
- js写txt文件
- java 写txt文件
- 写txt 文件
- c# 写txt文件
- matlab 写txt文件
- c++写txt文件
- matlab 写txt文件
- java写txt文件
- xamrain 写txt文件
- 写txt文件
- WINCE下写TXT文件
- VB.NET 写TXT文件
- robots.txt文件怎么写
- 写文件到txt中
- java读、写txt文件
- 写LOG到TXT文件
- java写.txt文件换行
- C++中写txt文件
- [ZJOI2008树的计算]树链剖分
- 11.C#:浅谈HTTP中Get与Post的区别
- 一场奇怪的面试,TensorFlow 实现FizzBuzz
- ajax_用ajax实现多文件上传
- ueditor与struts2整合之图片上传
- 写txt文件
- QT-Socket编程之模拟TCP五层协议解/封装
- eclipse.ini配置详情
- React(二)
- 理解互斥量和信号量
- 浅谈浏览器http的缓存机制
- js表格隔行变色
- poj_3321 Apple Tree(dfs序+树状数组)
- 80老翁谈人生(351):北大违纪派存在的客观基础