java追加写入TXT文件
2017-03-29 21:46
337 查看
java中,对文件进行追加内容操作的三种方法
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
// 如果文件存在,则追加内容;如果文件不存在,则创建文件,追加内容的三种方法
public class AppendContentToFile {
@SuppressWarnings(“static-access”)
public static void main(String[] args) {
AppendContentToFile a = new AppendContentToFile();
a.method1();
a.method2(“E:\personalitydic.txt”, “”);
a.method3(“E:\personalitydic”, “时尚/a”);
}
方法1:
public void method1() {
FileWriter fw = null;
try {
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f=new File(“E:\personalitydic.txt”);
fw = new FileWriter(f, true);//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)));// 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 {
// 打开一个随机访问文件流,按rw读写方式
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();
}
}
}
前面两个方法比较常用。
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
// 如果文件存在,则追加内容;如果文件不存在,则创建文件,追加内容的三种方法
public class AppendContentToFile {
@SuppressWarnings(“static-access”)
public static void main(String[] args) {
AppendContentToFile a = new AppendContentToFile();
a.method1();
a.method2(“E:\personalitydic.txt”, “”);
a.method3(“E:\personalitydic”, “时尚/a”);
}
方法1:
public void method1() {
FileWriter fw = null;
try {
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f=new File(“E:\personalitydic.txt”);
fw = new FileWriter(f, true);//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)));// 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 {
// 打开一个随机访问文件流,按rw读写方式
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();
}
}
}
前面两个方法比较常用。
相关文章推荐
- java如何追加写入txt文件
- java如何追加写入txt文件
- java如何追加写入txt文件
- java如何追加写入txt文件
- java如何追加写入txt文件
- Java 如何将数据追加写入到文件
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- Java 文件读写(txt类型读写并追加内容)
- Java读取txt文件和写入txt文件
- java实现持续写入内容到txt文件
- 将包中的所有java源文件代码写入一个txt文件中
- java中如何获取远程计算机post提交的数据并把这些数据写入本地的txt文件中
- Java读取txt文件 copy文件 写入文件
- Java读取txt文件和写入txt文件
- Java 追加写入文件
- java将字符串写入到txt文件中
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- java写入文件(文件内容末尾追加)
- java 流 写入txt文件, 并且设置txt编码格式
- Java基础:向txt文档中追加写入多个对象,并读取