Need help
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Solution {
public static void main(String[] args) {
BufferedReader reader = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// When you run the program without arguments, the product list must remain unchanged.
if(args.length == 0) {
return;
}
// The program should read a file name for CrUD operations from the console.
reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
// String fileName = "f2.txt";
reader.close();
// Copy file table to HashMap
Map<String, String> map = new HashMap<>();
fis = new FileInputStream(fileName);
byte[] buffer = new byte[52];
String sBuffer = "";
while (fis.available() > 0) {
fis.read(buffer);
sBuffer = new String(buffer);
map.put(sBuffer.substring(0, 8), sBuffer.substring(8));
}
fis.close();
// Get argument values & perform CrUD operations on HashMap
// String id = "", productName = "", price = "", quantity = "";
if(args[0].equals("-u")){
map.replace(args[1], args[2] + args[3] + args[4]);
} else if(args[0].equals("-d")) {
map.remove(args[1]);
} else {
throw new IllegalArgumentException("First argument must eighter be -u or -d");
}
// Copy updated HashMap to File
fos = new FileOutputStream(fileName);
for(Map.Entry<String, String> entry : map.entrySet()) {
fos.write((entry.getKey() + entry.getValue()).getBytes());
}
// Close file streams
fos.close();
} catch (IOException e) {
try {
reader.close();
fis.close();
fos.close();
} catch (IOException ex) {
}
}
}
}