文件路径问题:/-> \ [英] File path issue with: / -> \
问题描述
// Dividend Limit check or increase the Dividend
if (dival == 10) {
writer.println("Divident has reached it Limit !");
i++;
// update the file name
String upath = "channel_" + i;
System.out.println(path);
// find channel_1 and replace with the updated path
if (path.contains("channel_1")) {
path = "D:/File Compression/Data/low_freq/low_freq/house_1/"
+ upath + ".dat";
} else {
JOptionPane.showMessageDialog(null, "Invalid File Choosen");
System.exit(0);
}
dival = 10;
} else {
dival = dival + 10;
writer.println("Dividen:" + dival);
}
这些行是递归方法.第一次给出正确的路径:
these lines are in a recursive method. first time it gives right path:
D:/File Compression/Data/low_freq/low_freq/house_1/channel_2.dat
但是在第二次调用时,它将正斜杠翻转为反斜杠:
But on the second call it flips the forward slash to back slash:
D:\File Compression\Data\low_freq\low_freq\house_1\channel_1.dat
如果我不使用该条件,它将很好地工作.
it works fine if I do not use the condition.
if(path.contains("channel_"))
推荐答案
\ 在Java中被称为转义序列,其用途广泛.
\ is called as Escape sequence in java which is used in various purposes .
在您的情况下,请使用File.separator
In your case use File.separator
String path = "D:"+File.separator+"File Compression"+File.separator+"Data"+File.separator+"low_freq"+File.separator+"low_freq"+File.separator+"house_1"+File.separator;
使用双斜杠 \\ !这是一种特殊的逃生模式.像\ n或\ r.
转义序列,通常在Windows的文本文件中使用,特别是在记事本中.
Use double slash \\ ! It's a special escape pattern. Like \n or \r.
Escape sequence normally used in text files in Windows, specially in notepad.
下面列出了主要的Java转义序列.它们用于表示非图形字符以及诸如双引号,单引号和反斜杠之类的字符.如果要在字符串文字中表示双引号,则可以用\表示.如果要在字符文字中表示单引号,则可以用\'表示.
The primary Java escape sequences are listed below. They are used to represent non-graphical characters and also characters such as double quotes, single quotes, and backslashes. If you'd like to represent a double quote within a String literal, you can do so with \". If you'd like to represent a single quote within a character literal, you can do so with \'.
这篇关于文件路径问题:/-> \的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!