简体   繁体   English

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

[英]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.

相关问题 我们可以在Java jar文件中使用Windows bat文件吗,以便Java可以运行一些Windows命令,如果可以,我们应该将bat文件保留在jar文件的旁边 - Can we use windows bat file in Java jar file, so that java can run some windows command, if yes where we should keep bat file in side that jar file 我们什么时候应该在 Java 中使用/不使用初始化? - When should we use / not use initialization in Java? 什么时候应该在java中使用finalize()方法? - When should be we use finalize() method in java? 我们什么时候应该在 Java 8 中使用供应商? - When we should use Supplier in Java 8? 当我们在Mongo聚合中使用filePrefix进行分组时,如何投影fileName - How to project fileName when we group with filePrefix in the Mongo aggregation 我们什么时候应该在字符串文字上使用字符串的实习生方法 - When should we use intern method of String on String literals 使用文件名作为FileReader的输入 - Using the filename as input for FileReader 什么时候以及为什么要在Java / Android中使用事件类型 - When and why should we use Event type in Java/Android 我们什么时候应该使用Java的Thread over Executor? - When should we use Java's Thread over Executor? 在Java中,在处理多个接口时应使用泛型吗? - In Java should we use generics when dealing with multiple interfaces?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM