0

I have a program where I save school grades in a .txt File. I want to let the user choose where this File should be saved. It works with the JFileChooser find but Java have a problem with the FilePath. The filepath from the JFileChooser looks like this: C:\Users...\Documents\n.txt But if I want to read the TextFile in the Program Java says that it couldn't find the Filepath. It should look like this: C:\Users\...\Documents\n.txt

How can I get the Path with double-backslashes?

public void actionPerformed(ActionEvent e) {

            JFileChooser jf = new JFileChooser();
            jf.showSaveDialog(null);
            String fPath = jf.getSelectedFile().getPath();
            fPath.replaceAll('\', '\\');

            System.out.println(p);

        }

that does not work it says invalid character constant

4 Answers4

2

There are some places where the backslash serves as escape character, and must be escaped, to be simply the backslash of a Windows path separator.

These places are inside .properties files, java String literals and some more.

You could for Windows paths alternatively use a slash (POSIX compliance of Windows).

        fPath = fPath.replace('\\', '/');

Backslash:

        fPath = fPath.replace("\\", "\\\\");

The explanation is that a single backslash inside char and string literals must be escaped: two backslashes represent a single backslash.

With regular expressions (replaceAll) a backlash is used as command: a digit is expressed as \d and as java String: "\\d". Hence the backslash itself becomes (behold):

        fPath = fPath.replaceAll("\\\\", "\\\\\\\\"); // PLEASE NOT

I almost did not see it, but methods on String do not alter it, but return a new value, so one needs to assign the result.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

When using hard coded file names in Java you should always use forward slashes / as file separators. Java knows how to handle them on Windows.

Also you should not use absolute paths. You don't know if that paths will exist at the target system. You should use either relative paths starting with your classpath as root "/..." or get some system dependen places from System.getProperty() https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getProperties--

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
1

Multiple issues in your code:

public void actionPerformed(ActionEvent e) {

        JFileChooser jf = new JFileChooser();
        jf.showSaveDialog(null);
        String fPath = jf.getSelectedFile().getPath();
        // fPath is a proper file path. This can be used directly with
        // new File(fPath). The contents will contain single \ character
        // as Path separator
        fPath.replaceAll('\', '\\');
        // I guess you are trying to replace a single \ character with \\
        // character. You need to escape the \ character. You need to
        // consider that both parameters are regexes.
        // doing it is:
        // fPath.replaceAll("\\\\", "\\\\\\\\");
        // And then you need to capture the return value. Strings are 
        // immutable in java. So it is:
        // fPath = fPath.replaceAll("\\\\", "\\\\\\\\");
        System.out.println(p);
        // I don't know what p is. I guess you want to use fPath
    }

That said, I do not understand why you want to convert the path returned by JFileChooser.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
0

You don't need the file path with double backslashes in Java. Double backslashes are for:

  1. The Java compiler, inside string literals.
  2. The Java regex compiler.

Everywhere else you can obtain backslashes, or use forward slashes.

Possibly you are looking for java.util.Properties?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I mean that the filepath must be set by the user because the program shouldn't only work on one PC –  Alarm für Cobra 11 Feb 06 '18 at 09:18
  • 1
    I have no idea what this means. You are neither the Java compiler nor the regular expression compiler: you therefore have no identifiable nor identified need for double backslashes; and if you do, `java.util.Properties` will provide them to you. – user207421 Feb 06 '18 at 09:24