java.nio.charset.MalformedInputException:输入长度
从文件中读取单词的代码创建具有该名称的文件并写入内容,这与软件更改有关,因为直到最近它仍然工作正常。更改了代码,并且字符集仍然存在错误java.nio.charset.MalformedInputException:输入长度,java,Java,从文件中读取单词的代码创建具有该名称的文件并写入内容,这与软件更改有关,因为直到最近它仍然工作正常。更改了代码,并且字符集仍然存在错误 public class ForRwWr { public static void main(String[] args) { BufferedWriter bw = null; FileWriter fw = null; BufferedReader reader = new BufferedReade
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();
}
}
}
}
RWWR的公共类{
公共静态void main(字符串[]args){
BufferedWriter bw=null;
FileWriter fw=null;
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
试一试{
BufferedReader in=新的BufferedReader(新的java.io.FileReader(“F:\\words.txt”);
字符串str;
String fileName=“F:\\words.txt”;
List lines=Files.readAllLines(path.get(fileName)、Charset.defaultCharset());
用于(字符串广告:行){
String FILENAME=“F:\\”+Ad+“.html”;
试一试{
fw=新文件编写器(文件名);
bw=新的缓冲写入程序(fw);
bw.write(“轨道科学”);
bw.write(“卫星导航”);
bw.write(“卫星导航”);
bw.书写(“红岩”);
}捕获(IOD异常){
d、 printStackTrace();
}最后{
试一试{
如果(bw!=null)
bw.close();
如果(fw!=null)
fw.close();
}捕获(IOEX异常){
例如printStackTrace();
}
}
}
}捕获(IOD异常){
d、 printStackTrace();
}最后{
试一试{
如果(bw!=null)
bw.close();
如果(fw!=null)
fw.close();
}捕获(IOEX异常){
例如printStackTrace();
}
}
}
}
直到最近,Worsed还不错,但现在给出了错误
java.nio.charset.MalformedInputException:输入长度
更改了字符集,现在错误为
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)
java.io.FileNotFoundException:F:\Wonderful.html(文件名、目录名或卷标语法不正确)
位于java.io.FileOutputStream.open0(本机方法)
在java.io.FileOutputStream.open(FileOutputStream.java:270)
位于java.io.FileOutputStream。(FileOutputStream.java:213)
位于java.io.FileOutputStream。(FileOutputStream.java:101)
位于java.io.FileWriter(FileWriter.java:63)
位于html.ForRwWr.main(ForRwWr.java:36)
java.io.FileNotFoundException:F:\.html(文件名、目录名或卷标语法不正确)
位于java.io.FileOutputStream.open0(本机方法)
在java.io.FileOutputStream.open(FileOutputStream.java:270)
位于java.io.FileOutputStream。(FileOutputStream.java:213)
位于java.io.FileOutputStream。(FileOutputStream.java:101)
位于java.io.FileWriter(FileWriter.java:63)
位于html.ForRwWr.main(ForRwWr.java:36)
生成成功(总时间:2秒)
Wonder和Android是文件中的单词,直到最近worced fine它所做的是tooc用户输入,并用文件名将其写入文件,就像昨天worced使用的文件有趣的东西技术中的单词一样!我认为问题的根源在于不同的字符集,而不是
UTF-8
。确保读取字符集后,将其设置为打开
List<String> lines = Files.readAllLines(Paths.get(fileName), ---character set here---);
List lines=Files.readAllLines(path.get(fileName),--此处字符集---);
它可能是ISO-8859-1
或UTF-16
。
但是,对象(对象引用)中的读取器
和在代码中似乎没有用处
最后一点,try catch
es结尾处的代码完全是重复的 我认为问题源于不同的字符集,而不是UTF-8
。确保读取字符集后,将其设置为打开
List<String> lines = Files.readAllLines(Paths.get(fileName), ---character set here---);
List lines=Files.readAllLines(path.get(fileName),--此处字符集---);
它可能是ISO-8859-1
或UTF-16
。
但是,
对象(对象引用)中的读取器
和在代码中似乎没有用处
最后一点,try catch
es结尾处的代码完全是重复的 您可以阅读F:\words.txt
两次:一次是使用FileReader,这是一个旧类,只能使用默认的Charset.defaultCharset()
从当前平台读取。
对于本地文件,这是不可移植的。
然后使用文件。readAllLines
作为UTF-8
我的猜测是:
- 要么words.txt不在UTF-8中,然后非ASCII文本就会出现问题
您可以使用以下方法对此进行测试:
List<String> lines = Files.readAllLines(Paths.get(fileName),
Charset.defaultCharset());
List lines=Files.readAllLines(path.get(fileName)),
defaultCharset());
- 或者您将words.txt转换(粘贴到);在此之前是ASCII码,现在是特殊字符
但是,解决方案是将words.txt转换为UTF-8(因此所有语言和脚本中都可以有单词:比如façade和mañana。像NotePad++或JEdit这样的编辑器可以做到这一点。您可以从当前平台上阅读F:\words.txt
两次:一次是使用文件阅读器阅读一个旧类,只能使用默认的Charset.defaultCharset()
)。
对于本地文件,这是不可移植的。
然后使用文件。readAllLines
作为UTF-8
我的猜测是:
- 要么words.txt不在UTF-8中,然后非ASCII文本就会出现问题
您可以使用以下方法对此进行测试:
List<String> lines = Files.readAllLines(Paths.get(fileName),
Charset.defaultCharset());
List lines=Files.readAllLines(path.get(fileName)),
defaultCharset());
- 或者将words.txt转换(粘贴到);在此之前是ASCII,现在是特殊字符
但是,解决方案是将words.txt转换为UTF-8(因此