А еще, почему вылез последний пункт, если у меня везде try-with-resources?
package com.javarush.task.task18.task1827;
/*
Прайсы
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Solution {
public static void main(String[] args) throws Exception {
String fileNameReader = "";
String fileNameWriter = "";
String res;
if (args.length > 0){
if (args[0].equals("-c") && args.length == 4) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
fileNameReader = reader.readLine();
fileNameWriter = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileNameWriter, true))) {
String id = idMaker(fileNameReader);
id = String.format("%-8.8s", id);
String productName = String.format("%-30.30s", args[1]);
String price = String.format("%-8.2f", Double.parseDouble(args[2]));
String quantity = String.format("%-4.4s", args[3]);
res = id + productName + price + quantity;
bw.write(res + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String idMaker(String fileNameReader) {
List<Integer> list = new ArrayList<>();
String str;
int rawId;
String res = "";
try (BufferedReader reader = new BufferedReader(new FileReader(fileNameReader))) {
while (reader.ready()) {
str = reader.readLine().substring(0, 8);
rawId = Integer.parseInt(str.replaceAll("[^0-9]", ""));
list.add(rawId);
}
Collections.sort(list);
res = Integer.toString(list.get(list.size() - 1) + 1);
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
}