java - 在路径中使用非法字符的 Windows 中使用 Java 访问文件

标签 java windows path

我正在使用 Windows 机器和 Java。我只是想备份一个文件,但我遇到了路径中的非法字符(“#”)的问题。我真的试过了,但我被困住了。我重写了它,尝试了我能找到或想到的所有变体。任何帮助将不胜感激。

public class SyncActionMachine {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException, URISyntaxException {
    String MSI_one, MSI_two, dropBox;
    GetDate getDate = new GetDate();

    MSI_one = "C:\\Users\\Brian\\AppData\\Roaming\\Macromedia\\Flash Player\\#SharedObjects\\Q2965ZS7\\localhost\\ActionMachine.sol";
    MSI_two = "C:\\Users\\Brian\\Desktop\\test.txt";
    dropBox = "C:\\Users\\Brian\\Dropbox\\Action Machine History\\ActionMachine.sol";

    File source = new File(MSI_one);
    File destination = new File(dropBox);

    // Attempt #1 using string with special characters  
    try {
        Files.copy(source.toPath(), destination.toPath());
    } catch (IOException iOException) {
        System.out.println("Didn't work: " + iOException);
    }

    // Attempt #2 using URI - not really sure how to use it.
    URI uri;
    uri = new URI("file:///C:/Users/Brian/AppDate/Roaming/Macromedia/Flash%20Player/%23SharedObjects/Q2965ZS7/localhost/ActionMachine.sol");
    Path uriSelfMadePath = Paths.get(uri);
    try {
        Files.copy(uriSelfMadePath, destination.toPath());
    } catch (IOException iOException) {
        System.out.println("Didn't work: " + iOException);
    }

    // Attempt #3 Suggestion from Aurasphere. Thanks again for quick response.
    // Not sure what I'm suppose to do with the URL
    String thePath = MSI_one;
    thePath = URLEncoder.encode(thePath, "UTF-8");
    Path aurasphereThePath = Paths.get(thePath);
    try {
        Files.copy(aurasphereThePath, destination.toPath());
    } catch (IOException iOException) {
        System.out.println("Didn't work: " + iOException);
    }

    // Attempt #4 build path using Patha and passing in augruments separately
    Path pathOneByOne = Paths.get("C:", "Users", "Brian", "AppDate", "Roaming", "Macromedia", "Flash Player",
            "#SharedObjects", "Q2965ZS7", "localhost", "ActionMachine.sol");
    try {
        Files.copy(pathOneByOne, destination.toPath());
    } catch (IOException iOException) {
        System.out.println("Didn't work: " + iOException);
    }

    // Seeing what all these path's look like
    URL fileUrl = source.toURI().toURL();
    URI fileUri = source.toURI();
    System.out.println("------------Path Print out------------------");
    System.out.println("URLEncoder : " + thePath);
    Path from = Paths.get(fileUri);
    System.out.println("URL : " + fileUrl);
    System.out.println("URI : " + fileUri);
    System.out.println("source: " + source);

}

}

感谢您的任何建议。

最佳答案

只需使用 URLEncode:

String thePath = "your_path";
thePath = URLEncoder.encode(thePath, "UTF-8"); 

关于java - 在路径中使用非法字符的 Windows 中使用 Java 访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041670/

相关文章:

java - 长时间关机时如何处理servlet请求

java - 打印方阵并使用 Java 对其进行转置

java - 比较两个非常大的列表的最佳方法

windows - 如何安装Microsoft Windows Terminal

Java 启动但返回退出代码 13

xcode - 从 bash 导入 $PATH 到 Xcode

excel - 防止 Excel 将 UDF 路径保存到公式单元格

java - 刷新jsp页面

c - 为什么我们需要取消映射才能使PE文件有效?

c++ - (C/C++) Windows 7 中的 EOF