绝对路径则需要经常更改,为此使用相对路径是一个不错的选择。但是使用./,../或.\\,..\\的方式会经常出现未找到文件的错误需要读取的文件路径为 项目根目录\experiment\1.txt
System.getProperty("user.dir")获取当前程序运行的工作根目录File.separator表示目录的分割符,此操作需要import java.io.FileString root = System.getProperty("user.dir");
String FileName="1.txt";
String filePath = root+File.separator+"experiment"+File.separator+FileName;
FileReader fr = new FileReader(filePath);
原文:https://www.cnblogs.com/cloud-ken/p/9963443.html