字节流

  • 初始化变量

    1
    private static final String FILENAME = "E:" + File.separator + "hello.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
private static void testWriteFile() {
File file = new File(FILENAME);
try {
//默认覆盖原内容,不向文件中追加新内容
OutputStream os = new FileOutputStream(file);
String txt = "你好,世界!";
byte[] bytes = txt.getBytes();
os.write(bytes);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void testWriteFileWithByte() {
File file = new File(FILENAME);
try {
//向文件中追加内容
OutputStream os = new FileOutputStream(file, true);
String txt = "你好,世界!";
byte[] bytes = txt.getBytes();
for (byte b : bytes) {
os.write(b);
}
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

读取文件内容

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
private static void testReadFile() {
File file = new File(FILENAME);
try {
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[1024];
int length = is.read(bytes);
is.close();
System.out.println("读取长度:" + length);
System.out.println("读取内容:" + new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}

private static void testReadFileUnknownSize() {
File file = new File(FILENAME);
try {
InputStream is = new FileInputStream(file);
//注意byte数组的初始大小,如果读取的数据大小超出时会报错
byte[] bytes = new byte[1024];
int count = 0;
int temp;
//只有当读到文件末尾的时候会返回-1
while((temp = is.read()) != -1) {
bytes[count++] = (byte) temp;
}
is.close();
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}