IDEA InteilliJ 中的文件路径



正如标题所暗示的那样,我在IntelliJ上遇到了问题。对于我必须做的 Java (CS101) 作业,我需要提示用户输入一个字符串来表示应从中读取输入文件的文件路径。该文件称为 songwriterData.csv所需的路径是/iofiles。现在,我没有文件夹或任何称为iofiles的东西(我知道),所以我唯一真正的问题是将我的文件配置为具有此路径,以便我可以将songwriterData.csv放入路径中。下面是一个示例,说明给定不正确路径与正确路径的输出应读取的内容:

Enter path for input file: ioflies/
Looking for ioflies/songwriterData.csv
Cannot find songwriterData.csv. Make sure file exists and path is correct.

Enter path for input file: iofiles/
songwriterData.csv successfully found. Processing will continue.

因此,它应该仅在用户键入"/iofiles"时继续该过程

同样,我不确定如何将其转换为 intelliJ 识别的有效文件路径。任何帮助将不胜感激。

到目前为止,我已经编写了测试器代码:

public static String getInputFile(Scanner console) {
System.out.print("Enter path for input file:");
String path = console.nextLine();
String inputFile = path + "/songwriterData.csv";
File hello = new File(inputFile);
System.out.println(inputFile);
System.out.println(hello.exists());
return path;
}

但是当我运行程序时,将扫描程序从 main 方法传递给此方法,它返回 false(该文件不存在)。我将文件粘贴到项目中,这没有帮助。我基本上只需要让它返回真实,仅此而已。

Intellij Idea 和 Eclipse 都使用项目的根路径作为基础。因此,只需在项目文件夹中(而不是在 src 文件夹或其他项目子文件夹中)中创建 isoFile 文件夹。

最新更新