public static void main(String[] args) throws IOException { // write your code here String flag=null; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String fileName = bf.readLine(); bf.close(); String argumentString=""; for (String str: args) argumentString+=" "+str; if(args[0].equals("-c")) {flag =argumentString.substring(0,3); String productName=argumentString.substring(4,33); String price = argumentString.substring(34,41); String quntity =argumentString.substring(41); String id = idGenerator(fileName); String generatedString = id+productName+price+quntity; write(generatedString,fileName);} } private static void write(String product,String fileName) throws IOException { FileOutputStream fileWriter = new FileOutputStream(fileName,true); fileWriter.write(product.getBytes( System.getProperty("console.encoding","Cp1251"))); fileWriter.write(13); fileWriter.close(); //System.out.println(product); } private static String idGenerator(String fileName) throws IOException { ArrayList<String> id = new ArrayList(); String maxId="0";String valueGenerator="";int count=0;boolean whit=true; FileInputStream fileInput = new FileInputStream(fileName); while (fileInput.available()>0) { int codePoint = fileInput.read(); if (whit){ valueGenerator += (char) codePoint; count++;} if (count == 8) { id.add(valueGenerator.trim()); valueGenerator = ""; count = 0; whit = false; } if (codePoint == 10) whit = true; } fileInput.close(); for (String idAll:id) if (Integer.parseInt(maxId)<Integer.parseInt(idAll))maxId=idAll; maxId=String.valueOf(Integer.parseInt(maxId)+1); if (8-maxId.length()>0)for (int i=0;i<8-maxId.length();i++ )maxId+=" "; return maxId; } }