Can you please give me an advice..
package com.codegym.task.task18.task1827;
/*
Prices
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
final int LINE_LEN = 52;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// Check args
if(args.length == 0) {
return;
}
if(!args[0].equals("-c")) {
return;
}
// for (String arg: args) {
// System.out.println(arg);
// }
// Read file name
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = null;
fileName = reader.readLine();
reader.close();
// String fileName = "f1.txt";
// Copy file data to list
fis = new FileInputStream(fileName);
ArrayList<String> list = new ArrayList<>();
byte[] buffer = new byte[LINE_LEN];
while (fis.available() > 0) {
fis.read(buffer);
list.add(new String(buffer));
// System.out.print(new String(buffer));
}
fis.close();
// Write data from list to the file
fos = new FileOutputStream(fileName);
int id = 0;
int maxId = 0;
String sLine = "";
String line;
for(int i = 0; i < list.size(); i++) {
line = list.get(i);
// System.out.print(line);
id = Integer.parseInt(line.substring(0,8).trim());
// Find max Id
if(id > maxId) {
maxId = id;
}
fos.write(line.getBytes());
}
// Increment maxId by 1 and add spaces
String sMaxId = String.valueOf(maxId + 1);
int len = sMaxId.length();
for(int i = 0; i < (8 - len); i++) {
sMaxId += " ";
}
// Get other args
String productName = args[1];
String price = args[2];
String quantity = args[3];
len = productName.length();
// Format product name
if (len > 30) {
productName = productName.substring(0, 30);
} else if (len < 30){
for(int i = 0; i < (30 - len); i++) {
productName += " ";
}
}
// Format price
len = price.length();
if (len > 8) {
price = price.substring(0, 8);
} else if (len < 8) {
for(int i = 0; i < (8 - len); i++) {
price += " ";
}
}
// Format quantity
len = quantity.length();
if (len > 4) {
quantity = quantity.substring(0, 4);
} else if (len < 4) {
for(int i = 0; i < (4 - len); i++) {
quantity += " ";
}
}
// Combine all parameters & add new line at the end
String newLine = sMaxId + productName + price + quantity + "\n";
// System.out.print(newLine);
fos.write(newLine.getBytes());
fos.close();
} catch (Exception e) {
try {
fis.close();
fos.close();
} catch (IOException ex) {
}
}
}
}