java - 错误“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"
解决方案
推荐阅读
- ios - dyld:未加载库:@rpath/GoogleInteractiveMediaAds.framework/GoogleInteractiveMediaAds 自定义框架集成中的错误
- linux - 如何使用 Bash 收缩/收缩一组值
- python - 如何将 django 1.11 移植到 django 2.2 和 python 2.7 到 3.5?
- powerbi - 总和基于值是否在不同的表中可用
- amazon-web-services - 如何使用氡的 JSON 结果在 Athena 中创建表
- linux-kernel - Linux设备树:如何解析包含单元列表的属性
- laravel - 如何更改现有字段类型表的属性
- google-analytics - 我想设置一个目标,以便漏斗也显示用户总数
- webhooks - Integromat - 保存以前参加的人并将新人添加到列表中
- eclipse - 如何将 Apache POI 正确添加到 JSP 以在 Eclipse 中执行 XWPFDocument?