0

I'm writing a Java program that has a working drag and drop GUI for files. All of the files that are dragged in the DnD GUI are put into an String array that holds the file names. 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:

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.

Thanks!

user1062058
  • 285
  • 1
  • 3
  • 13

1 Answers1

1

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.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • I just edited the question -- there are no errors that happen I should have noted. I don't use Eclipse, I use Geany/Gedit so it's very straight forward... all of the files are in one place and the method that calls this generator strips the path out completely, leaving just the filename. Thank you for your comment... do you have anymore thoughts? – user1062058 Apr 30 '12 at 23:30
  • Take a look at this question: http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java – MByD Apr 30 '12 at 23:32
  • And also, by an empty catch block, you will probably miss any error thrown in the method. – MByD Apr 30 '12 at 23:33
  • Even with the files being in the same directory, do I still have to tell the method where the current directory is? Thanks very much for your input. – user1062058 Apr 30 '12 at 23:36
  • You welcome :) If you don't give absolute path, (e.g. starting with `/` in Unix or `C:\\` in windows) the path is relative to the current working directory, which is not where the source file is, but where the application is running from. The method I linked to in the first comment shows how to get the current directory. – MByD Apr 30 '12 at 23:39
  • Okay, I edited my question with an exception catch statement and the top line gets the current directory as you linked to. I added the absolute path to the scanner and printwriter but it still doesn't work. I System.out.println'ed the full file output and it was correct in terms of my Linux system... any thoughts on this? – user1062058 Apr 30 '12 at 23:43
  • 1. Try printing the line read from scanner. 2. Debug it to see if anything is read at all. – MByD Apr 30 '12 at 23:46
  • No luck on that because it prints out /opt/lampp/htdocs/all/test/myFile.txt (No such file or directory). I can't figure out how to point these correctly – user1062058 Apr 30 '12 at 23:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10722/discussion-between-binyamin-sharet-and-user1062058) – MByD Apr 30 '12 at 23:50
  • That you Binyamin for chatting with me. To everyone else, the problem is still there. – user1062058 May 01 '12 at 00:01
  • I am making a more specific question about the FileReader error here: http://stackoverflow.com/questions/10392435/java-filereader-not-finding-files Thanks! – user1062058 May 01 '12 at 01:19