Background
For a program I'm writing I need to be able to read Windows filenames from a file. Unfortunately, Windows use \ instead of /, which makes this tricky. I've been trying different ways, but it never seems to work. Here's the code
try{
BufferedReader reader = new BufferedReader(new FileReader(cfgFilePath));
try{
String line;
while(line = reader.readLine()!=null){
if(line.indexof("LocalFile") != -1){
this.localFile = line.substring(line.indexOf(" ")+1);
System.out.println("Value: " + this.localFile);
}
}
}
}catch(IOException ee){
System.err.println(ee.getMessage());
}
If I supply the filepath C:, so that the config file says LocalFile C:, it works fine and prints Value: C: to the console. C:\ seems to work fine too; Value: C:\ is printed. But when I try C:\Users it doesn't work. It prints C:\Users, but not with my println call. I tried adding backslashes, so the path is C:\\Users and it still doesn't work. When debugging, it seems the application returns from the function in which this is ran when hitting the while-loop to read the line containing the file path.
Question
I suppose it has to do with the fact that \U is not a valid character and I don't want the user to be forced to write the file path in a special format just because my program can't handle it. So how can I fix this?
Aucun commentaire:
Enregistrer un commentaire