我正在使用两台计算机处理一个项目,第一台在工作,第二台在家里。当我使用 FileRead
时,我需要更改 FileRead
需要读取的位置。
例如在家里是这样的:
ReadFile rf = new ReadFile("C:\\Users\\Home\\Desktop\\Project\\src\\triangle3d\\TheText.ase");
当我通过电子邮件给自己发送此信息并在工作场所打开它时,我每次都需要将其更改为:
ReadFile rf = new ReadFile("C:\\Users\\Work\\Desktop\\Project\\src\\triangle3d\\TheText.ase");
现在有没有一种方法可以找到 TheText.ase 而无需说明其位置? 这行得通吗? :
ReadFile rf = new ReadFile("TheText.ase");
最佳答案
public class LocationFileManager
{
public LocationFileManager(String s)
{
ReadFile rf=new ReadFile("C:\Users\\" + s + "\Desktop\Project\src\triangle3d\TheText.ase");
}
public static void main(String[] args)
{
String s = ""; //<--------- enter here either Home or Work
LocationFileManager lfm = new LocationFileManager(s);
}
}
您只需在主方法中设置 s 的值即可选择是在家还是工作
编辑 --- 天哪,如果这仍然太多工作,那么试试这个......
import javax.swing.JOptionPane;
public class LocationFileManager
{
public LocationFileManager(String s)
{
ReadFile rf=new ReadFile("C:\Users\\" + s + "\Desktop\Project\src\triangle3d\TheText.ase");
}
public static void main(String[] args)
{
Object[] possibleValues = { "Home", "Work"};
String s = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
LocationFileManager lfm = new LocationFileManager(s);
}
}
关于java - 如何使用 FileRead 读取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26530745/