我正在尝试用java读取一个文件。以下是代码。
String str = ".\\SomeFileName";
File file = new File(str);
InputStream is = new FileInputStream(file.getPath());在最后一行抛出一个FileNotFoundException。有人能帮上忙吗?
发布于 2011-07-26 20:26:06
您需要确定当前的工作目录。您可以使用以下命令来确定您当前的工作目录:
String curDir = System.getProperty("user.dir");发布于 2011-07-26 20:33:27
你可以用两种方式来指定一个文件;绝对的,例如
String fileName1 = "c:\temp\myfile.txt"; \\For Windows
String fileName2 = "/home/qwerky/myfile.txt"; \\For Linux或者是相对的,例如
String fileName3 = "myfile.txt";如果使用的是相对路径,则该路径是相对于java的当前工作目录的。你可以通过获取这个文件来找到它。以及打印绝对路径。
File cwd = new File(".");
System.out.println("Current working directory is " + cwd.getAbsolutePath());发布于 2011-07-26 20:36:19
尝试提供绝对path...Full目录路径。
File file = new File("C:\\abc.txt");然后再对此进行测试。str是具有文件名的字符串
File file = new File(str);
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);https://stackoverflow.com/questions/6829926
复制相似问题