首页 > 解决方案 > 错误“java.nio.charset.MalformedInputException:输入长度 = 1”Java

问题描述

这是我的代码

public class Main {

    public static void main(String[] args) throws IOException {
        // define the directory that contains the text files
        String dir = "C:\\Users\\German V\\Desktop\\insertIntoTemplate\\txtfiles";
        Path dirPath = Paths.get(dir);
        // predefine some lines to be appended to every file
        List<String> linesToBeAppendedBottom = new ArrayList<>();
        //String last = "\npkg_import.finalize(req,res);" + "\nEND;";
        String lastContent = new String(Files.readAllBytes(Paths.get("last.txt")), StandardCharsets.UTF_8);
        linesToBeAppendedBottom.add("\n" + lastContent);

        try {
            // go through all files in the directory (tested with .txt files only)
            Files.list(dirPath)
                    // filter only files
                    .filter(Files::isRegularFile)
                    .forEach(filePath -> {

                        List<String> linesToBeAppendedTop = new ArrayList<>();
                        // add the first line as predefined first line
                        String firstContent = null;
                        try {
                            firstContent = new String(Files.readAllBytes(Paths.get("first.txt")), StandardCharsets.UTF_8);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        linesToBeAppendedTop.add(firstContent + "\n");

                        try {
                            linesToBeAppendedTop.addAll(Files.readAllLines(filePath));
                            // append the extended text to the file (again),
                            // but this time overwrite the content
                            Files.write(filePath, linesToBeAppendedTop, StandardOpenOption.TRUNCATE_EXISTING);
                            Files.write(filePath, linesToBeAppendedBottom, StandardOpenOption.APPEND); //APPEND
                        } catch (IOException e) {
                            System.err.println("Could not append text to file "
                                    + filePath.toAbsolutePath().toString());
                            e.printStackTrace();
                        }
                    });
        } catch (IOException e) {
            System.err.println("Could not list files in "
                    + dirPath.toAbsolutePath().toString());
            e.printStackTrace();
        }
    }
}

项目的想法是我将文件 last.txt 和 first.txt 中的文本放到我目录中的所有文件中,

我将向您展示一个数据示例

ret:=pkg_check.import_street(      req_id=>req,       ext_system=>1,       external_id=>33000071128,      country_iso=>'UA',      region=>5000015,      district=>1008302816,      city=>33000000094,      street_type=>3324,      name_ua=>'Коцкого',      name_en=>'Koskoho',      name_ru=>NULL,      datestart=>TO_DATE('02.01.2019', 'dd.mm.yyyy'),      datestop=>TO_DATE('31.12.9999', 'dd.mm.yyyy'),      oldname_ua=>NULL,      oldname_en=>NULL,      oldname_ru=>NULL)   
ret:=pkg_check.import_street(      req_id=>req,       ext_system=>1,       external_id=>33000001171133,      country_iso=>'RU',      region=>500015,      district=>1002400816,      city=>33000077394,      street_type=>335,      name_ua=>'Собa',      name_en=>'Soba',      name_ru=>NULL,      datestart=>TO_DATE('02.01.2019', 'dd.mm.yyyy'),      datestop=>TO_DATE('31.12.9999', 'dd.mm.yyyy'),      oldname_ua=>NULL,      oldname_en=>NULL,      oldname_ru=>NULL)   
ret:=pkg_check.import_street(      req_id=>req,       ext_system=>1,       external_id=>33000001138,      country_iso=>'UA',      region=>5000015,      district=>1002400816,      city=>33000077394,      street_type=>3324,      name_ua=>'Вомьм',      name_en=>'Vomm',      name_ru=>NULL,      datestart=>TO_DATE('22.07.2013', 'dd.mm.yyyy'),      datestop=>TO_DATE('31.12.9999', 'dd.mm.yyyy'),      oldname_ua=>NULL,      oldname_en=>NULL,      oldname_ru=>NULL)   

所以我试图通过

firstContent = new String(Files.readAllBytes(Paths.get("first.txt")), StandardCharsets.UTF_8);
lastContent = new String(Files.readAllBytes(Paths.get("last.txt")), StandardCharsets.UTF_8);

我尝试了一些方法,例如 Charsets.UTF_8 , "UTF-8" 但它仍然对我不起作用,我仍然收到错误 "java.nio.charset.MalformedInputException: Input length = 1"

标签: javafiletextencoding

解决方案


推荐阅读