How to escape the backslashes and the automatically generated escape character in file path in java

后端 未结 4 983
北海茫月
北海茫月 2020-11-28 13:49

I have very small and simple problem but I am not getting solutions on it. Actually I am getting a CSV file path using file chooser. I am entering the data in this csv file

相关标签:
4条回答
  • 2020-11-28 14:31

    You have to use escaping in the initial literal (filepath), for example:

    String filepath="C:\\title.csv"
    
    0 讨论(0)
  • 2020-11-28 14:33

    you need to use:

     String filepath2=filepath.replace("\\","\\\\");
    
    0 讨论(0)
  • 2020-11-28 14:36

    String filepath2=filepath.replace("\","\\") is not valid code - \ is a special character in string literals and needs to be escaped:

    String escapedFilepath = filepath.replace("\\","\\\\"); //double all backslashes
    
    0 讨论(0)
  • 2020-11-28 14:42

    Use a double slash in Java string literal to escape a slash :

    String s = "c:\\new folder\\title.csv";
    

    If an end user enters a string in a JFileChooser, the string variable will contain all the characters entered by the user. Escaping is only needed when using String literals in Java source code.

    And use a prepared statement to insert strings into a database table. This will properly escape special characters and avoid SQL injection attacks. Read more about prepared statements in the Java tutorial about JDBC.

    0 讨论(0)
提交回复
热议问题