Ребят, подкиньте идею, а то голова уже не варит!! Выводит вроде правильно.
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
Прайсы 2
*/
//251378 Пляжные шорты черные 23.00 45
///Users/maxi/Downloads/Lion.avi.part2.txt
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file = reader.readLine();
BufferedReader fileReader = new BufferedReader(new FileReader(file));
StringBuffer inputBuffer = new StringBuffer();
String line= fileReader.readLine();
String[] num;
if(args[0].equals("-d")){
while (line != null) {
num = line.split(" ");
if (Integer.parseInt(num[0]) != Integer.parseInt(args[1])) {
inputBuffer.append(line);
inputBuffer.append("\n");
}
line = fileReader.readLine();
}
reader.close();
fileReader.close();
}
if (args[0].equals("-u")) {
while (line != null) {
num = line.split(" ");
if (Integer.parseInt(num[0]) != Integer.parseInt(args[1])) {
inputBuffer.append(line);
inputBuffer.append("\n");
}
Pattern pattern = Pattern.compile("^\\d+");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
try {
String id = String.format("%-8s", args[1]);
String productName = String.format("%-30s", args[2]);
String price = String.format("%-8s", args[3]);
String quantity = String.format("%-4s", args[4]);
if (line.substring(matcher.start(), matcher.end()).equals(args[1])) {
String nuv = id + productName + price + quantity;
inputBuffer.append(nuv);
inputBuffer.append("\n");
}
}
catch (Exception e){
e.getStackTrace();
}
}
line = fileReader.readLine();
}
reader.close();
fileReader.close();
}
// System.out.println(inputBuffer.toString());
FileOutputStream fileWrite = new FileOutputStream(file);
fileWrite.write(inputBuffer.toString().getBytes());
fileWrite.close();
}
}
if (args[0].equals("-d")) { num = line.split(" ");правильнее использовать substring(0,8).trim(), так как у тебя заведомо в строке первые 8 символов это id 2) аналогичная проблема что и в пункте 1 3) зачем ты закрываешь 2 раза поток? у тебя будет выброшено исключение: