Linux中的Java文件路径

Linux中的Java文件路径

本文介绍了Linux中的Java文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当愚蠢的问题,但我无法找到解决方案:

I have a rather silly question, but I haven't been able to find a solution for this:

当我尝试读取文件时得到文件未找到错误是运行时。它编译了文件。

我在Linux上,所以我使用的语句如下:

I am on Linux, so I use the statement something like:

Scanner s = new Scanner(new File("home/me/java/ex.txt"));

它给了我一个运行时错误:

and it gives me a runtime rror:

/home/me/javaException in thread "main" java.io.FileNotFoundException: home/me/java/ex.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:137)
at java.util.Scanner.<init>(Scanner.java:653)
at test.main(test.java:14)

我试过改变文件名中的每一个可能的东西,但似乎没有任何作用。

I tried changing every possible thing along the lines of filenames, but nothing seems to work.

有关为什么会发生这种情况的任何线索?默认情况下java在哪里查找文件?

Any clues as to why this is happening? where does java look for files by default?

推荐答案

看起来你缺少一个前导斜杠。也许试试:

Looks like you are missing a leading slash. Perhaps try:

Scanner s = new Scanner(new File("/home/me/java/ex.txt"));

(至于默认情况下它查找文件的位置,它是JVM运行的相对位置路径就像你问题中的路径一样)

(as to where it looks for files by default, it is where the JVM is run from for relative paths like the one you have in your question)

这篇关于Linux中的Java文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:14