Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用属性文件中的属性值创建文件实例_Java_Spring Boot - Fatal编程技术网

Java 使用属性文件中的属性值创建文件实例

Java 使用属性文件中的属性值创建文件实例,java,spring-boot,Java,Spring Boot,我试图创建一个文件实例,从属性值解析html记录。问题在于我必须将文件的url放入文件属性中,以下是我的示例: 读取文件的对应代码: public void extraxtElementWithoutId() { Map<String,List<List<Element>>> uniciteIds = new HashMap<String,List<List<Element>>>(); Fi

我试图创建一个文件实例,从属性值解析html记录。问题在于我必须将文件的url放入文件属性中,以下是我的示例:

读取文件的对应代码:

public void extraxtElementWithoutId() {
        Map<String,List<List<Element>>> uniciteIds = new HashMap<String,List<List<Element>>>();
        FileReader fileReader = null;
        Document doc = null;
        try {
            fileReader = new FileReader(new ClassPathResource(FILEPROPERTYNAME).getFile());
            Properties prop = new Properties();
            prop.load(fileReader);
            Enumeration<?> enumeration = prop.propertyNames();
            List<List<Element>> fiinalReturn = null;
            while (enumeration.hasMoreElements()) {
                String path = (String) enumeration.nextElement();
                System.out.println("Fichier en question : " + prop.getProperty(path));
                URL url = getClass().getResource(prop.getProperty(path));
                System.out.println(url.getPath());
                File inputFile = new File(url.getPath());
                doc = Jsoup.parse(inputFile, "UTF-8");
                //fiinalReturn = getListofElements(doc);
                //System.out.println(fiinalReturn);
                fiinalReturn = uniciteIds.put("Duplicat Id", getUnicityIds(doc));
                System.out.println(fiinalReturn);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                fileReader.close();
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
public void extraxtelementwithout id(){
Map uniciteIds=newhashmap();
FileReader FileReader=null;
单据单据=空;
试一试{
fileReader=newFileReader(newclassPathResource(FILEPROPERTYNAME).getFile());
Properties prop=新属性();
prop.load(文件阅读器);
枚举=prop.propertyNames();
List fiinalReturn=null;
while(枚举.hasMoreElements()){
字符串路径=(字符串)枚举.nextElement();
System.out.println(“Fichier en question:+prop.getProperty(path));
URL URL=getClass().getResource(prop.getProperty(path));
System.out.println(url.getPath());
File inputFile=新文件(url.getPath());
doc=Jsoup.parse(输入文件,“UTF-8”);
//fiinalReturn=getListofElements(文档);
//系统输出打印项次(fiinalReturn);
fiinalReturn=uniciteId.put(“复制Id”,getunicityId(doc));
系统输出打印项次(fiinalReturn);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
fileReader.close();
}捕获(例外e){
e、 printStackTrace();
}
}
}
提前谢谢大家,,
致以最诚挚的问候。

您犯了一个非常常见的错误

URL=getClass().getResource(prop.getProperty(path))

尝试将属性值设置为(通过删除src)-
/testHtmlFile/test.html
等等。不要更改代码

urlenter1=/testHtmlFile/test.html而不是在它前面加上src

prop.getProperty(path)
应该与文件的生成路径位置一致。检查生成目录,了解这些文件的存储方式。它们不是存储在src下,而是直接存储在build目录下

解释一些关于从类路径读取文件的路径值


另外,作为旁注(与问题无关),试着不要做
prop.getProperty(path)
,而是使用
org.springframework.beans.factory.annotation.value
annotation直接在类中注入属性值

您需要粘贴生成目录的屏幕截图,显示这些文件的路径,即生成代码所在的目录。它可能类似于bin或build。在您的例子中,它可能是-urlenter1=/main/java/testHtmlFile/test.htmlIt。在顶部的屏幕截图中,我只有一个类来解析html文件。非常感谢您的时间,在您的本地根目录上必须有一个构建目录,该项目保存在磁盘上。尝试阅读IntelliJ中的build pah。此外,请尝试我提到的上述路径。