【问题标题】:file path from \ to /从 \ 到 / 的文件路径
【发布时间】: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" 为什么需要将其转换为不正确的路径版本?如果您控制使用该信息的代码,您应该修复该代码。如果您不控制使用它的代码,请提出错误报告。

标签: java path


【解决方案1】:

你真的应该使用系统属性file.separator

分隔文件路径组件的字符。这是“/” UNIX 和 Windows 上的“\”。

String fileSeparator = System.getProperty("file.separator");

您也可以将文件分隔符访问为File.separator

考虑打破您的路径,使用此属性代替正斜杠或反斜杠。

【讨论】:

    【解决方案2】:

    很简单,试试这个:

    String first = "C:\\Mine\\Java";
    String second = first.replace("\\", "/");
    second = second.replaceFirst("/", "//");
    System.out.println(second);
    

    输出:

    希望这可能会有所帮助。

    问候

    【讨论】:

      【解决方案3】:

      这应该可以:C:\Users use double \

      【讨论】:

        【解决方案4】:

        试试这个

        /**
         * Prepare dropbox path from the path.
         * 
         * @param path 
             *            that is to be formated.
         * @return 
             *        Return dropbox formated path.
         */
        public static String createDropboxPathFormat(String path) {
        
            // Replaced all \ with / of the path.
            String dropboxPath = path.replaceAll("[\\\\]", "/");
        
            // Finally replaced all // with /
            dropboxPath = dropboxPath.replaceAll("[//]", "/");
            return dropboxPath;
        }    
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-16
          • 2014-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-15
          • 1970-01-01
          • 2021-02-03
          相关资源
          最近更新 更多