Меняет товар с определенным id порядок вроде бы не меняется Б где может быть ошибка подскажите пожалуйста.
package com.javarush.task.task18.task1828;
/*
Прайсы 2
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
// String name = "D:/Files/docWrite.txt";
// InputStream is = new ByteArrayInputStream(name.getBytes());
// System.setIn(is);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String fileName = bufferedReader.readLine();
bufferedReader.close();
SortedMap<Integer, String> map = new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
if (args.length > 0) {
String str;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
while ((str = br.readLine()) != null) {
String id = str.substring(0, 8).trim();
String s = String.format("%-42.42s", str.substring(8));
map.put(Integer.parseInt(id), s);
}
br.close();
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(fileName));
switch (args[0]) {
case "-u":
String id = args[1];
if (map.containsKey(Integer.parseInt(id))) {
String productName = "";
for (int i = 2; i < args.length - 2; i++) {
productName += args[i] + " ";
}
productName = String.format("%-30.30s", productName);
String price = String.format("%-8.8s", args[args.length - 2]);
String quantity = String.format("%-4.4s", args[args.length - 1]);
map.put(Integer.parseInt(id), productName + price + quantity);
map.forEach((k, v) -> {
try {
outStream.write(String.format("%-8.8s", String.valueOf(k)).getBytes());
outStream.write(v.getBytes());
outStream.write("\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
});
}
break;
case "-d":
String idDel = args[1];
if (map.containsKey(Integer.parseInt(idDel))) {
map.remove(Integer.parseInt(idDel));
map.forEach((k, v) -> {
try {
outStream.write(String.format("%-8.8s", String.valueOf(k)).getBytes());
outStream.write(v.getBytes());
outStream.write("\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
outStream.close();
}
}
}