import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class ReadStringFromFileAppMain {
private static final String SOURCE_FILE_NAME = "ajajaj.txt";
public static void main(String[] args) throws IOException {
Scanner in=new Scanner(System.in);
File sourceFile = new File("F:" + File.separator + "myjava" + File.separator + "lets_java" + File.separator + SOURCE_FILE_NAME);
classicWay(sourceFile);
coolerWay(sourceFile);
}
private static void classicWay(File sourceFile) throws IOException {
System.out.println("经典方式读取文件");
try (
FileInputStream fis = new FileInputStream(sourceFile);
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr);
){
String readLine = null;
while ((readLine = reader.readLine()) != null) {
System.out.println(readLine.toString().toUpperCase());
}
}
}
private static void coolerWay(File sourceFile) {
System.out.println("时尚方式读取文件");
try (
FileInputStream fis = new FileInputStream(sourceFile);
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr);
){
reader.lines().map(String::trim).map(String::toUpperCase).forEach(System.out::println);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}