Java读取文件字节流
Java读取文件字节流
Java文件流
流是一个很重要的概念。这里的话不阐述,就简单说说文件的读。这里的话我们尽可能的和python的文件的读进行比较,写出一个类似的功能。
文件的简单读写
读文件直接使用read方法
但是,这个方法的调用没有python那么简单。现在我们先演示一下。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Fileread {
public static void main(String[] args) {
String path = "C:\\Users\\31395\\Desktop\\test\\hello.txt";
try {
FileInputStream fls = new FileInputStream(path);
int n = 0;
while(true){
n = fls.read();//文件结束的时候返回的是-1,此外注意异常接受
System.out.print((char)n);//这里强制转换为char显然中文是会乱码的
if(n==-1){
break;}
}
fls.close()//不要忘了要关掉
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}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
此外你可以使用一个字节数组来接收
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Fileread {
public static void main(String[] args) {
String path = "C:\\Users\\31395\\Desktop\\test\\hello.txt";
try {
FileInputStream fls = new FileInputStream(path);
byte[] a = new byte[100];
//fls.read(a,1,5);1,5是指对a操作在a[1]开始存数据。存5个
fls.read(a);//可以处理中文问题
String get = new String(a);
System.out.println(get);
fls.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}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
此外你还可以使用字节数组和read()混合使用,好处是可以做到逐行读取
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Fileread {
public static void main(String[] args) {
String path = "C:\\Users\\31395\\Desktop\\test\\hello.txt";
try {
FileInputStream fls = new FileInputStream(path);
byte[] a = new byte[100];
int n = 0;
int i = 0;
while(true){
n = fls.read()
if(n!=-1){
a[i] = (byte)n;
i++;
}
else{
break;
}
}
String get = new String(a);
System.out.println(get);
fls.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}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
33
34
35
36
37
38
文件逐行读取
这个我觉得是很重要的
因为python的readline()我觉得的很不错。但是在Java中,如图:
没有找到类似的方法,这个时候就不得不自己动脑子想一想了。
结合read()和字节数组可以找到一个思路,那就是逐一提取,判断(char)n !=’\n’。
这里的话我直接写了一个类。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileReadLine {
private String path = null;
List<String> Line_String = new ArrayList<String>();
public FileReadLine(){
}
public FileReadLine(String path){
this.setPath(path);
}
public String getPath() {
return path;
}
public void setPath(String path) {
File file = new File(path);
if(file.exists()){
this.path = path;}
else{
this.path = null;
}
}
public List<String> getLine_String() {
return Line_String;
}
public void setLine_String(List<String> line_String) {
Line_String = line_String;
}
public List<String> GetLine() {
String path = this.getPath();
if(path==null){
return Line_String;
}
byte line[] = new byte[100];
FileInputStream fls=null;
int n = 0;
try {
fls = new FileInputStream(path);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
int i = 0;
while(true){
try {
n = fls.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if((char)n!='\n'){
line[i]=(byte)n;
i++;//这里的i显然统计了多少个元素
}
else{
String get = new String(line);
Line_String.add(get);
i=0;
line = new byte[100];
}
if(n==-1){
line[i-1]=0;
String get = new String(line);
Line_String.add(get);
break;
}
}
return Line_String;
}
}
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
下面我们来做一个测试
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
String path = "C:\\Users\\31395\\Desktop\\test\\hello.txt";
FileReadLine FileLines = new FileReadLine(path);
List<String> Line_String = FileLines.GetLine();
for(String Line:Line_String){
System.out.println(Line);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
接下来还有很多方法没有实现,我觉得可以试着去慢慢实现,把它做成一个比较厉害的工具类。
————————————————
版权声明:本文为CSDN博主「HUTEROX」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/FUTEROX/article/details/116174904