gpt4 book ai didi

java - 从文本文件中获取数据并将其放入锯齿状数组中

转载 作者:行者123 更新时间:2023-12-01 22:38:26 25 4
gpt4 key购买 nike

String Service;
BufferedReader in = null;
BufferedReader readn = new BufferedReader(new FileReader(new File(filepath)));

//Job [] dj = new Job[vlinenumber];
String linen="";
while ((linen = readn.readLine()) != null) {
//System.out.println (line);
String[] vem =linen.split(",");
int j=linen.length();



String regiration=vem[0];
String make=vem[1];
String Model=vem[2];
String Year=vem[3];
String odo=vem[4];


int ant=vem.length;
System.out.println(ant+"this is ");

String cv=vem[5];

if(ant==3){
String aaa= vem[6];
}

if(ant==4){
String aaa= vem[7];
}

文本文件示例:

OWNED,Ford,Falcon XL Ute,2008,167000,14331
FAST,Maserati,Quattroporte Sport GT,2007,48960,14321,90309
OK2BL8,Morris,Minor 1000,1969,567340,14315,90311

最佳答案

你应该这样尝试:

     public class Test {

public static void main(String[] args) throws IOException {

BufferedReader readn = new BufferedReader(new FileReader(new File(
"d:\\data.txt")));

String linen = "";
List<Details> dList = new ArrayList<>();
while ((linen = readn.readLine()) != null) {
String[] data = linen.split(",");
Details d = new Details();
if (data.length > 0) {
d.setReg(data[0]);
}
if (data.length > 1) {
d.setMake(data[1]);
}
if (data.length > 2) {
d.setModel(data[2]);
}
if (data.length > 3) {
d.setYear(data[3]);
}
if (data.length > 4) {
d.setOdo(data[4]);
}
if (data.length > 5) {
d.setCv(data[5]);
}
if (data.length > 6) {
d.setAa(data[6]);
}
if (data.length > 7) {
d.setAa(data[7]);
}
System.out.println(d);
dList.add(d);

}
}

}

模型类:

  class Details {
String reg;
String make;
String model;
String year;
String odo;
String aa;
String cv;

public String getReg() {
return reg;
}

public void setReg(String reg) {
this.reg = reg;
}

public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getYear() {
return year;
}

public void setYear(String year) {
this.year = year;
}

public String getOdo() {
return odo;
}

public void setOdo(String odo) {
this.odo = odo;
}

public String getAa() {
return aa;
}

public void setAa(String aa) {
this.aa = aa;
}

public String getCv() {
return cv;
}

public void setCv(String cv) {
this.cv = cv;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return reg + "\t" + make + "\t" + model + "\t" + year + "\t" + odo+"\t"+cv+"\t"+aa;
}
}

关于java - 从文本文件中获取数据并将其放入锯齿状数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26542752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com