[英]In java when we use the function 'FileReader(String fileName)', where should we keep the file fileName?
In my program I have used the function FileReader(String fileName)
to read a file. 在我的程序中,我使用了
FileReader(String fileName)
函数来读取文件。 This file is kept at fileName is holding a string setup.ini
. 该文件保存在fileName中,其中包含一个字符串
setup.ini
。 I have kept this file in the same folder from where I am compiling my java program but even after I have removed the file from this folder I am not getting any Exception of File not found. 我将此文件保存在编译Java程序时所在的文件夹中,但是即使从该文件夹中删除了该文件,也没有找到File Exception。 So I wonder does the compiler take the file from some other location?
因此,我想知道编译器是否从其他位置获取文件吗?
Please see the code below: 请参见下面的代码:
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";
}
}
The compiler doesn't search for your file anywhere. 编译器不会在任何地方搜索您的文件。 The file is searched for at run time, not at compile time.
在运行时而不是编译时搜索文件。 If you give a relative path, the file will be searched for in the directory where you run the program.
如果提供相对路径,将在运行程序的目录中搜索文件。
the file should be right inside your project outside src directory ,
the file should be in the same folder where src folder is present
|--MyProject
|--src
|--youFile.txt
or you can give full path to the file which is located anywhere on the disk
String fileName = "c:/folder1/folder2/yourFile.txt";
Any request to open a file inside a java application makes the JVM start its search from the CLASSPATH
of that particular java class. 任何在Java应用程序内打开文件的请求都会使JVM从该特定Java类的
CLASSPATH
开始搜索。 So, when you place the file you want to open in code in the CLASSPATH
- basically the folder in which the .java file (and hence the compiled .class file) resides. 因此,当您要在
CLASSPATH
中的代码中放置要打开的文件时-基本上就是.java文件(以及因此的已编译.class文件)所在的文件夹。 Elaborating, if your file someFile.txt
is to be accessed by SomeClass
which is residing in package org.pack1.pack2
, that someFile.txt
should be present in the folder \\org\\pack1\\pack2\\
详细地讲,如果您的文件
someFile.txt
由位于org.pack1.pack2
包中的SomeClass
访问,则someFile.txt
应该位于文件夹\\org\\pack1\\pack2\\
Use of absolute paths to access a file inside an application is discouraged as it would dent the portability of that application. 不鼓励使用绝对路径访问应用程序内的文件,因为它会削弱该应用程序的可移植性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.