Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exists from string

Tags:

java

This is the code I use when I try to read some specific text in a *.txt file:

public void readFromFile(String filename, JTable table) {

        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new FileReader(filename));
            String a,b,c,d;
            for(int i=0; i<3; i++)
            {
              a = bufferedReader.readLine(); 
              b = bufferedReader.readLine(); 
              c = bufferedReader.readLine(); 
              d = bufferedReader.readLine(); 
              table.setValueAt(a, i, 0);
              table.setValueAt(b, i, 1);
              table.setValueAt(c, i, 2);
              table.setValueAt(d, i, 3);
            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the reader
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

And it is called in this way:

readFromFile("C:/data/datafile.txt", table1)

The problem is the following: the 1st time I open the program the *.txt file I'm going to read does not exist, so I thought I could use the function exists(). I have no idea about what to do, but I tried this:

if(("C:/data/datafile.txt").exists()) {
readFromFile("C:/data/datafile.txt", table1)
}

It is not working because NetBeans gives me a lot of errors. How could I fix this?

like image 954
Alberto Rossi Avatar asked Nov 27 '22 00:11

Alberto Rossi


2 Answers

String has no method named exists() (and even if it did it would not do what you require), which will be the cause of the errors reported by the IDE.

Create an instance of File and invoke exists() on the File instance:

if (new File("C:/data/datafile.txt").exists())
{
}
like image 75
hmjd Avatar answered Jan 02 '23 07:01

hmjd


Note: This answer use classes that aren't available on a version less than Java 7.

The method exists() for the object String doesn't exist. See the String documentation for more information. If you want to check if a file exist base on a path you should use Path with Files to verify the existence of the file.

Path file = Paths.get("C:/data/datafile.txt");

if(Files.exists(file)){
   //your code here
}

Some tutorial about the Path class : Oracle tutorial
And a blog post about How to manipulate files in Java 7

Suggestion for your code:
I'll point to you the tutorial about try-with-resources as it could be useful to you. I also want to bring your attention on Files#readAllLines as it could help you reduce the code for the reading operation. Based on this method you could use a for-each loop to add all the lines of the file on your JTable.

like image 26
Marc-Andre Avatar answered Jan 02 '23 07:01

Marc-Andre