PDA

View Full Version : File Input in Java




thefil
Jul 26, 2007, 10:31 AM
I'm coming from a Windows environment and I have a bit of a file input problem...

glass = new Scanner( new File("\Users\filipkrynicki\Mydia 4.0\build\classes\mydia40\db.m4"));

returns an error "illegal escape character"

*edit* I fixed that problem by doubling the \\s, but now it can't find the file. Am I doing something wrong?



itickings
Jul 26, 2007, 11:01 AM
My guess would be the use of \ instead of /.

To avoid this kind of problem, consider using File.separator (http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#separatorChar) instead of assuming a specific separator.

Eraserhead
Jul 26, 2007, 01:10 PM
My guess would be the use of \ instead of /.

To avoid this kind of problem, consider using File.separator (http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#separatorChar) instead of assuming a specific separator.

Totally correct \ is only the Windows separator, / is used by OS X/Unix/Linux. To run it on both the System Property File.separator is the way to go.

ChrisBrightwell
Aug 1, 2007, 11:41 PM
Another vote here for File.separator. :)