A
A
Andrey Afonchenko2020-12-01 20:38:52
Java
Andrey Afonchenko, 2020-12-01 20:38:52

How to read information from a file into multiple arrays using Java?

Hello. There is a file. I wrote down the names and grades from the corresponding arrays into it. In the file, the lines look like: (Last name ; grade) Can you please tell me how to read the information into the other two arrays now?

package com.company;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Class_File {
    public void writeFile() {
        String temp;
        File file = new File("text.txt");
        if(file.exists()) {
            System.out.println("Файл існує!");
        }
        else {
            System.out.println("Файлу не інснує!");
        }
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(file);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        for(int i = 0; i < Main.name.length; i++) {
            temp = Main.name[i] + " ; " + Main.score[i];
            pw.println(temp);
        }
        System.out.println("Розмір файлу у байтах: " + file.length());
        System.out.println("Шлях до файлу: " + file.getAbsolutePath());
        pw.flush();
        pw.close();
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2020-12-01
Hasanly @azerphoenix

Hello!
Read the file line by line, then each line as you read do split() by the character ";" and add to an array or collection.

D
Denis Zagaevsky, 2020-12-01
@zagayevskiy

Open the file for reading, read line by line and split by ;, adding values ​​to the appropriate array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question