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

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.

Leave a Comment