javanetbeansfedorafile-ionetbeans-7.2

How to read input from a file in a java project using netbeans 7.2 on fedora?


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???


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.