简体   繁体   English

JAVA如何在Project中引用文件

[英]JAVA how to refer the file in Project

I am create a simple Java Project in my VS Code, and here is the project structure.我在我的 VS Code 中创建了一个简单的 Java 项目,这里是项目结构。 在此处输入图像描述

I want to refer the wordcount.txt in my code, but it fail to find the file.我想在我的代码中引用wordcount.txt ,但找不到该文件。

Here is my test code:这是我的测试代码:

public class BatchJob {

public static void main(String[] args) throws Exception {
    // set up the batch execution environment
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    //URL url = BatchJob.class.getClassLoader().getResource("resources/wordcount.txt");
    DataSource<String> dataset = env.readTextFile("wordcount.txt");
    DataSet<Tuple2<String, Integer>> result = dataset.flatMap(new Tokenizer())
                                                    .filter(new FilterFunction<Tuple2<String, Integer>>(){
                                                        @Override
                                                        public boolean filter(Tuple2<String, Integer> arg0){
                                                            return arg0.f1 >0;
                                                        }
                                                    })
                                                    .groupBy(0)
                                                    .sum(1);
                                                    result.print();
    
}
public static class Tokenizer implements FlatMapFunction<String, Tuple2<String, Integer>>{
    @Override
    public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
        String[] tokens = value.toLowerCase().split(",");
        for (String token : tokens) {
            if ( !token.isEmpty() && token.length() > 0) {
                out.collect(new Tuple2<String, Integer>(token, 1));
            }
        }
    }
}

} }

Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now.应用程序资源在部署时将成为嵌入式资源,因此明智的做法是立即开始访问它们。 An must be accessed by URL rather than file . 必须由 URL 而不是 file 访问 See the info.查看信息。 page for embedded resource for how to form the URL.有关如何形成 URL的嵌入式资源页面

Thanks for your work, work with getResource .感谢您的工作,与getResource一起工作。 Here is the working code这是工作代码

URL url = BatchJob.class.getClassLoader().getResource("wordcount.txt"); DataSource<String> dataset = env.readTextFile( URLDecoder.decode(url.getFile(),"UTF-8") );

Unfortunately, this fix goes wrong at url.getFile() .不幸的是,这个修复在url.getFile()出错了。

Harking back to the bold part of the original advice.. ".. must be accessed by URL rather than file" : This is not a suggestion or merely a good programming practice, it is a requirement.回到原始建议的粗体部分.. “..必须由 URL 而不是文件访问” :这不是一个建议,也不是一个好的编程实践,而是一个要求。 The thing is, once the app.问题是,一旦应用程序。 is built, the resource will be inside a Jar and will not be a File any longer.构建完成后,资源将位于 Jar 内,不再File It will not be accessible as a File .它不能作为File访问 So while it might work when running it from the IDE (when the URL points to something that is a file), it will fail for the built Jar.因此,虽然从 IDE 运行它时它可能会起作用(当 URL 指向一个文件时),它对于构建的 ZACA268A40E82159FB16B5B74C674E15E 将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM