1

I'm trying to create an instance of a file to parse html records from a property value. the problem is in the url of the file that I must put in the file properties, here is my example :

enter image description here

the correspondance code for reading file :

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();
            }
        }
    }

Thank you in advance, Best Regards.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
Omar Amaoun
  • 526
  • 1
  • 3
  • 20

1 Answers1

1

You are making a very common mistake for line -

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

Try with property value as ( by removing src ) - /testHtmlFile/test.html and so on. Don't change code.

UrlEnterer1=/testHtmlFile/test.html instead of preceding it with src.

prop.getProperty(path) should be as per your build path location for the file. Check your build directory as how these files are stored. These are not stored under src but directly under build directory.

This answer explains a little bit about path value for file reading from class path.

Also, as a side note ( not related to question ) , try not doing prop.getProperty(path) but directly injecting property value in your class using org.springframework.beans.factory.annotation.Value annotation.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • You need to paste a screen shot of your build directory showing paths for these files there i.e. directory where your build code is going . It might be something like **bin** or **build** .In your case, it might be - **UrlEnterer1=/main/java/testHtmlFile/test.html** – Sabir Khan Oct 22 '18 at 10:55
  • It's in the screen shot at the top, I only have a class to parse the html files. Thank you so much for your time – Omar Amaoun Oct 22 '18 at 11:14
  • There must be a build directory on your local - at root where this project is kept on disk. Try reading about build pah in IntelliJ. Also, try above path that I mentioned. – Sabir Khan Oct 22 '18 at 11:17