Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

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

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 in to database using load data local infile query.

Suppose my entered file path is "C:itle.csv" When I put this string in to query the you will see the combination in the path. This which is actually part of the file path and not the escape character ''. But the java and mysql consider it as escape character.

then I tried to replace '' in the file path string with "" using following code line.

String filepath="C:itle.csv";
String filepath2=filepath.replace("","");

Still there is not effect on the file path and it still consider the '' as escape character.

So my question is how to solve this problem without changing the name of the file?

If we have path like

String filepath="C:
ew folderitle.csv";

It will consider the and as escape character. how to solve this problem if the name of the file or folder in path cause for escape character?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...