package com.javarush.task.task18.task1828;
/*
Прайсы 2
*/
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String nameFile;
nameFile = br.readLine();
switch (args[0]){
case "-d": delete(nameFile, args[1]); break;
case "-u": update(nameFile, args[1], args[2], args[3], args[4]); break;
}
}
private static void update(String nameFile, String id, String productName, String price, String quantity) throws IOException {
Pattern p = Pattern.compile("^\\d+");
File upFile = new File (nameFile);
BufferedReader brf = new BufferedReader(new FileReader(upFile));
File copyFile = new File(upFile.getParent(),"copy.txt");
BufferedWriter brOut = new BufferedWriter(new FileWriter(copyFile, true));
String line;
while((line=brf.readLine())!=null){
Matcher m = p.matcher(line);
if(m.find()){
String iD = line.substring(0,m.end());
if (!iD.equals(id)){
brOut.write(line);
brOut.newLine();
} else {
StringBuilder strB = new StringBuilder();
String nameID = String.format("%-8s", id);
strB.append(nameID);
String nameProduct = String.format("%-30s", productName);
strB.append(nameProduct);
String namePrice = String.format("%-8s", price);
strB.append(namePrice);
String nameQuantity = String.format("%-4s", quantity);
strB.append(nameQuantity);
brOut.write(strB.toString());
brOut.newLine();
}
}
}
brf.close();
brOut.close();
upFile.delete();
copyFile.renameTo(upFile);
}
private static void delete(String nameFile, String id) throws IOException {
Pattern p = Pattern.compile("^\\d+");
File upFile = new File (nameFile);
BufferedReader brf = new BufferedReader(new FileReader(upFile));
File copyFile = new File(upFile.getParent(),"copy.txt");
BufferedWriter brOut = new BufferedWriter(new FileWriter( copyFile, true));
String line ;
while((line=brf.readLine())!=null){
Matcher m = p.matcher(line);
if(m.find()) {
String iD = line.substring(0, m.end());
if (!iD.equals(id)) {
brOut.write(line);
brOut.newLine();
}
}
}
brf.close();
brOut.close();
upFile.delete();
copyFile.renameTo(upFile);
}
}
Aleksandr
22 уровень
Что не так ?
Обсуждается
Комментарии (1)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
Anonymous #374105
29 ноября 2018, 23:13
Ещё один заброшенный вопрос ...
0