Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an issue with this line of code :

Map<String, Long> wordCounts = Files.lines(Paths.get(fileSource))
                .flatMap(line -> pattern.splitAsStream(line))
                .collect(Collectors.groupingBy(String::toLowerCase,
                    TreeMap::new, Collectors.counting()));

If I run from Intellij it works fine.
If I package my application (Maven) and run it with :

java -jar myapp.war

I am getting this error :

Caused by: java.nio.charset.MalformedInputException: Input length = 1

I don't know why.

[UPDATE] This way, the file is being created.

Path path = Paths.get(newTxtName);
try {
    BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
    writer.append(parsedDocx);
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}

But there is something wrong with this line when I execute the war file on a different Server(windows 10 family) (not my pc) :

 Map<String, Long> wordCounts = Files.lines(Paths.get(fileSource), Charset.forName("UTF-8"))

I get a

java.nio.file.AccessDeniedException: <fileName>

It works fine on my computer (windows 10 family) though, with the same war.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
224 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you know the character encoding of the file you are reading, then use method lines(Path, Charset) in class java.nio.file.Files. From your comment it appears that the file encoding is UTF-8. So the code in your question should be:

Map<String, Long> wordCounts = Files.lines(Paths.get(fileSource),
                                           Charset.forName("UTF-8"))
                                    .flatMap(line -> pattern.splitAsStream(line))
                                    .collect(Collectors.groupingBy(String::toLowerCase,
                                             TreeMap::new, Collectors.counting()));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...