【发布时间】:2012-01-23 18:10:36
【问题描述】:
我有这个问题:我正在从 JFileChooser 中选择一个文件,如果我进行系统打印,我会得到这个路径:C:\Users\Joakim\Desktop\dude.txt,当我想使用这个链接来复制这个时文件到另一个位置我需要有这样的路径: C://Users/Joakim/Desktop/dude.txt 我该怎么做?
public void upload(String username) throws RemoteException, NullPointerException{
JFileChooser chooser = new JFileChooser(getProperty + "/desktop/");
int returnVal = chooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
} try {
String fileName = chooser.getSelectedFile().getName();
System.out.println(fileName); //name of the file
File selectedFile = chooser.getSelectedFile();
System.out.println(selectedFile); //path of the file
//File path= selectedFile.replaceAll('/','/');
String serverDirectory = ("C://Users/Joakim/Dropbox/Project RMI/SERVER/");
byte[] filedata = cf.downloadFile(selectedFile);
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(serverDirectory + fileName));
output.write(filedata, 0, filedata.length);
output.flush();
output.close();
} catch (Exception e) {
System.err.println("FileServer exception: " + e.getMessage());
e.printStackTrace();
}
}
在此先感谢 :)
编辑:所以这并没有像我计划的那样成功。我想将路径更改为 C://Users/Joakim/Desktop/dude.txt 但这还不够。我需要有 //C://Users/Joakim/Desktop/dude.txt。我现在的问题是得到它并仍然将它用作文件。我确实测试了
File newFil = new File("//" + selectedFile);
byte[] filedata = cf.downloadFile(nyFil);
这对我不起作用。我还是出去 C://Users/Joakim/Desktop/dude.txt 有人有一个或两个小费吗? :)
【问题讨论】:
-
如果你真的需要,为什么不简单地替换字符串中的
` through/`? -
注释掉的行?取消注释,并将第一个参数替换为
'\\'。并将/的第一个实例替换为两个'em。 -
"C:\Users\Joakim\Desktop\dude.txt 当我想使用此链接将此文件复制到另一个位置时,我需要具有如下路径:C: //Users/Joakim/Desktop/dude.txt" 为什么需要将其转换为不正确的路径版本?如果您控制使用该信息的代码,您应该修复该代码。如果您不控制使用它的代码,请提出错误报告。