Java通过java.io.FileInputStream读取txt文本。
package com;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestIO {
public static void main(String[] args) {
FileInputStream in = null;
try {
in = new FileInputStream("test.txt");
int c;
c = in.read();
while (c != -1) {
System.out.print((char) c);
c = in.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
其中test.txt内为英文字符串,放在eclipse项目文件夹根目录下,对于中文需要做编码处理。