gpt4 book ai didi

java - 在java中,当我们使用函数 'FileReader(String fileName)' 时,我们应该将文件 fileName 保存在哪里?

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:35 24 4
gpt4 key购买 nike

在我的程序中,我使用了函数FileReader(String fileName)来读取文件。该文件保存在 fileName 中,其中包含一个字符串 setup.ini。我已将此文件保存在编译 java 程序的同一文件夹中,但即使从该文件夹中删除了该文件,我也没有收到任何未找到文件的异常。所以我想知道编译器是否从其他位置获取该文件?

请参阅下面的代码:

public class ReadINI
{
public static void main(String args[]) throws IOException
{
String s = getParameter("bin","setup.ini");
System.out.println("Result " + s);
}

public static String getParameter(String inputValue, String fileName)
{
try
{
BufferedReader myInput = new BufferedReader(new FileReader(fileName));
try
{

try {
String fileLine;
fileLine = myInput.readLine();

do
{
String stringArray[] = fileLine.split("=");
if (inputValue.equals(stringArray[0]))
return stringArray[1];
}while ((fileLine = myInput.readLine()) != null);
}
catch (Exception e)
{
System.err.println("Error1: " + e);
}
} // end try
catch (Exception e)
{
System.err.println("Error2: " + e);
}

} // end try
catch (Exception e)
{
System.err.println("failed to open file setup.ini");
System.err.println("Error3: " + e);
}
return "Not Found";
}

}

最佳答案

编译器不会在任何地方搜索您的文件。该文件是在运行时而不是编译时搜索的。如果您提供相对路径,系统将在您运行程序的目录中搜索该文件。

关于java - 在java中,当我们使用函数 'FileReader(String fileName)' 时,我们应该将文件 fileName 保存在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15333999/

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