简体   繁体   中英

Java PrintWriter exception

I am trying to parse some links and then store the information in text files, I have all the links that should be parsed in a list, but after parsing and storing information about 100 links I got error which I really could not understand why it occurs, here is my code:

for(String link : links){
            Document doc = Jsoup.connect(link).get();
            Element e1 = doc.select("h1").first();
            String authorName = e1.ownText();
            String fileName  = authorName.replaceAll("\\s+","");
            PrintWriter writer = new PrintWriter("/home/taner/Test/"+fileName+".txt", "UTF-8");
            String description = doc.getElementsByClass("article__content").text();
            writer.write(description);
            writer.close();
        }

and thats the error I get:

Exception in thread "main" java.io.FileNotFoundException: /home/taner/Test/MarcusSchmidt/JohannaDrott.txt (No such file or directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at java.io.PrintWriter.<init>(PrintWriter.java:192)
    at java.io.PrintWriter.<init>(PrintWriter.java:232)
    at Test1.main(Test1.java:253)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

line 253 is actually the PrintWriter writer = new PrintWriter("/home/taner/Test/"+fileName+".txt", "UTF-8"); line

You are translating authorName into a file name without any quoting. What happens when the authorName contains a Slash? => You have an additional directory in your structure.

Note that Java accepts Slash and Backslash as Directory Separator, so you have to replace both.

That's basically what happens in your exception: authorName = "MarcusSchmidt/JohannaDrott", which leads to a new sub-directory "MarcusSchmidt", which does not exist.

Another problem is if two pages have the same authorName, then the file will be overwritten.

In general you should be careful when translating values from an uncontrolled source, such as the Internet, into system resources, as this opens your code to vulnerabilities.

You need to create the file (and directories) first:

final File file = new File("/home/taner/Test/" + fileName + ".txt");
file.mkdirs();
file.createNewFile();

Check if you have this Directory Structure: /home/taner/Test/MarcusSchmidt/ . Seems like your fileName string is MarcusSchmidt/JohannaDrott.txt and not just JohannaDrott.txt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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