简体   繁体   English

Java方法无法提取文件

[英]Java Method Can't Pick Up Files

I'm writing a Java program that has a working drag and drop GUI for files. 我正在编写一个Java程序,该文件具有适用于文件的拖放GUI。 All of the files that are dragged in the DnD GUI are put into an String array that holds the file names. 在DnD GUI中拖动的所有文件都放入保存文件名的String数组中。 I have a method that loops through the array and strips the path to leave only the filenames and then sends the filename (for the Scanner) and the desired output filename (for the PrintWriter) to this method at the end of each loop: 我有一个遍历数组并剥离路径以仅保留文件名的方法,然后在每个循环结束时将文件名(用于Scanner)和所需的输出文件名(用于PrintWriter)发送给此方法:

public void fileGenerator(String in, String out) {          
    try {
    String current_directory = System.getProperty("user.dir");
    Scanner input = new Scanner(new FileReader(current_directory+"/"+in));
    PrintWriter output = new PrintWriter(current_directory+"/"+out);
        while(input.hasNext()) {
            String line = input.nextLine();
            output.println(line);
        } output.close(); 
    input.close();
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
}

The code is not working, it does not produce the output file. 该代码无法正常工作,它不会产生输出文件。 I am getting a "No such file or directory" error with the full path... I have tested it in terminal, it is the correct path. 我的完整路径出现“没有这样的文件或目录”错误...我已经在终端中对其进行了测试,它是正确的路径。 Any input is appreciated. 任何输入表示赞赏。

I should note that all of the Java source files, classes, and input files are in the same directory. 我应该注意,所有Java源文件,类和输入文件都在同一目录中。

Thanks! 谢谢!

First problem I see is that you ignore the exception, so you don't know if it opens the input file successfully. 我看到的第一个问题是您忽略了异常,因此您不知道它是否成功打开了输入文件。 Don't ignore exceptions, even if you don't know what to do with them, print them so you could analyze your problems later on. 不要忽略异常,即使您不知道如何处理它们,也要打印出来,以便以后可以分析问题。

Second, debug the code, see where it gets an exception, if at all, see what are the values at each step. 其次,调试代码,看它在哪里得到异常,如果有的话,看每一步的值。

Third, to answer your question, assuming you work with Eclipse, if you refer to the file with relative path, the working directory is not the source / class folder, but the project folder. 第三,假设您使用Eclipse,则回答您的问题,如果您引用具有相对路径的文件,则工作目录不是源/类文件夹,而是项目文件夹。

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

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