Question

the file is aa.txt in the directory /home/user the code I wrote is

input=new FileInputStream("//home//user//aa.txt");

but the program can not open the file. when I run it on windows, it works what is the format of the path in fedora to be read correctly by the program???

Was it helpful?

Solution

Since \ is used as an escape character (for instance \n = new line and \t = tab) we need to write \\ to mean a single \ when placing this character in a String.

This issue does not exist with a forward slash / For linux directories the forward slash / is used; windows uses the backslash. Writing OS independent code could be a pain, but it's not an issue. Just use the forward slash when dealing with files and Java automagically translates it for you to the correct OS specific format.

For instance C:/Users/Owner/Documents becomes C:\Users\Owner\Documents on windows. Or you could write "C:\\Users\\Owner\\Documents" but the simple forward slash format looks simpler.

OTHER TIPS

You don't have to escape the / (slash) character.

So you basically need this:

input=new FileInputStream("/home/user/aa.txt");

However, it is far wiser to use File.separator instead:

input=new FileInputStream(File.separator+"home"+File.separator+"user"+File.separator+"aa.txt");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top