【问题标题】:Is there a standard way to transform a String into a File considerning the possibility of a URL/URI formatted input String考虑到 URL/URI 格式输入字符串的可能性,是否有标准方法将字符串转换为文件
【发布时间】:2020-01-13 20:19:54
【问题描述】:

我想获得最准确的File 类型表示的字符串,该字符串应该以以下几种形式之一引用本地(现有)文件:

String file0 = "/home/my_user/file.txt"

String file1 = "file:///home/my_user/file.txt"

String file2 = "file.txt"; // assuming that the working dir is /home/my_user.

是否有一个(准)使用标准库的单行程序或者像 apache-commons 这样的常见第三方可以解决问题?

谢谢。

【问题讨论】:

    标签: java string file url uri


    【解决方案1】:

    您可以为此目的定义自己的函数。下面给出函数定义和测试代码:

    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.URL;
    
    public class Main {
        public static void main(String[] args) {
            String file0 = "/Users/arvind.avinash/file.txt";
            String file1 = "file:///Users/arvind.avinash/file.txt";
            String file2 = "file.txt"; // assuming that the working dir is /Users/arvind.avinash.
    
            System.out.println(getFile(file0).exists());
            System.out.println(getFile(file1).exists());
            System.out.println(getFile(file2).exists());
        }
    
        static File getFile(String pathOrUri) {
            URI uri;
            File file = null;
            try {
                uri = new URL(pathOrUri).toURI();
            } catch (MalformedURLException e) {
                return new File(pathOrUri);
            } catch (URISyntaxException e) {
                return new File(pathOrUri);
            }
            if (uri != null) {
                file = new File(uri);
            }
            return file;
        }
    }
    

    输出:

    true
    true
    true
    

    [更新]

    下面是一个更简化的版本:

    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.URL;
    
    public class Main {
        public static void main(String[] args) {
            String file0 = "/Users/arvind.avinash/file.txt";
            String file1 = "file:///Users/arvind.avinash/file.txt";
            String file2 = "file.txt"; // assuming that the working dir is /Users/arvind.avinash.
    
            System.out.println(getFile(file0).exists());
            System.out.println(getFile(file1).exists());
            System.out.println(getFile(file2).exists());
        }
    
        static File getFile(String pathOrUri) {
            URI uri;
            try {
                uri = new URL(pathOrUri).toURI();
            } catch (MalformedURLException | URISyntaxException e) {
                return new File(pathOrUri);
            }
            return new File(uri);
        }
    }
    

    【讨论】:

    • 感谢代码。如果你有时间,我认为它可以简化一点。
    【解决方案2】:

    您可以在示例 1 和示例 3 中调用 new File(x),它应该可以工作。

    至于#2,您可以创建一个URI,然后从中创建File。事实上,我认为他们都可能会使用 URI 工作

    String fileStr = "file:///home/my_user/file.txt";
    try {
      URI uri = new URI(fileStr);
      File f = new File(uri);
    } catch (URISyntaxException ex) { ...}
    

    【讨论】:

    • 感谢您的回答。但是,不确定try {} 块试图完成什么......可能它甚至没有编译,或者编译。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2010-09-17
    • 2022-11-02
    • 2015-10-08
    • 2012-05-19
    • 2012-01-26
    相关资源
    最近更新 更多