При проверке в IDEA никаких ошибок.
package com.javarush.task.task18.task1827;
/*
Прайсы
*/
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
final String FORMAT = "%-8d%-30s%-8.2f%-4d\n";
Long id = 1l;
Long maxId = 0l;
String productName = null;
Double price = 0d;
Integer quantity = 0;
if (args.length == 4) {
if (args[0].equals("-c")) {
productName = args[1];
price = Double.parseDouble(args[2]);
quantity = Integer.parseInt(args[3]);
}
if (productName != null && price != 0 && quantity != 0) {
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rwd");
FileChannel fileChannel = randomAccessFile.getChannel();
long position = randomAccessFile.length();
// If file is not empty
if (position > 0) {
randomAccessFile.seek(0);
// Find maximum ID and increment it
while (fileChannel.position() < position) {
String line = randomAccessFile.readLine();
Long curId = Long.parseLong(line.substring(0, 8).trim());
if (curId > maxId)
maxId = curId;
}
id = ++maxId;
}
String output = String.format(FORMAT, id, productName, price, quantity);
randomAccessFile.write(output.getBytes());
randomAccessFile.close();
}
}
}
}