简体   繁体   中英

How to get File Path with double-backslashes in Java

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

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.

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

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 .

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM