Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
53 lines (40 loc) · 1.05 KB

File metadata and controls

53 lines (40 loc) · 1.05 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 文件字节输入流
*/
public class FileInputStreamDemo {
public static void main(String[] args) {
read();
}
// 读取数据
public static void read() {
File file = new File("d:" + File.separator + "test.txt");
try {
// 针对文件创建一个字节输入流
InputStream in = new FileInputStream(file);
// 创建一个10KB的字节数组,表示每次最大读取的长度
byte[] bytes = new byte[10 * 1024];
// 每次实际读取的长度
int len = -1;
// 创建一个StringBuffer用于保存每次读取的数据
StringBuffer stringBuffer = new StringBuffer();
// 开始读取数据
while ((len = in.read(bytes)) != -1) {
stringBuffer.append(new String(bytes, 0, len));
}
// 最后记得要关闭字节输入流释放资源
in.close();
// 打印出读取出来的数据
System.out.println(stringBuffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.