java 文件路径转义,如何在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:\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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值