- 字节流与二进制文件
==========================
我的代码
class Student {
private int id;
private String name;
private int age;
private double grade;
public Student() {
}
public Student(int id, String name, int age, double grade) {
this.id = id;
this.setName(name);
this.setAge(age);
this.setGrade(grade);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
if (name.length() > 10) {
throw new IllegalArgumentException("name's length should <=10 " + name.length());
}
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age <= 0) {
throw new IllegalArgumentException("age should >0 " + age);
}
this.age = age;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
if (grade < 0 || grade > 100) {
throw new IllegalArgumentException("grade should be in [0,100] " + grade);
}
this.grade = grade;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
}
}
public class Main {
public static void main(String[] args)
{
String fileName="d:\\student.data";
try(DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName)))
{
Student[] stu=new Student[3];
stu[0]=new Student(1,"zhangsan",19,65.0);
stu[1]=new Student(2,"lisi",19,75.0);
stu[2]=new Student(3,"wangwu",20,85.0);
for(Student stu1:stu) {
dos.writeInt(stu1.getId());
dos.writeUTF(stu1.getName());
dos.writeInt(stu1.getAge());
dos.writeDouble(stu1.getGrade());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("1");
} catch (IOException e) {
e.printStackTrace();
System.out.println("2");
}
try(DataInputStream dis=new DataInputStream(new FileInputStream(fileName)))
{
while(dis!=null) {
int id=dis.readInt();
String name=dis.readUTF();
int age=dis.readInt();
double grade=dis.readDouble();
Student stu=new Student(id,name,age,grade);
System.out.println(stu);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("3");
} catch (IOException e) {
e.printStackTrace();
System.out.println("4");
}
}
我的总结
学会了使用try..with...resouces关闭资源,原来可以用用finally来关闭文件,但也要判断是否可以关闭资源的问题。而用try…with…resource可以解决该问题,当try语句块运行结束时,相应资源会被自动关闭。这是因为FileInputStream 实现了java中的java.lang.AutoCloseable。所有实现了这个接口的类都可以在try-with-resources结构中使用。
- 字符流与文本文件
==========================
我的代码
public class Main {
public static void main(String[] args) throws IOException {
String FileName = "D:\\TSBrowserDownloads\\Students.txt";
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(FileName), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line);
} finally {
if (br != null) {
br.close();
}
}
}
}
public static void ListreadStudents(String fileName){
ArrayList<Student> StudentList=new ArrayList<Student>();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
while(br!=null) {
String line=br.readLine();
String[] stu=line.split("\\s+");
int id=Integer.parseInt(stu[0]);
String name=stu[1];
int age=Integer.parseInt(stu[2]);
double grade=Double.parseDouble(stu[3]);
Student Stu=new Student(id,name,age,grade);
StudentList.add(Stu);
}
} finally{
if (br!=null){
br.close();
}
}
}
我的总结
要以UTF-8打开文件,否则会乱码。BufferedReader只用read和ReadLine方法,得用实验提到的"\\s+"的方法将读到的一整行用分隔符来分开,再用类型转换来读。
2.缓冲流
我的代码
public class test {
@Test
public void test() {
String FILENAME = "test.txt";
long begin = System.currentTimeMillis();
Scanner scanner = null;
try {
scanner = new Scanner(new File(FILENAME));
while (scanner.hasNextLine()) {// 只是读出每一行,不做任何处理
scanner.nextLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
scanner.close();
}
long end = System.currentTimeMillis();
System.out.println("last " + (end - begin));
System.out.println("read using Scanner done");
}
@Test
public void Bufftest() {
String FILENAME = "test.txt";
long begin = System.currentTimeMillis();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(FILENAME)));
while (br.readLine() != null) {
}
;// 只是读出,不进行任何处理
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("last " + (end - begin));
System.out.println("read using BufferedReader done");
}
}
我的总结
缓冲流BufferedReader的方法要比Scanner的方法快很多