java操作文本文件

java的操作相对python繁琐一点,所以这里把一些常用的记录下来。

java 操作文本文件


1.判断文件夹是不是存在,不存在创建

1
2
3
4
5
6
7
String path = "/home/tianchaoxiong/LinuxData/data/verifies/";
File file =new File(path);
//如果文件夹不存在则创建
if (!file.exists())
{
file.mkdir();
}

2.判断文件是不是存在,不存在创建

1
2
3
4
5
6
7
8
9
10
String fileName = "ceshi.txt";
File filename = new File(path+fileName);
if(!file.exists())
{
try {
file.createNewFile();//不存在直接创建
} catch (IOException e) {
e.printStackTrace();
}
}

3.java 从指定路径中读文本文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OptTxt {
public static void main(String[] args) throws IOException {
readTxt();
// saveTxt();
}

private static void readTxt() throws IOException {
// 可为文本相对路劲,可以文件夹绝对路径
String path3 = "ceshi.txt";
// StringBuffer用来接收解析完了之后的文本内容
StringBuffer sb = new StringBuffer();
// 自定义函数读文本 返回一个StringBuffer
readToBuffer(sb, path3);
// StringBuffer转为String显示
String resultString = sb.toString();
System.out.println(resultString);

}

public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
InputStream is = new FileInputStream(filePath);
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
reader.close();
is.close();
}
}

4.把txt写入指定路径的文件文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OptTxt {
public static void main(String[] args) throws IOException {
// readTxt();
saveTxt();
}

private static void saveTxt() throws IOException {
// 可为文本相对路劲,可以文件夹绝对路径
String path = "/home/tianchaoxiong/LinuxData/data/verifies/";
File file = new File(path);
// 如果文件夹不存在则创建
if (!file.exists()) {
file.mkdir();
}
// 如果文件不存在则创建
String fileName = "ceshi.txt";
File filename = new File(path + fileName);
if (!file.exists()) {
try {
file.createNewFile();// 不存在直接创建
} catch (IOException e) {
e.printStackTrace();
}
}
// 写入内容
String content = "This is the content to write into file";
writeToBuffer(content, filename);
}

private static void writeToBuffer(String content, File filename) {
try {
FileWriter fw = new FileWriter(filename.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("success");
} catch (IOException e) {
System.out.println("fail");
e.printStackTrace();
}

}
}
------本文结束感谢阅读------