Прочитать информацию из файла в несколько масивов
Всем привет. Есть файл. в него записал фамилии и оценки из соответствующих массивов. В файле строки имеют вид: (Фамилия ; оценка) Подскажите, пожалуйста, как теперь прочитать информацию в другие два массива?
package com.company;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Class_File {
public void writeFile() {
String temp;
File file = new File("text.txt");
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);
}
pw.flush();
pw.close();
}
}
Ответы (2 шт):
Автор решения: Эникейщик
→ Ссылка
Читай построчно каждую строку, дели по символу ;, в один массив добавляй первый элемент, в другой - второй.
Автор решения: Олексій Моренець
→ Ссылка
void readFile() throws IOException
{
File file = new File("text.txt");
try (FileReader fr = new FileReader(file);
BufferedReader reader = new BufferedReader(fr))
{
String temp;
int index = 0;
while ((temp = reader.readLine()) != null)
{
String[] parts = temp.split(";");
Main.name[index] = parts[0];
Main.score[index] = Integer.parseInt(parts[1]);
index++;
}
}
}