Reputation: 33
Code that reads word from file creates file with that name and writes content is it something to do with sw changes cause it worked fine till recently. Changed code and also the charset still the error
public class ForRwWr {
public static void main(String[] args) {
BufferedWriter bw = null;
FileWriter fw = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
BufferedReader in = new BufferedReader(new java.io.FileReader("F:\\words.txt"));
String str;
String fileName = "F:\\words.txt";
List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());
for (String Ad : lines) {
String FILENAME = "F:\\" + Ad + ".html";
try {
fw = new FileWriter(FILENAME);
bw = new BufferedWriter(fw);
bw.write("Orbital Science");
bw.write("Satellite Navigation");
bw.write("Satellite Navigation");
bw.write("Hongyan");
} catch (IOException d) {
d.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
} catch (IOException d) {
d.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
worced fine till recently but now gives the error
java.nio.charset.MalformedInputException: Input length
changed the charset and now the error is
java.io.FileNotFoundException: F:\Wonderful .html (The filename, directory name, or volume label syntax is incorrect)
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:101)
at java.io.FileWriter.<init>(FileWriter.java:63)
at html.ForRwWr.main(ForRwWr.java:36)
java.io.FileNotFoundException: F:\ .html (The filename, directory name, or volume label syntax is incorrect)
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:101)
at java.io.FileWriter.<init>(FileWriter.java:63)
at html.ForRwWr.main(ForRwWr.java:36)
BUILD SUCCESSFUL (total time: 2 seconds)
Wonderful and Android are the words from file till recently worced fine what it did is tooc user input and wrote it to files with the file name as the word from file funny thing technology what worced yesterday !does worc today or has bein haced
Upvotes: 0
Views: 17875
Reputation: 109547
You read F:\words.txt
twice: once with a FileReader - an old class that can only read with the default Charset.defaultCharset()
, from the current platform.
This is not portable, for local files.
And then with the Files.readAllLines
as UTF-8.
My guess is:
You can test this with:
List<String> lines = Files.readAllLines(Paths.get(fileName),
Charset.defaultCharset());
However the solution would be to convert words.txt to UTF-8 (so all languages and scripts can have words in it: like façade and mañana. An editor like NotePad++ or JEdit might do this.
Upvotes: 3
Reputation: 20891
I think the issue stems from different character set which is other than UTF-8
. After ensuring the read character set, set it on
List<String> lines = Files.readAllLines(Paths.get(fileName), ---character set here---);
It may be ISO-8859-1
or UTF-16
.
However, reader
and in
objects(object references) seem useless in your code.
And a last point, try-catch
es at the end are exactly code duplicate!
Upvotes: 2