求高手解决下面这个题目?关于IO流的。 小弟我定义了一个Student类,然后又定义了一集合类ArrayList,里面存放Student类型的数据 现在小弟我想把ArrayList通过java的输出流保存为 *.txt文件中,如何讲ArrayList保存为*.txt文件 求高手指教》。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个和你用ArrayList没多大关系,一个个取就get(i)就好,至于怎么写入txt,打开一个IO流,用你定义的格式写就好...google例子很多的如果你是存的自己定义的Student类,用范型吧...其实java是伪范型....取的时候直接取就好ArrayList<Student> arrlst = new ArrayList<Student>();arrlst.get(index); 给你个例子import java.io.FileNotFoundException;public class WriteData { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { // TODO 自动生成方法存根 java.io.File file=new java.io.File("score.txt"); if(file.exists()){ System.out.println(file.getAbsolutePath()); System.out.println("File already exists"); System.exit(0); } //Create a file java.io.PrintWriter output=new java.io.PrintWriter(file); //Write formatted output to the file output.print("John T Smith "); output.println(90); output.print("Eric K Jones "); output.println(85); //Close the file output.close(); }} 关键是LZ想怎么保存,是序列化保存,还是自己解析Student的属性信息来保存? 通过 stream 序列化 对象, Student 实现Serializable接口 给个土办法:1、Student类应该是个值对象类吧,定义:public String toString() { return 属性1 + ", " + 属性2 + ", " + 属性3 ... ;}2、打开目标文件:PrintStream out = new PrintStream(new FileOutputStream(new File("C:\\student.txt")));3、循环整个ArrayListout.println(student); package com.wnf.url.test;public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } 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; }} List<Student> stuList=new ArrayList<Student>(); for (int i = 0; i < 100; i++) { Student s=new Student("stu"+i, i); stuList.add(s); }//********************************************************************************** StringBuilder sb=new StringBuilder(); sb.append("name"+"\t"+"age"); sb.append("\n\r"); for (int i = 0; i < stuList.size(); i++) { sb.append(stuList.get(i).getName()+"\t"+stuList.get(i).getAge()); sb.append("\n\r"); } Writer writer=new FileWriter(new File("D:\\student.txt")); writer.write(sb.toString()); writer.close(); } 简单的写了个import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class WriteToTxt { public static void main(String[] args) throws IOException { List<Student> stuList = new ArrayList<Student>(); Student s1 = new Student(1,"王宋",20," 西南交通大学"); Student s2 = new Student(2,"张三",22," 北京大学"); Student s3 = new Student(3,"李四",19," 复旦大学"); Student s4 = new Student(4,"王五",18," 安徽工业大学"); Student s5 = new Student(5,"赵六",22," 哈佛大学"); stuList.add(s1); stuList.add(s2); stuList.add(s3); stuList.add(s4); stuList.add(s5); File studentTxt =new File("student.txt"); PrintStream out = new PrintStream(new FileOutputStream(studentTxt)); for(Iterator iter = stuList.iterator();iter.hasNext();) { String str = ((Student)iter.next()).toString(); byte[] buff = str.getBytes(); out.write(buff); System.out.println(); } }}class Student { private int id; private String name; private int age; private String school; public Student(int id,String name,int age,String school) { this.id = id; this.name = name; this.age = age; this.school = school; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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 String getSchool() { return school; } public void setSchool(String school) { this.school = school; } @Override public String toString() { // TODO Auto-generated method stub return "编号:"+this.id+",姓名:"+this.name+",年龄:"+this.age+",在"+this.school+"读书。"; } } 小弟对java的io流始终处于迷糊的状态,哪位大哥能给点总结的比较好的,条理比较的清晰的笔记分享给小弟。不胜感激!! 这个对IO的讲解:http://blog.csdn.net/gaowen_han/article/details/7163737http://blog.csdn.net/gaowen_han/article/details/7171893 正则表达式问题 java.lang.OutOfMemoryError错误(有代码) 请问,我想用JAVA做个服务器,用VB做客户端,该如何实现? 正则表达式换行问题 想做个程序,希望有人来共同参考一下。 能不能把办证两个字屏蔽了啊 一个简单编译问题 在JAVA中有没有类似Crystal Reports的中间件,哪里可以下载?马上给分 Tread中的join是干什么的 Java中不支持必包,这种情况大家如何处理 请教下各位,怎么用内部类做监视器??? 大专毕业生的工作疑惑
如果你是存的自己定义的Student类,用范型吧...其实java是伪范型....取的时候直接取就好
ArrayList<Student> arrlst = new ArrayList<Student>();
arrlst.get(index);
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO 自动生成方法存根
java.io.File file=new java.io.File("score.txt");
if(file.exists()){
System.out.println(file.getAbsolutePath());
System.out.println("File already exists");
System.exit(0);
}
//Create a file
java.io.PrintWriter output=new java.io.PrintWriter(file);
//Write formatted output to the file
output.print("John T Smith ");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
//Close the file
output.close();
}}
给个土办法:
1、Student类应该是个值对象类吧,定义:
public String toString() {
return 属性1 + ", " + 属性2 + ", " + 属性3 ... ;
}2、打开目标文件:
PrintStream out = new PrintStream(new FileOutputStream(new File("C:\\student.txt")));3、循环整个ArrayList
out.println(student);
package com.wnf.url.test;public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
} List<Student> stuList=new ArrayList<Student>();
for (int i = 0; i < 100; i++) {
Student s=new Student("stu"+i, i);
stuList.add(s);
}
//**********************************************************************************
StringBuilder sb=new StringBuilder();
sb.append("name"+"\t"+"age");
sb.append("\n\r");
for (int i = 0; i < stuList.size(); i++) {
sb.append(stuList.get(i).getName()+"\t"+stuList.get(i).getAge());
sb.append("\n\r");
}
Writer writer=new FileWriter(new File("D:\\student.txt"));
writer.write(sb.toString());
writer.close();
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class WriteToTxt {
public static void main(String[] args) throws IOException {
List<Student> stuList = new ArrayList<Student>();
Student s1 = new Student(1,"王宋",20," 西南交通大学");
Student s2 = new Student(2,"张三",22," 北京大学");
Student s3 = new Student(3,"李四",19," 复旦大学");
Student s4 = new Student(4,"王五",18," 安徽工业大学");
Student s5 = new Student(5,"赵六",22," 哈佛大学");
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
stuList.add(s4);
stuList.add(s5);
File studentTxt =new File("student.txt");
PrintStream out = new PrintStream(new FileOutputStream(studentTxt));
for(Iterator iter = stuList.iterator();iter.hasNext();) {
String str = ((Student)iter.next()).toString();
byte[] buff = str.getBytes();
out.write(buff);
System.out.println();
}
}
}class Student {
private int id;
private String name;
private int age;
private String school;
public Student(int id,String name,int age,String school) {
this.id = id;
this.name = name;
this.age = age;
this.school = school;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
} @Override
public String toString() {
// TODO Auto-generated method stub
return "编号:"+this.id+",姓名:"+this.name+",年龄:"+this.age+",在"+this.school+"读书。";
}
}
http://blog.csdn.net/gaowen_han/article/details/7171893