本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java如何读取文件文本内容的几种方式汇总 - joshua317的博客文章来源:https://www.toymoban.com/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();
}
/**
* 字节流>字符流>字符串
* 使用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(字节流) 实现了InputStream接口,用来读取文件中的字节流,参数是文件或者文件路径+文件名称
FileInputStream fileInputStream = new FileInputStream(filePath);
// 将 fileInputStream(字节流) 流作为参数,转为InputStreamReader(字符流)
InputStreamReader reader = new InputStreamReader(fileInputStream);
// 将 字符流(参数)转为字符串流,带缓冲的流读取,默认缓冲区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(字节流) 实现了InputStream接口,用来读取文件中的字节流,参数是文件或者文件路径+文件名称
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();
}
/**
* 读取整个文件
* 使用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);
}
/**
* 读取整个文件
* 使用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原创文章,转载请注明:转载自joshua317博客 Java如何读取文件文本内容的几种方式汇总 - joshua317的博客文章来源地址https://www.toymoban.com/news/detail-520183.html
到了这里,关于Java如何读取文件文本内容的几种方式汇总的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!