目录
FileInputStream有两种构造方式
示例代码:
import java.io*;
public class MainTest{
public static void main(String args[]){
String filePath = "D:/test/a.txt";
FileInputStream fis = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String strTmp = "";
while((strTmp = br.readLine())!=null){
System.out.println(strTmp);
}
br.close();
}
}
Path path = Paths.get("D:/test/a.txt");
String data = Files.readString(path);
System.out.println(data);
Path path = Paths.get("D:/aa.txt");
List<String> lines = Files.readLines(path);
Path path = Paths.get("D:/aa.txt");
byte[] data = Files.readAllBytes(path);
String result = new String(data, "utf-8");
CodepageDetectorProxy codepageDetectorProxy = CodepageDetectorProxy.getInstance();
codepageDetectorProxy.add(JChardetFacade.getInstance());
// Charset就是字符集,可以用来解码byte数组为字符串
Charset charset = codepageDetectorProxy.detectCodepage(file.toURI().toURL());
? ? ? ? 2.1?使用DataInputStream?
DataInputStream din = new DataInputStream(new FileInputStream(file));
byte[] data = new byte[1024];
while(din.read(data) > 0) {
? ? // 处理数据
}
din.close();?
? ? ? ? 2.2 使用RandomAccessFile
RandomAccessFile randomFile = new RandomAccessFile(file, "r");
byte[] data = new byte[1024];
while(randomFile.read(data) > 0) {
// 处理数据
}
randomFile.close();
?? ? ? ? 2.3 DataInputStream 与 RandomAccessFile 的区别
????????RandomAccessFile可以通过移动文件指针改变读取的位置,可以按照几种基本类型直接读取数据,可以跳过一定的字节。
????????DataInputStream是输入流。??