0

i m trying to code for sorting strings,taking input from text file.When i m trying to specify a file for this program is gives me FileNotFoundExcetion i m unable to understand why? even i tried to get file path by writing code for that,in the screenShoot u can see that path is correct but the program is still giving me ERROR

here is thet Screenshort https://app.box.com/s/qytu1d9xlm0vcb6atz42

here is my code

public static void main(String[] args) throws FileNotFoundException, IOException {
  ArrayList<String> row1 = new ArrayList<>();
  FileWriter writer;

  try {

     String filename = "1.txt";
     String finalfile = "";
     String workingDir = System.getProperty("user.dir");

     String your_os = System.getProperty("os.name").toLowerCase();
     if (your_os.indexOf("win") >= 0) {
        finalfile = workingDir + "\\" + filename;
     } else if (your_os.indexOf("nix") >= 0 || your_os.indexOf("nux") >= 0) {
        finalfile = workingDir + "/" + filename;
     } else {
        finalfile = workingDir + "{others}" + filename;
     }

     System.out.println("Final filepath : " + finalfile);
     File file = new File(finalfile);

     if (file.createNewFile()) {
        System.out.println("Done");
     } else {
        System.out.println("File already exists!");
     }

  } catch (IOException e) {
     e.printStackTrace();
  }

  try (BufferedReader reader = new BufferedReader(new FileReader("finalfile"))) {
     String s;
     while ((s = reader.readLine()) != null) {
        row1.add(s);
     }
     Collections.sort(row1);
     writer = new FileWriter("output.txt");
     for (String s1 : row1) {
        writer.write(s1 + "\n");
     }
     reader.close();
     writer.close();
  } catch (Exception e) {
     System.out.print("Error : " + e);
  }

}
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Are you kidding? You are trying to open "finalfile", not the file whose name is in variable `finalfile` – Ingo Jan 28 '14 at 15:36

1 Answers1

1

In

BufferedReader reader = new BufferedReader(new FileReader("finalfile"))) 

the parameter to the FileReader constructor is hard coded as "finalfile" - you need to use the variable instead:

BufferedReader reader = new BufferedReader(new FileReader(finalfile))) 
                                                         ^^^^^^^^^^^

You also need to move String finalfile = ""; before the first try block, otherwise it is out of scope when you are creating the FileReader.

Also, there is no need to query the operating system and manually set the directory path separator. If you really need to, use File.separator. Otherwise, simply use the forward slash - this is working cross-platform.

It is good to see that you are using try-with-resources - however, you should do it consequently; simply create all required resources in the try statement, and then there is no need to explicitly close them:

try (BufferedReader reader = new BufferedReader(new FileReader(finalfile));
     FileWriter writer = new FileWriter("output.txt")) {

   ...

   // reader and writer will be auto-closed
} catch (IOException e) {
   System.out.print("Error : " + e);
}
Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123