Не проходит валидацию. Прошу взглянуть свежим взглядом, мои зенки уже замыленные))
package com.javarush.task.task18.task1827;
/*
Прайсы
*/
import java.io.*;
import java.util.Locale;
public class Solution {
public static void main(String[] args) throws Exception {
if (args.length != 0 && "-c".equals(args[0])) {
createProduct(args);
}
}
private static void createProduct(String[] args) {
String productName = args[1];
double productPrice = Double.parseDouble(args[2]);
int productQuantity = Integer.parseInt(args[3]);
String dataBase = "";
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
dataBase = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
long nextId = getNextIdFromBd(dataBase);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dataBase, true)))) {
writer.write((String.format(Locale.CANADA,"%n" +
"%-8d" +
"%-30.30s" +
"%-5.2f" +
"%4d", nextId, productName, productPrice, productQuantity)));
} catch (IOException e) {
e.printStackTrace();
}
}
private static long getNextIdFromBd(String fileName) {
File file = new File(fileName);
String lastStringFromBd = getLastStringFromFile(file);
long id = Integer.parseInt(lastStringFromBd.substring(0,8));
return id + 1;
}
private static String getLastStringFromFile(File file) {
StringBuilder builder = new StringBuilder();
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
long fileLength = file.length() - 1;
randomAccessFile.seek(fileLength);
for (long pointer = fileLength; pointer >= 0; pointer--) {
randomAccessFile.seek(pointer);
char c;
// read from the last one char at the time
c = (char) randomAccessFile.read();
// break when end of the line
if (c == '\n') {
break;
}
builder.append(c);
}
// Since line is read from the last so it
// is in reverse so use reverse method to make it right
builder.reverse();
randomAccessFile.close();
return new String(builder.toString().getBytes("ISO-8859-1"), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return "Something wrong. It's not a last line from file.";
}
}
}