The problem is this: you need to create separate files for each of the customers (customer) from the file already created, and each such file should contain information about the Name, Last Name, Price and Quantity. columns

public class FileOperations {

static final String DIR_NAME = "C:" + File.separator + "outdata" + File.separator; static final String FILE_NAME = "output.csv";

static File outputDir = new File(DIR_NAME); static File outputFile = new File(DIR_NAME + FILE_NAME); public static void save() throws IOException { if (outputFile.exists()) System.out.println("File " + outputFile.getAbsolutePath() + " already exists"); if (!outputDir.exists()) outputDir.mkdirs(); // true - adds data, false - replaces FileWriter fw = new FileWriter(outputFile, false); BufferedWriter bw = new BufferedWriter(fw); bw.write("number;firstname;lastname"); bw.newLine(); bw.write("1;\"John\";\"Smith\""); bw.newLine(); bw.write("2;\"Anne\";\"Brown\""); bw.close(); fw.close(); } public static String[][] readCsvFile(File inFile) throws FileNotFoundException { String[][] output = new String[3][4]; Scanner scanner = new Scanner(inFile); int i = 0; while (scanner.hasNext()) { String lineOfText = scanner.next(); System.out.println("Line " + i + ": " + lineOfText); String[] cells = lineOfText.split(";"); System.out.println("Cells: " + java.util.Arrays.toString(cells)); for (int j=0;j<cells.length;j++) { output[i][j] = cells[j]; } i++; } scanner.close(); return output; } public static void main(String[] args) { try { save(); String[][] out = readCsvFile(outputFile); for (int i =0;i<out.length;i++) { System.out.println("Row: " + java.util.Arrays.toString(out[i])); } } catch (IOException e) { System.out.println("File is not writeable: " + e.getMessage()); } } 

} `

    1 answer 1

    If I understand correctly, then in a certain source file is information about customers. As a solution to this problem, I would suggest the following simple procedure:
    1) Create a Customer class that describes such an object as a customer, i.e. with all required fields (first name, surname, etc.)
    2) Read the source data into a set of instances of this class. Here you just read the name, surname, price, quantity. Having considered, you create an instance of the Customer class, filling its fields.
    3) When you process all the data from the source file in this way, you can continue to create a separate file (the client’s last name can be used as the file name) and output the information about the client to each such separate file.

    Of course, this is not the fastest way, but, however, it is extremely clear.