读取项目中放置的文件

1. 获取文件3.txt:

A、相对路径:从src开始写起

//里面路径写成:3.txt报错:java.io.FileNotFoundException: 3.txt (系统找不到指定的文件。)
FileInputStream in = new FileInputStream( "src\\3.txt"); 

B、绝对路径:

String path = new File("").getCanonicalPath()+ "\\src\\";
System.out.println("path: " + path);//path: E:\programfiles\idea\testboot\src\
FileReader reader = new FileReader(path + "3.txt");

2. 获取文件1.txt

InputStream in3 = new FileInputStream("1.txt");

猜你喜欢

转载自blog.csdn.net/qq_38384102/article/details/83893267
今日推荐