java--第9章 输入输出流

实验目的:

       1.理解I/O流的概念,掌握其分类。

       2.掌握文本文件读写、二进制文件读写。

实验内容:

        1.编程实现任意文件的复制功能。

        2.利用文件流和缓冲流复制文件的内容。

        3.创建文件和显示文件的名称和内容。

        4.接收键盘输入的字符串并显示在屏幕上。

实验步骤:

1.编写一个程序将字符串“明月松间照,清泉石上流。”写入C:\a.txt中,并实现对该文件的读取。

源代码:

package homework.实验9_输入输出流;

import java.io.BufferedWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class sy9_1 {
   
public static void main(String [] args){
       
Scanner sc=new Scanner(System.in);
       
String s=sc.nextLine();
       
System.out.println("写入字符串 : "+s);
       
try {
           
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\a.txt"));
           
bw.write(s);
           
bw.flush();
           
bw.close(); //关闭流
       
} catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

运行结果截图:

2. 编写一个程序利用字节流将C:\a.txt中的内容复制到另一个文件b.txt中,并提示“文件复制成功。”。

源代码:

package homework.实验9_输入输出流;

import java.io.*;
public class sy9_2{
   
public static void main(String[] args)throws IOException{
       
FileInputStream read = new FileInputStream(new File("D:\\a.txt"));
       
FileOutputStream wr = new FileOutputStream(new File("D:\\b.txt"));
       
byte[] b = new byte[1024];
       
int len = 0;
       
while((len=read.read(b))!=-1){
           
wr.write(b,0,len);
           
wr.flush();
        }
       
wr.close();
       
read.close();
    }
}

 

运行结果截图:

3. 编写一个程序利用字符流将C:\a.txt中的内容复制到另一个文件b.txt中

源代码:

package homework.实验9_输入输出流;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class sy9_2 {

 

           public static void main(String[] args) {

                     // TODO Auto-generated method stub

                     FileReader fr = null;

                     FileWriter fw = null;

                     int c = 0;

                     try {

                                fr = new FileReader("d://a.txt");

                                fw = new FileWriter("d://c.txt");

                                while((c=fr.read())!=-1) {

                                          fw.write(c);

                                }

                                System.out.println("文件复制成功");

                     }catch(FileNotFoundException e) {

                                System.out.println("文件没找到");

                     }catch(IOException e) {

                                System.out.println("输入错误");

                     }finally {

                                try {

                                          fw.close();

                                } catch (IOException e) {

                                          // TODO Auto-generated catch block

                                          e.printStackTrace();

                                }

                                try {

                                          fr.close();

                                } catch (IOException e) {

                                          // TODO Auto-generated catch block

                                          e.printStackTrace();

                                }

                     }

           }

}

4.以下程序实现对象序列化和反序列化,请把程序补充完整。

Student 类:

 

import java.io.Serializable;

public class Student implements Serializable{

     private String name;

     private int age;

     private String address;

     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 getAddress() {

         return       address         ;

     }

     public void setAddress(String address) {

         this.address = address;

     }

}

测试类:

import java.io.*;

public class Test {

     public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {

     Student mStudent1 =new Student();

     mStudent1.setName       ("张三");

     mStudent1.setAge(18);

     mStudent1.setAddress("武汉市");

    

     //ObjectOutputStream代表对象输出流,它的writeObject(Object obj)方法可对参数指定的obj对象进行序列化,

     //把得到的字节序列写到一个目标输出流中,在这里写到文件输出流。

     ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/Student.txt"));

     oos.writeObject(     mStudents1          );

     oos.close();

     System.out.println("序列化完成");

        

     //ObjectInputStream代表对象输入流,它的readObject()方法从一个源输入流中读取字节序列,

     //再把它们反序列化为一个对象,并将其返回。

     ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/Student.txt"));

     Student mStudent2 = (       Student        ) ois.readObject();

     ois.close();  System.out.println(mStudent2.getName()+"\t"+mStudent2.getAge()+"\t"+mStudent2.getAddress());

     }

}

 

 

实验小结

        通过数据流、序列化和文件系统提供系统输入和输出。Java把这些不同来源和目标的数据都统一抽象为数据流。Java语言的输入输出功能是十分强大而灵活的,美中不足的是看上去输入输出的代码并不是很简洁,因为你往往需要包装许多不同的对象。

  1. 序列化和反序列化概念

    把对象转换为字节序列的过程称为对象的序列化。

把字节序列恢复为对象的过程称为对象的反序列化

  对象的序列化主要有两种用途:

  1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;

  2) 在网络上传送对象的字节序列。

 

猜你喜欢

转载自blog.csdn.net/qq_45176548/article/details/112393291