本文為joshua317原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載自joshua317博客?Java如何讀取文件文本內(nèi)容的幾種方式匯總 - joshua317的博客文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-520183.html
package com.joshua317;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;
public class FileDemo {
static String filePath = "E:\\1.txt";
public static void main(String[] args) throws IOException {
readFile1();
// readFile1_1();
// readFile2();
// readFile3();
// readFile4();
// readFile5();
// readFile6();
// readFile6_1();
// readFile7();
// readFile7_1();
}
/**
* 字節(jié)流>字符流>字符串
* 使用File,FileInputStream,InputStreamReader,BufferedReader
* 使用FileInputStream,InputStreamReader,BufferedReader
* @throws IOException
*/
public static void readFile1() throws IOException {
// 使用File
// File file = new File(filePath);
// FileInputStream fileInputStream = new FileInputStream(file);
// FileInputStream(字節(jié)流) 實(shí)現(xiàn)了InputStream接口,用來(lái)讀取文件中的字節(jié)流,參數(shù)是文件或者文件路徑+文件名稱(chēng)
FileInputStream fileInputStream = new FileInputStream(filePath);
// 將 fileInputStream(字節(jié)流) 流作為參數(shù),轉(zhuǎn)為InputStreamReader(字符流)
InputStreamReader reader = new InputStreamReader(fileInputStream);
// 將 字符流(參數(shù))轉(zhuǎn)為字符串流,帶緩沖的流讀取,默認(rèn)緩沖區(qū)8k
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
fileInputStream.close();
bufferedReader.close();
}
public static void readFile1_1() throws IOException {
// 使用File
// File file = new File(filePath);
// FileInputStream fileInputStream = new FileInputStream(file);
// FileInputStream(字節(jié)流) 實(shí)現(xiàn)了InputStream接口,用來(lái)讀取文件中的字節(jié)流,參數(shù)是文件或者文件路徑+文件名稱(chēng)
FileInputStream fileInputStream = new FileInputStream(filePath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
while ((i = fileInputStream.read()) != -1) {
byteArrayOutputStream.write(i);
}
System.out.println(byteArrayOutputStream.toString());
fileInputStream.close();
byteArrayOutputStream.close();
}
/**
* 使用File,FileReader,BufferedReader
* 使用FileReader,BufferedReader
* @throws IOException
*/
public static void readFile2() throws IOException {
// 使用File
// File file = new File(filePath);
// FileReader fileReader = new FileReader(file);
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
fileReader.close();
bufferedReader.close();
}
/**
* 使用FileReader
* @throws IOException
*/
public static void readFile3() throws IOException {
// 使用File
// File file = new File(filePath);
// FileReader fileReader = new FileReader(file);
FileReader fileReader = new FileReader(filePath);
int i;
while ((i = fileReader.read()) != -1) {
System.out.print((char)i);
}
fileReader.close();
}
/**
* 使用Scanner
* @throws IOException
*/
public static void readFile4() throws IOException {
File file = new File(filePath);
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
sc.close();
}
/**
* 讀取整個(gè)文件
* 使用Files.readAllBytes,Paths
* @throws IOException
*/
public static void readFile5() throws IOException {
Path path = Paths.get(filePath);
byte[] bytes = Files.readAllBytes(path);
String data = new String(bytes);
System.out.println(data);
}
/**
* 讀取整個(gè)文件
* 使用Files.readAllLines,Paths,迭代器
* @throws IOException
*/
public static void readFile6() throws IOException {
Path path = Paths.get(filePath);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
Iterator<String> itr = lines.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
/**
* 使用Files.lines
* @throws IOException
*/
public static void readFile6_1() throws IOException {
Path path = Paths.get(filePath);
Stream<String> lines = Files.lines(path);
lines.forEach(e -> {
System.out.println(e);
});
}
/**
* 使用Files.newBufferedReader
* @throws IOException
*/
public static void readFile7() throws IOException {
Path path = Paths.get(filePath);
BufferedReader bufferedReader= Files.newBufferedReader(path);
String str;
while ((str = bufferedReader.readLine())!=null){
System.out.println(str);
}
bufferedReader.close();
}
public static void readFile7_1() throws IOException {
Path path = Paths.get(filePath);
BufferedReader bufferedReader= Files.newBufferedReader(path);
String str;
StringBuilder stringBuilder = new StringBuilder();
while ((str = bufferedReader.readLine())!=null){
stringBuilder.append(str + "\n");
}
System.out.println(stringBuilder.toString());
bufferedReader.close();
}
}
本文為joshua317原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載自joshua317博客?Java如何讀取文件文本內(nèi)容的幾種方式匯總 - joshua317的博客文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-520183.html
到了這里,關(guān)于Java如何讀取文件文本內(nèi)容的幾種方式匯總的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!