Ребят, я тут, конечно, извратился и уже сделал нормально через обычный FileReader, но всё равно не понимаю, почему не работает такой вариант, хоть он и извращенный. Я тестировал дебаггером и выяснил, что у меня в buff (буфер) записывается вообще не то, что нужно начиная со второй итерации и я не понимаю, что не так
public static void Create (String fileName, String name, String price, String quantity) throws Exception
{
FileInputStream inputStream = new FileInputStream(fileName);
byte[] buff = new byte[52];
int maxId = 0;
while (inputStream.available() > 0) {
inputStream.read(buff);
StringBuilder id = new StringBuilder();
for (int i = 0; i < 8; i++) {
if (buff[i] != 32)
id.append((char) buff[i]);
}
String idStr = id.toString();
maxId = Math.max(Integer.parseInt(idStr), maxId);
}
inputStream.close();
maxId++;
String id ="" + maxId;
while (id.length() < 8)
{
id += " ";
}
while (name.length() < 30)
{
name += " ";
}
if (name.length() > 30)
{
name = name.substring(0, 29);
}
while (price.length() < 8)
{
price += " ";
}
if (price.length() > 8)
{
price = price.substring(0, 7);
}
while (quantity.length() < 4)
{
quantity += " ";
}
if (quantity.length() > 4)
{
quantity = quantity.substring(0, 3);
}
FileOutputStream outputStream = new FileOutputStream(fileName, true);
String result = id + name + price + quantity + "/n";
outputStream.write(result.getBytes());
outputStream.close();
}