без параметров - мы считаем имя файла и все? или что-то еще нужно сделать? Лично я из ТЗ не понял.
и в файл, добавляется же новой строкой, оттестировал несколько раз на своих файлах - добавляет, что не так то?
package com.javarush.task.task18.task1827;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
Прайсы
*/
public class Solution {
public static void main(String[] args) throws Exception {
//String s = "/Users/a14841729/Downloads/test1.txt";
//String args[] = new String[] {"-c", "новый товар5", "1.50", "2"};
if (args[0].equals("-c")) {
String s = readConsole();
writeFile(s, cou(findNumberEn(s), args[1], args[2], args[3]));
} else {
readConsole();
}
}
public static String readConsole() {
String s = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
s = reader.readLine();
} catch (IOException e) {
System.out.println("ReadConsole is fail");
}
return s;
}
public static String cou(int id, String productName, String price, String qua) {
String s = "";
String b = String.valueOf(id);
if (b.length() < 8) {
int a = 8 - b.length();
for (int i = 0; i < a; i++) {
b = b + " ";
}
}
s = s + b;
if (productName.length() < 30) {
int a = 30 - productName.length();
for (int i = 0; i < a; i++) {
productName = productName + " ";
}
}
s = s + productName;
if (price.length() < 8) {
int a = 8 - price.length();
for (int i = 0; i < a; i++) {
price = price + " ";
}
}
s = s + price;
if (qua.length() < 4) {
int a = 4 - qua.length();
for (int i = 0; i < a; i++) {
qua = qua + " ";
}
}
s = s + qua + "\n";
return s;
}
public static void writeFile(String nameFileWrite, String file) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(nameFileWrite,true))) {
writer.write(file);
} catch (IOException e) {
System.out.println("File Write is fail");
}
}
public static int findNumberEn(String name) {
ArrayList<Integer> arr = new ArrayList<>();
String s = "";
try (BufferedReader reader = new BufferedReader(new FileReader(name))) {
while (reader.ready()) {
String[] num = reader.readLine().split("");
for (int i = 0; i < 9; i++) {
if (isDigit(num[i])) {
s = s + num[i];
} else {
}
}
arr.add(Integer.parseInt(s));
s = "";
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
e.printStackTrace();
}
Collections.sort(arr);
return arr.get(arr.size() - 1) + 1;
}
public static boolean isDigit(String s) throws NumberFormatException {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}