package file.system.demo.exception;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
public static String getFile(String realpath) {
Scanner scanner = null;
String text = "";
try {
File file= new File(realpath);
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(scanner!=null){
while(scanner.hasNextLine()){
text+=scanner.nextLine();
}
scanner.close();
}
//System.out.println(text);
return text;
}
static class InnerTest{
public static void main(String[] args) {
String realpath = "D:\\test.txt";
String text=getFile(realpath);
System.out.println(text);
}
}
}
实现方式有很多,还可以用字节流FileInputStream,字符流FileReader等方式读取
纯文本还是?