问题遇到的现象和发生背景
我用java构建了一个结构体,在读取数据的时候一直重复读取文件中的部分内容
问题相关代码,请勿粘贴截图
class shuju{
private int grade = 0;
private String leixing,lianmeng,qiudui,qiuxing;
public shuju(){}
public shuju(String leixing, String lianmeng,String qiudui,String qiuxing, int grade){
this.leixing = leixing;
this.lianmeng = lianmeng;
this.qiudui = qiudui;
this.qiuxing = qiuxing;
this.grade = grade;
}
public void setleixing(String leixing) { this.leixing = leixing; }
public void setlianmeng(String lianmeng) { this.lianmeng = lianmeng; }
public void setqiudui(String qiudui) { this.qiudui = qiudui; }
public void setqiuxing(String qiuxing) { this.qiuxing = qiuxing; }
public void setGrade(int grade) { this.grade = grade; }
public String getleixing() { return leixing; }
public String getlianmeng() { return lianmeng; }
public String getqiudui() { return qiudui; }
public String getqiuxing() { return qiuxing; }
public int getGrade() { return grade; }
}
public class 尝试1 {
private static final int INFINITE = 1000000000;// 对于该题相当于无穷大
private static shuju[] sj ;// 保存所有平台
public static void main(String[] args) throws IOException{
Scanner sc1 = new Scanner(System.in);
int n;
int zz=1;
n = sc1.nextInt();
//到时候如果有问题看这里
sj=new shuju[n+1];
shuju s =new shuju();
s.setGrade(1);
String filePath = "D:/a/b";
File dir = new File(filePath);
// 一、检查放置文件的文件夹路径是否存在,不存在则创建
if (!dir.exists()) {
dir.mkdirs();// mkdirs创建多级目录
}
File checkFile = new File(filePath + "/filename.txt");
FileWriter writer = null;
//读取文件的地方
// 1.创建文件对象
File fromFile = new File("D:/a/b/filename.txt");
// 2.创建字符输入流
Reader reader = null;
try {
reader = new FileReader(fromFile);
// 3.循环读取(打印)
int content = reader.read();
int t=0;
while (content != -1) {
char aaa=(char)content;
String bb ="";
bb=bb+aaa;
if(aaa==' ')
{
t++;
bb="";
}
if(t==1)
{
while(content != -1)
{
content = reader.read();
aaa=(char)content;
if(aaa==' ')
break;
bb=bb+aaa;
}
s.setleixing(bb);
//System.out.println(s.getleixing());
}
if(t==2)
{
while(content != -1)
{
content = reader.read();
aaa=(char)content;
if(aaa==' ')
break;
bb=bb+aaa;
}
s.setlianmeng(bb);
// System.out.println(s.getlianmeng());
}
if(t==3)
{
while(content != -1)
{
content = reader.read();
aaa=(char)content;
if(aaa==' ')
break;
bb=bb+aaa;
}
s.setqiudui(bb);
// System.out.println(s.getqiudui());
}
if(t==4)
{
while(content != -1)
{
content = reader.read();
aaa=(char)content;
if(aaa==' ')
break;
bb=bb+aaa;
}
s.setqiuxing(bb);
//System.out.println(s.getqiuxing());
t=0;
sj[zz]=s;
/* if(zz==2)
{
System.out.println(zz-1);
System.out.println(sj[zz-1].getleixing());
System.out.println(sj[zz-1].getlianmeng());
System.out.println(sj[zz-1].getqiudui());
System.out.println(sj[zz-1].getqiuxing());
}*/
System.out.println(zz);
System.out.println(sj[zz].getleixing());
System.out.println(sj[zz].getlianmeng());
System.out.println(sj[zz].getqiudui());
System.out.println(sj[zz].getqiuxing());
zz++;
// System.out.println(sj[zz-1].getqiuxing());
}
//System.out.println(bb);
content = reader.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
// 4.关闭流
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取数据写入结构体,这里一直重复读取
for(int j=1;j<zz;j++)
{
System.out.println(j);
System.out.println(sj[j].getleixing());
System.out.println(sj[j].getlianmeng());
System.out.println(sj[j].getqiudui());
System.out.println(sj[j].getqiuxing());
}
}
运行结果及报错内容
这是文件内容