Решение, может, не самое лаконичное, но тем не менее - рабочее. Подскажите, где не вижу ошибку или какую проверку еще сделать?
package com.javarush.task.task18.task1827;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Прайсы
*/
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String fileName = read.readLine();
read.close();
if (args.length > 0 && args[0].equals("-c")){
//считываю построчно всё из файла
Scanner sc = new Scanner(new File(fileName));
List<String> lines = new ArrayList<>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
sc.close();
//вытаскиваю айдишки
String[] arr = lines.toArray(new String[0]);
String[] arr2 = new String[arr.length];
for (int a = 0; a < arr.length; a++){
String id = arr[a].substring(0, 8);
arr2[a] = id;
}
//нахожу самый большой айди
String GetId;
if (arr2.length == 0)
GetId = "1";
else {
String a = Integer.toString(Integer.MIN_VALUE);
for (String s : arr2) {
String trim = s.substring(0, 8).trim();
if (Integer.parseInt(trim) > Integer.parseInt(a))
a = trim;
}
GetId = Integer.toString(Integer.parseInt(a) + 1);
}
//формирую имя
StringBuilder Name = new StringBuilder(args[1]);
String productName;
if (Name.length() < 30){
do {Name.append(" ");}
while (Name.length() != 30);
}
productName = Name.substring(0, 30);
//формирую цену
StringBuilder Price = new StringBuilder(args[2]);
String price;
if (Price.length() < 8){
do {Price.append(" ");}
while (Price.length() != 8);
}
price = Price.substring(0, 8);
//формирую количество
StringBuilder Quant = new StringBuilder(args[3]);
String quantity;
if (Quant.length() < 4){
do {Quant.append(" ");}
while (Quant.length() != 4);
}
quantity = Quant.substring(0, 4);
//формирую айди
StringBuilder Id = new StringBuilder(GetId);
String id;
if (Id.length() < 8){
do {Id.append(" ");}
while (Id.length() != 8);
}
id = Id.substring(0, 8);
//соединяю
String tovar = id + productName + price + quantity + "\n";
//добавляю в файл
FileWriter writer = new FileWriter(fileName, true);
BufferedWriter bufferWriter = new BufferedWriter(writer);
bufferWriter.write(tovar);
bufferWriter.close();
writer.close();
}
}
}
Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
заменила на такое считывание из файла, остались только незакрытые потоки