Reputation: 1459
I am developing a program in Java that associates with a file, so I useFile.separator
in order to manage it.
The weird fact is that everywhere I call it, it returns the \
as it should since I am working on Windows except in the last function I use it. There, it returns the ;
. I'll give you a piece of my code although I believe it won't help much. Is there anything else I should know to fix the problem?
System.out.println("File:" +source+"\n");
String filename= f.getName().substring(f.getName().lastIndexOf(File.pathSeparator)+1,f.getName().length());//here at printing I get /
System.out.println("Filename:" +filename+"\n");
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(output+ File.pathSeparator + filename);//here I get ;
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
Upvotes: 3
Views: 104
Reputation: 1501926
No, File.pathSeparator
is ;
on Windows. For example, you might have:
PATH=c:\Utils;c:\SomethingElse
File.pathSeparator
is ;
on Windows and :
on Unix.
You're thinking of File.separator
which would be \
on Windows and /
on Unix.
Here's a simple way of seeing that:
import java.io.File;
public class Test {
public static void main(String[] args) throws Exception {
System.out.println("File.separator is " + File.separator);
System.out.println("File.pathSeparator is " + File.pathSeparator);
}
}
(In your first sentence you even talk about File.separator
- but then you use File.pathSeparator
in the code.)
Note that if you use the methods within the File
class, you typically don't need to specify the separator at all - it's hard to tell exactly what you're trying to achieve, but I suspect something like:
File file = new File(f.getName());
File outputFile = new File(output, file.getName());
// Now use outputFile
... would work better and be cleaner.
Upvotes: 8