this is my code
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();
}
}
}
idea of project is that i am putting text from file last.txt and from first.txt to all files in my directory,
i will show you an example of data
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)
so i am trying to get those files by
firstContent = new String(Files.readAllBytes(Paths.get("first.txt")), StandardCharsets.UTF_8);
lastContent = new String(Files.readAllBytes(Paths.get("last.txt")), StandardCharsets.UTF_8);
I've tried some ways like Charsets.UTF_8 , "UTF-8" but still it doesn't work for me, i still getting error "java.nio.charset.MalformedInputException: Input length = 1"