简体   繁体   English

运行Java命令应用程序时无法从文件读取

[英]having trouble reading from file when running a java command app

i want to make a simple command line java app that gets as argument the name of a file located in the same directory with the jar file of the app, so that i can read from it. 我想制作一个简单的命令行Java应用程序,该应用程序将与该应用程序的jar文件位于同一目录中的文件的名称作为参数,以便我可以从中读取。 however i'm getting a FileNotFoundException. 但是我得到了FileNotFoundException。 here's my code: 这是我的代码:

public static void main(String[] args) {
    if (args.length == 0) {
        System.out.println("Give the name of the input file as argument.");
    } else if (args.length > 1) {
        System.out.println("Only one input file is allowed");
    } else {
        try {
            BufferedReader in = new BufferedReader(new FileReader(args[0]));
            System.out.println("File found");
        } catch (FileNotFoundException e) {
            System.out.println("Could not find file " + args[0]);
        }
    }
}

so suppose i'm having a txt file named a.txt and myApp.jar file inside the same directory. 因此,假设我在同一目录中有一个名为a.txt的txt文件和myApp.jar文件。 i start cmd and cd to this specific directory, and then type: 我将cmd和cd启动到此特定目录,然后键入:

java -jar myApp.jar a.txt

and i get "Could not find file a.txt" 我得到“找不到文件a.txt”

what am i doing wrong? 我究竟做错了什么?

EDIT: after thinking of it i guess the file is not found because it should be in the same directory with the class file, in other words inside the jar. 编辑:考虑之后,我猜找不到文件,因为它应该与类文件位于同一目录中,换句话说,在jar中。 so my question is, how is it possible to access it when it is outside of the jar file? 所以我的问题是,当它在jar文件之外时如何访问它?

The "current" directory is going to be determined by the environment when you launch your application. 启动应用程序时,“当前”目录将由环境确定。 To find out where it's looking, change the error output line to be: 要找出它在哪里,请将错误输出行更改为:

System.out.println("Could not find file " + (new File(args[0])).getCanonicalPath());

(you may have to add throws IOException to your main() declaration to get the above to compile). (您可能必须在main()声明中添加throws IOException才能编译以上内容)。

If you want to access resources inside the classpath (or jar), use ClassLoader.getResourceAsStream - you will then wrap a Reader around the returned InputStream using InputStreamReader . 如果要访问类路径(或jar)中的资源,请使用ClassLoader.getResourceAsStream-然后,将使用InputStreamReader将Reader包裹在返回的InputStream周围。

Check with this... (what you find) 检查一下...(找到的内容)

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.out.println("Give the name of the input file as argument.");
    } else if (args.length > 1) {
        System.out.println("Only one input file is allowed");
    } else {
        try {
            BufferedReader in = new BufferedReader(new FileReader(args[0]));
            System.out.println("File found");
        } catch (FileNotFoundException e) {
            // Finding the dir
            String current = new java.io.File( "." ).getCanonicalPath();
            System.out.println("Current dir: " + current);
            System.out.println("Current user.dir: " + System.getProperty("user.dir"));
            //
            System.out.println("Could not find file " + args[0]);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM