【问题标题】:java.nio.charset.MalformedInputException: Input lengthjava.nio.charset.MalformedInputException:输入长度
【发布时间】:2018-08-15 12:12:57
【问题描述】:

从文件中读取单词的代码会创建具有该名称的文件并写入内容是否与 sw 更改有关,因为它直到最近都运行良好。更改的代码和字符集仍然是错误

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

        }
    }
}

直到最近还好,但现在出现错误

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)

Wonderful 和 Android 是文件中的单词,直到最近 worced

【问题讨论】:

标签: java


【解决方案1】:

您阅读了两次F:\words.txt:一次使用 FileReader - 一个旧类,只能从当前平台读取默认 Charset.defaultCharset()。 对于本地文件,这是不可移植的。 然后将Files.readAllLines 设为UTF-8。

我的猜测是:

  • 要么 word.txt 不是 UTF-8 格式的 ,然后你就会遇到非 ASCII 文本的问题。

您可以使用以下方法进行测试:

List<String> lines = Files.readAllLines(Paths.get(fileName),
                 Charset.defaultCharset());
  • 或者您已将 words.txt 转换(粘贴到);之前是 ASCII,现在是特殊字符。

但是解决方案是将 words.txt 转换为 UTF-8(因此所有语言和脚本都可以在其中包含单词:例如 façade 和 mañana。像 NotePad++JEdit 这样的编辑器 可能会这样做。

【讨论】:

  • Charset.defaultCharset() 使用的可移植性如何?例如在土耳其字符集计算机上与中文
  • @snr 我想表达Charset.defaultCharset()(或FileReader)是不可移植的方式,具体取决于应用程序的启动位置。但是,由于该应用程序较早运行,最有可能的是有人在文件中添加了单词,并且现在才使用特殊字符。
  • 是的,先生,由于阅读速度快,我省略了 这不是便携式的。此外,它不应该是解决方案,但它是捕捉解决方案的一种暗示。
  • 用不同的字符集更改了代码,现在错误是
【解决方案2】:

我认为问题源于不同于UTF-8 的不同字符集。确保读取字符集后,设置为

 List<String> lines = Files.readAllLines(Paths.get(fileName), ---character set here---);

可能是ISO-8859-1UTF-16。 但是,readerin objects(object references) 在您的代码中似乎没用。

最后一点,最后的try-catches 是完全重复的代码!

【讨论】:

    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2012-08-23
    • 1970-01-01
    相关资源
    最近更新 更多