java中怎样从文件里读取数据,然后赋值给对象数组
代码:Filefile=newFile("data.txt");BufferedReaderin=newBufferedReader(newFileReader(file...
代码:File file = new File( "data.txt ");
BufferedReader in = new BufferedReader(new FileReader(file));
for(int i=0;i <200;i++){
String line;
int flg = 1;
while ((line = in.readLine()) != null){
System.out.println(line);
}
}
in.close();
in=null;
file=null;
文件(txt):
zhang 2001 os:89.0 java:78.0 math:89.5
wang 2002 os:89.0 java:78.0 math:89.5
li 2003 os:89.0 java:78.0 math:89.5
对象数组的构造方法:public Student0(String name,Long id,double result[]){
this.name = name;
this.id = id;
this.result = result;
}
其中 result数组存放成绩
求怎样取值,怎样赋值.....
坐等高人。。 展开
BufferedReader in = new BufferedReader(new FileReader(file));
for(int i=0;i <200;i++){
String line;
int flg = 1;
while ((line = in.readLine()) != null){
System.out.println(line);
}
}
in.close();
in=null;
file=null;
文件(txt):
zhang 2001 os:89.0 java:78.0 math:89.5
wang 2002 os:89.0 java:78.0 math:89.5
li 2003 os:89.0 java:78.0 math:89.5
对象数组的构造方法:public Student0(String name,Long id,double result[]){
this.name = name;
this.id = id;
this.result = result;
}
其中 result数组存放成绩
求怎样取值,怎样赋值.....
坐等高人。。 展开
2个回答
展开全部
大体的实现功能,代码写的并不严谨,很多需要判断的地方都没判断,你凑合看看功能是如何实现的就行,至于各种判断验证什么的你自己搞定吧。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileToArray {
public static void main(String[] args) throws IOException {
String path = FileToArray.class.getResource("data.txt").toString()
.substring(6);
File file = new File(path);
BufferedReader in = new BufferedReader(new FileReader(file));
List<Student0> students = new ArrayList<Student0>();
String line;
int flg = 1;
while ((line = in.readLine()) != null) {
String[] data = line.split(" ");
String name = data[0];
long id = Long.parseLong(data[1]);
String[] result = new String[data.length - 2];
for (int i=2; i<data.length; i++) {
result[i-2] = data[i];
}
Student0 sdt = new Student0(name, id, result);
students.add(sdt);
}
in.close();
in = null;
file = null;
for (Student0 std : students) {
System.out.println(std.getName() + " " + std.getId());
for (String res : std.getResult()) {
System.out.println(res);
}
System.out.println("=================");
}
}
}
class Student0 {
private String name;
private long id;
private String[] result;
public Student0(String name, long id, String[] result) {
this.name = name;
this.id = id;
this.result = result;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String[] getResult() {
return result;
}
public void setResult(String[] result) {
this.result = result;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileToArray {
public static void main(String[] args) throws IOException {
String path = FileToArray.class.getResource("data.txt").toString()
.substring(6);
File file = new File(path);
BufferedReader in = new BufferedReader(new FileReader(file));
List<Student0> students = new ArrayList<Student0>();
String line;
int flg = 1;
while ((line = in.readLine()) != null) {
String[] data = line.split(" ");
String name = data[0];
long id = Long.parseLong(data[1]);
String[] result = new String[data.length - 2];
for (int i=2; i<data.length; i++) {
result[i-2] = data[i];
}
Student0 sdt = new Student0(name, id, result);
students.add(sdt);
}
in.close();
in = null;
file = null;
for (Student0 std : students) {
System.out.println(std.getName() + " " + std.getId());
for (String res : std.getResult()) {
System.out.println(res);
}
System.out.println("=================");
}
}
}
class Student0 {
private String name;
private long id;
private String[] result;
public Student0(String name, long id, String[] result) {
this.name = name;
this.id = id;
this.result = result;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String[] getResult() {
return result;
}
public void setResult(String[] result) {
this.result = result;
}
}
展开全部
不是很明白你的要求
for(int i=0;i <200;i++){特别是对于 这行,不明白
这个循环
while ((line = in.readLine()) != null){
System.out.println(line);
}
已经把文件从头读到尾了,你的循环是用来做什么的?
读取的数据就在System.out.println(line);
那里处理,不明白你的String name,Long id,double result[]怎么对应,所以不敢写,你对应起来再说吧(不明白id对应哪个,name对应哪个,result是不是对应分数?)
for(int i=0;i <200;i++){特别是对于 这行,不明白
这个循环
while ((line = in.readLine()) != null){
System.out.println(line);
}
已经把文件从头读到尾了,你的循环是用来做什么的?
读取的数据就在System.out.println(line);
那里处理,不明白你的String name,Long id,double result[]怎么对应,所以不敢写,你对应起来再说吧(不明白id对应哪个,name对应哪个,result是不是对应分数?)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |