gpt4 book ai didi

java - 如何使用 FileRead 读取文本?

转载 作者:行者123 更新时间:2023-12-02 05:20:50 25 4
gpt4 key购买 nike

我正在使用两台计算机处理一个项目,第一台在工作,第二台在家里。当我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com