【发布时间】:2013-07-14 11:15:07
【问题描述】:
// 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);
}
这些行采用递归方法。第一次给出正确的路径:
D:/File Compression/Data/low_freq/low_freq/house_1/channel_2.dat
但在第二次调用时,它会将正斜杠翻转为反斜杠:
D:\File Compression\Data\low_freq\low_freq\house_1\channel_1.dat
如果我不使用条件,它可以正常工作。
if(path.contains("channel_"))
【问题讨论】:
-
您发布的代码没有出现这个问题。 “路径”从何而来? NB 从来没有必要在 Java 文件名中使用反斜杠。
-
@EJP 路径来自字符串路径是静态的,它在其他类中,所以我从类名中调用它并在此处使用该路径。这是代码
public void actionPerformed(ActionEvent arg0) { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "Data File", "dat"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(getParent()); if (returnVal == JFileChooser.APPROVE_OPTION) { path = chooser.getSelectedFile().getAbsolutePath(); } }