Тестировал код с разными параметрами, записывает в файл по условию. Помогите.
package com.javarush.task.task18.task1827;
/*
Прайсы
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<Integer> maxNumbers = new ArrayList<>(); // для внесения id из файла
String forMaxNumber = "";
String sB = null;
int InMax = 0;
String StrMax = null;
ArrayList<Byte> allByteNumbers = new ArrayList<>();
for (int i = 48; i < 58; i++) {
allByteNumbers.add((byte) i);
}
StringBuilder strB = new StringBuilder();
String prodName = "";
if (args.length > 0 && args[0].equals("-c")) {
for (int i = 1; i < args.length - 2; i++) {
prodName = prodName + args[i] + " ";
}
prodName = prodName.trim();
byte[] productName = prodName.getBytes();
byte[] price = args[args.length - 2].getBytes();
byte[] quantity = args[args.length - 1].getBytes();
String id1 = "";
String productName1 = "";
String price1 = "";
String quantity1 = "";
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String nameOfFile = reader.readLine();
File file = new File(nameOfFile);
String str = "";
byte[] forReader = null;
try (BufferedReader fileReader = new BufferedReader(new FileReader(file))) {
while ((str = fileReader.readLine()) != null) {
forReader = str.getBytes();
for (int i = 0; i < 8; i++) {
if (allByteNumbers.contains(forReader[i]))
forMaxNumber = forMaxNumber + (char) forReader[i];
}
maxNumbers.add(Integer.parseInt(forMaxNumber));
forMaxNumber = "";
}
InMax = Collections.max(maxNumbers);
StrMax = ++InMax + ""; // получаем максимальный индекс
// strB.append("\n");
byte[] id = StrMax.getBytes();
for (int i = 0; i < 8; i++) {
if (id.length > i) id1 = id1 + (char) id[i];
else id1 = id1 + " ";
}
strB.append(id1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (int i = 0; i < 30; i++) {
if (productName.length > i) productName1 = productName1 + (char) productName[i];
else productName1 = productName1 + " ";
}
strB.append(productName1);
for (int i = 0; i < 8; i++) {
if (price.length > i) price1 = price1 + (char) price[i];
else price1 = price1 + " ";
}
strB.append(price1);
for (int i = 0; i < 4; i++) {
if (quantity.length > i) quantity1 = quantity1 + (char) quantity[i];
else quantity1 = quantity1 + " ";
}
strB.append(quantity1);
// System.out.println(strB);
try (BufferedWriter bf = new BufferedWriter(new FileWriter(file, true))) {
bf.newLine();
bf.write(strB.toString());
} catch (FileNotFoundException e) {
}
}
}
}
}