0

I'm using Windows7. I've written this simple java code:

package filetest;
import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File myfile = new File("C://test//test.txt");

        if (myfile.exists()) {
            System.out.println("file exists");
        } else {
            System.out.println("file doesn't exist");
        }

    }

}

The file DOES exists in C:/test/test.txt, but the answer is that file doesn't exists. Why?

EDITED: I've changed the code and it still doesn't find the file, but now it creates the file. So I can write to that directory. And the created file is named "test"

package filetest;
import java.io.File;
import java.util.*;

public class FileTest {
    public static void main(String[] args) {
        File myfile = new File("C:\\test\\test.txt");
        final Formatter newfile;
        if (myfile.exists()) {
            System.out.println("file exists");
        } else {
            System.out.println("file doesn't exist");
            try {
                newfile = new Formatter("C://test//test.txt");
                System.out.println("file has been created");
            } catch(Exception e) {
                System.out.println("Error: " + e);                
            }
        }

    }

}
11
  • 1
    // is not a valid directory separator on windows. \\ would work, since that'd just be a regular escaped backslash. Commented Aug 12, 2014 at 14:17
  • Thanks. I've tried C:\\test\\test.txt It doesn't work either. Commented Aug 12, 2014 at 14:18
  • 1
    Do you have enough permissions to access this file? Does the user that is running your application have permissions? Commented Aug 12, 2014 at 14:20
  • I'm working with an administrator account. I should have permission to access this file, shouldn't I? Commented Aug 12, 2014 at 14:25
  • 1
    You got it! Change your folder properties, show the extensions and rename your file removing ".txt" from it. Commented Aug 12, 2014 at 14:35

4 Answers 4

1

In windows path separator used is '\' for these you need to escape backslash.So your code will be something like:

public class FileTest {
    public static void main(String[] args) {
        File myfile = new File("C:\\test\\test.txt");

        if (myfile.exists()) {
            System.out.println("file exists");
        } else {
            System.out.println("file doesn't exist");
        }

    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to double your slashes. You have to user wether "/" or "\\".

EDIT :

The weird thing is that I tried it out and both "/" and "\\" work fine for me. In fact, it works regardless of the number of "/" I use... for example "C:////test/////////test.txt" is okay. You have another problem, and I have no idea of what it could be.

Comments

1

I would recommend using isFile() instead of exists(). Its a better way of checking if the path points to a file rather than if a file exists or not. exists() may return true if your path points to a directory.

Comments

0

@SSorensen In your EDITED code, you added the backslash properly

@ line 7

File myfile = new File("C:\\test\\test.txt");

but you forgot to update slashes with backslashes @ line 14

newfile = new Formatter("C://test//test.txt");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.