В файл все выводится правильно.
package com.javarush.task.task18.task1827;
/*
Прайсы
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
File file = new File(reader.readLine());
BufferedReader rf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
int maxId = 0;
String str;
String s;
while ((str = rf.readLine())!=null){
String strSub = str.substring(0, 8);
strSub = strSub.trim();
if (maxId < Integer.parseInt(strSub)); {
maxId = Integer.parseInt(strSub);
}
}
maxId++;
rf.close();
reader.close();
if(args[0].equals("-c") && args[1].length() <= 30 && args[2].length() <= 8 && args[3].length() <= 4){
for (int i = args[1].length();i < 30;i++){
args[1]+=" ";
}
for(int i = args[2].length(); i < 8;i++){
args[2]+=" ";
}
String maxstr = String.valueOf(maxId);
for (int i = maxstr.length();i < 8;i++){
maxstr+=" ";
}
String st = maxstr +args[1].toString() + args[2].toString()+ args[3].toString();
PrintStream printStream = new PrintStream(new FileOutputStream(file, true));
printStream.println(st);
printStream.close();
}
}
}