10

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:\title.csv" When I put this string in to query the you will see the \t combination in the path. This \t which is actually part of the file path and not the escape character '\t'. 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:\title.csv";
String filepath2=filepath.replace("\\","\\\\");

Still there is not effect on the file path and it still consider the '\t' 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:\new folder\title.csv";

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

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Param-Ganak
  • 5,787
  • 17
  • 50
  • 62
  • Bear in mind that if you want to use LOAD DATA INFILE the file you want to process has to be on the machine where the DB server is hosted, so you first have to upload the file to the DB server and after this has happened you shouldn't bother for windows paths (unless the server is hosted on a windows machine) – LordDoskias Sep 26 '11 at 10:25
  • @ James DW @ LordDoskias Hello friends! James I tried your solution but It gives compilation error when I put filepath.replace("\\","\\\"); LordDoskian My DB server and the file both on same machine and on windows OS. I just want to make the filepath="C:\new\title.csv" to "c:\\new\\title.csv". as replacing '\' with '\\' works with normal path line "C:\my\myfile.csv" but not with above filepath in which accidentally escape character comes with the combination of '\' and folder name or file name. – Param-Ganak Sep 26 '11 at 10:33
  • 1
    How do you actually notice that Java is considering the \n in your file path an escape character? – michael667 Sep 26 '11 at 10:40
  • @michael667 I take the path in a string variable and i.e. String filepath="C:\title.csv" and printed it on console then I tried to replace \ in that path wiht filepath.replace("\\","\\\\"); and again printed the string on console for both printing it has given me same output. Thank You. – Param-Ganak Sep 26 '11 at 10:44
  • Can you post a compileable piece of code to reproduce this behaviour? – michael667 Sep 26 '11 at 10:51
  • @Param, WTF this is now the third question here where you ask exactly the same thing. Can you please stop this? Everyone told you know that a String inside of a java source file is something completely DIFFERENT than a string comming from the outside into the program! If you want to have backslashed String Filenames inside of Java (WTF, why don't you use slashes instead??) `String filepath="C:\new folder\title.csv"; ` then the only thing you have to do is this: `String filepath="C:\\new folder\\title.csv";` – Angel O'Sphere Sep 26 '11 at 13:31

4 Answers4

14

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • @ James DW @ LordDoskias @ JB Nizet Hello friends! James I tried your solution but It gives compilation error when I put filepath.replace("\\","\\\"); LordDoskian My DB server and the file both on same machine and on windows OS. I just want to make the filepath="C:\new\title.csv" to "c:\\new\\title.csv". as replacing '\' with '\\' works with normal path line "C:\my\myfile.csv" but not with above filepath in which accidentally escape character comes with the combination of '\' and folder name or file name. – Param-Ganak Sep 26 '11 at 10:32
  • If a user enters some path in a JFileChooser, the returned file path will contain the backslash. You only need to escape the backslash in string literals in Java code. Show us how you test the contents of the file path. – JB Nizet Sep 26 '11 at 11:32
  • How should I escape `/` in this: `str.replace(/ ــ$/, '')`. *(I want to escape `/` in ``)* – stack Dec 17 '15 at 22:11
  • @stack ask your own question, with real code, instead of using a comment on a 4 years old answer. – JB Nizet Dec 17 '15 at 22:22
  • Ok [I asked](http://stackoverflow.com/questions/34345552/how-to-escape-slash), but I'm pretty sure people give me a lot of downvote ..! – stack Dec 17 '15 at 22:33
  • @stack do you realize this question was about Java, and your question is about JavaScript? Java is to JavaScript what a car is to a carpet: they're completely different languages. – JB Nizet Dec 17 '15 at 22:40
6

you need to use:

 String filepath2=filepath.replace("\\","\\\\");
Ankur
  • 12,676
  • 7
  • 37
  • 67
  • Hello Friend I tried that but no effect. The problem is as it is – Param-Ganak Sep 26 '11 at 10:34
  • 1
    maybe then you need to escape each escape sequence like `filepath.replace("\n", "\\n")` – Ankur Sep 26 '11 at 10:44
  • @ Ankur Mittal yes that is possible but I am searching for a more easy solution because in your solution we have to write the given line for each and every escape sequence character. Thank You! – Param-Ganak Sep 26 '11 at 10:48
  • 1
    @Param-Ganak you can use apache common library `StringEscapeUtils.escapeJava(str)` – Ankur Sep 26 '11 at 10:57
  • Hello Friend! is this library comes with java or we have to add it externally? It this library free? Thank You! – Param-Ganak Sep 26 '11 at 11:03
  • it doesn't come with java, it is apache library, and yes it is free but you need to add it externally – Ankur Sep 26 '11 at 11:06
  • The string where he is trying to do that does not contain any '\' so the replace "\\" for "\\\\" of course does not do anything. His string contains a "\t" and a "\n" char ;D ... because he is unable to understand what a String literal is and when he uses a JFileChooser and gets a String like "C:\this\is\a\nice\file" he believes he has to "fix" the "\t" and "\n" parts ... but 100 or more people have pointed out to him that his "believes" are wrong but he does not "believe" this either. IMHO we should close all his questions regarding this "\" problem. – Angel O'Sphere Sep 26 '11 at 13:35
4

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
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
0

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

String filepath="C:\\title.csv"
galoget
  • 722
  • 9
  • 15
michael667
  • 3,241
  • 24
  • 32