Трезвый взгляд может увидит что не так
package com.javarush.task.task18.task1828;
/*
Прайсы 2
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
BufferedReader in = in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "Cp1251"));
BufferedWriter writer = null;
int id = 0;
String productName = null;
String price = null;
String quantity = null;
ArrayList<String> list = new ArrayList<>();
String line = "";
if (args.length <= 5) {
while (in.ready()) {
line = in.readLine();
id = Integer.parseInt(args[1]);
if (args.length == 5) {
productName = args[2];
price = args[3];
quantity = args[4];
}
String str = line.substring(0,8);
int findId = Integer.parseInt(str.split(" ")[0]);
if (args[0].equals("-u")) {
if (findId != id) {
list.add(line);
} else {
list.add(String.format("%-8s%-30s%-8s%-4s", id, productName, price, quantity));
}
} else if (args[0].equals("-d")) {
if (findId != id) {
list.add(line);
}
}else return;
}
in.close();
writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(fileName),"Cp1251"));
for (String s : list) {
writer.write(s);
writer.newLine();
}
list.clear();
writer.close();
}
}
}