Java 从文件中读取类的对象数组
我有一个由三个字段组成的类,两个Java 从文件中读取类的对象数组,java,filereader,Java,Filereader,我有一个由三个字段组成的类,两个int和一个double。我已经列出了我的类对象,并希望使用扫描仪、缓冲读取器和文件读取器从文本文件中逐行读取数组的对象 MWE: 但是,出现了一个错误“将行更改为字符串”。如果我有一个3维数组,而不是包含3个字段的Sleg类slegs对象,我知道如何读取文件,但是对于这个,我不知道。 我很感激你的想法 您提供的代码有一些问题。您试图将Boolean.parseBoolean(String)与String[]一起使用,假设文本文件中的行数,list2变量的类型为S
int
和一个double
。我已经列出了我的类对象,并希望使用扫描仪
、缓冲读取器
和文件读取器
从文本文件中逐行读取数组的对象
MWE:
但是,出现了一个错误“将行更改为字符串”。如果我有一个3维数组,而不是包含3个字段的Sleg类slegs对象,我知道如何读取文件,但是对于这个,我不知道。
我很感激你的想法 您提供的代码有一些问题。您试图将
Boolean.parseBoolean(String)
与String[]
一起使用,假设文本文件中的行数,list2
变量的类型为Sleg[]
,但您试图为其元素分配一个double
,以及各种其他内容
假设您只对将文件读入Slug
对象列表感兴趣,您可能希望执行以下操作:
// Use a list since the length of the file is unkown
List<Sleg> slegs = new ArrayList<Sleg>();
File slFile = new File("slFile.txt");
// Use try-with-resources block so the reader is closed automatically,
// no need to use Scanner since we're only interested in reading lines...
try (BufferedReader reader = new BufferedReader(new FileReader("slFile.txt"))) {
// Read the file line by line
String line;
while ((line = reader.readLine()) != null) {
// Split the line, convert values, and add new sleg.
String[] numbers = line.trim().split(" ");
int i = Integer.parseInt(numbers[0]);
int j = Integer.parseInt(numbers[1]);
double l = Double.parseDouble(numbers[2]);
slegs.add(new Sleg(i, j , l));
}
} catch (FileNotFoundException e) {
System.out.println(slFile.toString() + " does not exist.");
} catch (IOException e) {
// Handle any possible IOExceptions as well...
System.out.println("Unable to read : " + slFile.toString());
}
//使用列表,因为文件长度未知
List slegs=new ArrayList();
File slFile=新文件(“slFile.txt”);
//使用try with resources块,使读卡器自动关闭,
//不需要使用扫描仪,因为我们只对阅读行感兴趣。。。
try(BufferedReader=newbufferedreader(newfilereader(“slFile.txt”)){
//逐行读取文件
弦线;
而((line=reader.readLine())!=null){
//拆分线、转换值并添加新的sleg。
字符串[]数字=line.trim().split(“”);
int i=Integer.parseInt(数字[0]);
intj=Integer.parseInt(数字[1]);
double l=double.parseDouble(数字[2]);
添加(新的Sleg(i,j,l));
}
}catch(filenotfounde异常){
System.out.println(slFile.toString()+“不存在”);
}捕获(IOE异常){
//同时处理任何可能的IOException。。。
System.out.println(“无法读取:+slFile.toString());
}
您提供的代码存在一些问题。您试图将Boolean.parseBoolean(String)
与String[]
一起使用,假设文本文件中的行数,list2
变量的类型为Sleg[]
,但您试图为其元素分配一个double
,以及各种其他内容
假设您只对将文件读入Slug
对象列表感兴趣,您可能希望执行以下操作:
// Use a list since the length of the file is unkown
List<Sleg> slegs = new ArrayList<Sleg>();
File slFile = new File("slFile.txt");
// Use try-with-resources block so the reader is closed automatically,
// no need to use Scanner since we're only interested in reading lines...
try (BufferedReader reader = new BufferedReader(new FileReader("slFile.txt"))) {
// Read the file line by line
String line;
while ((line = reader.readLine()) != null) {
// Split the line, convert values, and add new sleg.
String[] numbers = line.trim().split(" ");
int i = Integer.parseInt(numbers[0]);
int j = Integer.parseInt(numbers[1]);
double l = Double.parseDouble(numbers[2]);
slegs.add(new Sleg(i, j , l));
}
} catch (FileNotFoundException e) {
System.out.println(slFile.toString() + " does not exist.");
} catch (IOException e) {
// Handle any possible IOExceptions as well...
System.out.println("Unable to read : " + slFile.toString());
}
//使用列表,因为文件长度未知
List slegs=new ArrayList();
File slFile=新文件(“slFile.txt”);
//使用try with resources块,使读卡器自动关闭,
//不需要使用扫描仪,因为我们只对阅读行感兴趣。。。
try(BufferedReader=newbufferedreader(newfilereader(“slFile.txt”)){
//逐行读取文件
弦线;
而((line=reader.readLine())!=null){
//拆分线、转换值并添加新的sleg。
字符串[]数字=line.trim().split(“”);
int i=Integer.parseInt(数字[0]);
intj=Integer.parseInt(数字[1]);
double l=double.parseDouble(数字[2]);
添加(新的Sleg(i,j,l));
}
}catch(filenotfounde异常){
System.out.println(slFile.toString()+“不存在”);
}捕获(IOE异常){
//同时处理任何可能的IOException。。。
System.out.println(“无法读取:+slFile.toString());
}
好的,我使用LinkedList是因为我不知道有多少个项目/行(我可以数一数,但如果没有关于有多少行的指示,即第一行指示项目的数量…我将使用LinkedList)。现在,您的方法dataGen
将返回一个LinkedList
存储Sleg
对象。以下是一个正在工作的MVCE:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.Scanner;
public class Model {
public static final int Y = 7;
public static final int K = 42;
public static final int G = 3;
public Model() {
dataGen();
}
public LinkedList<Sleg> dataGen() {
LinkedList<Sleg> data = new LinkedList<>();
File slFile = new File("slFile.txt");
try {
Scanner in7 = new Scanner(new BufferedReader(new FileReader("slFile.txt")));
while (in7.hasNextLine()) {
//get current line and split it via a space.
String[] split = in7.nextLine().split(" ");
int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]);
double z = Double.parseDouble(split[2]);
Sleg newItem = new Sleg(x, y, z);//new entry.
data.add(newItem);//add as an entry to data.
}
in7.close();//should have this!!!
} catch (FileNotFoundException e) {
System.out.println("Unable to read : " + slFile.toString());
}
for (Sleg current : data) {
System.out.println(current.toString());
}
return data;
}
public static void main(String[] args) {
new Model();
}
class Sleg {
int x = 0;
int y = 0;
double z = 0;
Sleg(int x, int y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.util.LinkedList;
导入java.util.Scanner;
公共类模型{
公共静态最终整数Y=7;
公共静态最终int K=42;
公共静态最终整数G=3;
公共模型(){
dataGen();
}
公共链接列表dataGen(){
LinkedList数据=新建LinkedList();
File slFile=新文件(“slFile.txt”);
试一试{
Scanner in7=新的扫描仪(新的BufferedReader(新的文件读取器(“slFile.txt”));
while(在7.hasNextLine()中){
//获取当前行并通过空格将其拆分。
字符串[]split=in7.nextLine().split(“”);
intx=Integer.parseInt(拆分[0]);
int y=Integer.parseInt(拆分[1]);
double z=double.parseDouble(split[2]);
Sleg newItem=newsleg(x,y,z);//新条目。
data.add(newItem);//作为数据项添加。
}
in7.close();//应该有这个!!!
}catch(filenotfounde异常){
System.out.println(“无法读取:+slFile.toString());
}
用于(Sleg电流:数据){
System.out.println(current.toString());
}
返回数据;
}
公共静态void main(字符串[]args){
新模型();
}
类Sleg{
int x=0;
int y=0;
双z=0;
Sleg(整数x,整数y,双z){
这个.x=x;
这个。y=y;
这个。z=z;
}
@凌驾
公共字符串toString(){
返回“(“+x+”、“+y+”、“+z+”)”;
}
}
}
好的,我使用LinkedList是因为我不知道有多少项/行(我可以数一数,但如果没有关于有多少行的指示,即第一行指示项目的数量)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.Scanner;
public class Model {
public static final int Y = 7;
public static final int K = 42;
public static final int G = 3;
public Model() {
dataGen();
}
public LinkedList<Sleg> dataGen() {
LinkedList<Sleg> data = new LinkedList<>();
File slFile = new File("slFile.txt");
try {
Scanner in7 = new Scanner(new BufferedReader(new FileReader("slFile.txt")));
while (in7.hasNextLine()) {
//get current line and split it via a space.
String[] split = in7.nextLine().split(" ");
int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]);
double z = Double.parseDouble(split[2]);
Sleg newItem = new Sleg(x, y, z);//new entry.
data.add(newItem);//add as an entry to data.
}
in7.close();//should have this!!!
} catch (FileNotFoundException e) {
System.out.println("Unable to read : " + slFile.toString());
}
for (Sleg current : data) {
System.out.println(current.toString());
}
return data;
}
public static void main(String[] args) {
new Model();
}
class Sleg {
int x = 0;
int y = 0;
double z = 0;
Sleg(int x, int y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}
}
}
while(in7.hasNextLine()) {
for (int sl = 0; sl < list2.length; sl++) {
String[] line = in7.nextLine().trim().split(" ");
list2[sl] = Double.parseDouble(line);
}
}
int sl = 0;
while(in7.hasNextLine() && sl < list2.length) {
String[] line = in7.nextLine().trim().split(" ");
if (line.length < 3) {
System.out.println("invalid line: " + line);
continue;
}
list2[sl] = new Sleg(
Integer.parseInt(line[0]),
Integer.parseInt(line[1]),
Double.parseInt(line[2])
);
sl++;
}