java里如何从一个文件中读取多条记录
在java中,如何读取多条记录(记录格式例如name,age,wage),读入后如何能实现插入新的记录和原来的记录一起保存到原来的文件中?java新人。。求助~~...
在java中,如何读取多条记录(记录格式例如name,age,wage),读入后如何能实现插入新的记录和原来的记录一起保存到原来的文件中?
java新人。。求助~~ 展开
java新人。。求助~~ 展开
2个回答
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SimRecorder {
public List<Worker> kenWorkers(File data) {
List<Worker> workers = new ArrayList<Worker>();
try {
BufferedReader san = new BufferedReader(new FileReader(data));
String line = null;
while ((line = san.readLine()) != null) {
String[] args = line.split(",");
if (args.length >= 3) {
try {
Worker worker = new Worker(args[0], Integer
.parseInt(args[1].trim()), Integer
.parseInt(args[2].trim()));
workers.add(worker);
} catch (Exception e) {
System.err.println("参数有误!");
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return workers;
}
public boolean save(List<Worker> workers, File data) {
StringBuffer cons = new StringBuffer();
for (Worker o : workers) {
cons.append(o.getName() + "," + o.getAge() + "," + o.getWage()
+ "\r\n");
}
try {
FileWriter writer = new FileWriter(data);
writer.write(cons.toString());
writer.flush();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void list(List<?> objs) {
for (Object o : objs) {
System.out.println(o);
}
}
public SimRecorder() {
}
/**
* data.txt 数据存储格式如下:
* tiger,25,3500
* jim,22,3600
* slim,26,5800
*
*/
public static void main(String[] args) {
String datas = "E:\\JProgram\\workhome\\jweb\\simp\\src\\text\\data.txt";
File data = new File(datas);
SimRecorder recorder = new SimRecorder();
List<Worker> workers = recorder.kenWorkers(data);
SimRecorder.list(workers);
Worker worker = new Worker("new added", 25, 5000);
workers.add(worker);
recorder.save(workers, data);
}
}
class Worker {
private String name;
private int age;
private int wage;
public Worker() {
}
public Worker(String name, int age, int wage) {
super();
this.name = name;
this.age = age;
this.wage = wage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWage() {
return wage;
}
public void setWage(int wage) {
this.wage = wage;
}
public String toString() {
String ws = "**************" + "\nNAME: " + this.getName() + "\nAGE: "
+ this.getAge() + "\nWAGE: " + this.getWage();
return ws;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SimRecorder {
public List<Worker> kenWorkers(File data) {
List<Worker> workers = new ArrayList<Worker>();
try {
BufferedReader san = new BufferedReader(new FileReader(data));
String line = null;
while ((line = san.readLine()) != null) {
String[] args = line.split(",");
if (args.length >= 3) {
try {
Worker worker = new Worker(args[0], Integer
.parseInt(args[1].trim()), Integer
.parseInt(args[2].trim()));
workers.add(worker);
} catch (Exception e) {
System.err.println("参数有误!");
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return workers;
}
public boolean save(List<Worker> workers, File data) {
StringBuffer cons = new StringBuffer();
for (Worker o : workers) {
cons.append(o.getName() + "," + o.getAge() + "," + o.getWage()
+ "\r\n");
}
try {
FileWriter writer = new FileWriter(data);
writer.write(cons.toString());
writer.flush();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void list(List<?> objs) {
for (Object o : objs) {
System.out.println(o);
}
}
public SimRecorder() {
}
/**
* data.txt 数据存储格式如下:
* tiger,25,3500
* jim,22,3600
* slim,26,5800
*
*/
public static void main(String[] args) {
String datas = "E:\\JProgram\\workhome\\jweb\\simp\\src\\text\\data.txt";
File data = new File(datas);
SimRecorder recorder = new SimRecorder();
List<Worker> workers = recorder.kenWorkers(data);
SimRecorder.list(workers);
Worker worker = new Worker("new added", 25, 5000);
workers.add(worker);
recorder.save(workers, data);
}
}
class Worker {
private String name;
private int age;
private int wage;
public Worker() {
}
public Worker(String name, int age, int wage) {
super();
this.name = name;
this.age = age;
this.wage = wage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWage() {
return wage;
}
public void setWage(int wage) {
this.wage = wage;
}
public String toString() {
String ws = "**************" + "\nNAME: " + this.getName() + "\nAGE: "
+ this.getAge() + "\nWAGE: " + this.getWage();
return ws;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询