zoukankan      html  css  js  c++  java
  • 第13次抽考(IO流)

    1、将文本文件a.txt 复制成 b.txt。要求:
    a. 用逐个字符复制方式;
    b. 用逐行读写方式;
    c. 用字符数组方式
    2、将压缩包a.rar复制成b.rar。
    注意:复制前后手工打开文件,若复制后无法打开,则说明复制文件有误。
    3、设计一个单链表,配备基本的创建、输出等操作。通过序列化机制将其写入文件,再通过反序列化机制从文件中读出。在序列化前/后输出链表内容。

    一.

    1.a:

    package week4;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    //将文本文件a.txt复制成b.txt
    public class Work1 {
        public static void main(String[] args) throws IOException
        {
          File a=new File("a.txt");    
          //a.createNewFile();
          File b =new File("b.txt");
          //b.createNewFile();
         //文本文件用FileReader/FileWriter
         try {
            FileReader in =new FileReader(a);//这里就像一个水管接上了a.txt,a.txt像水龙头,当我们要read的时候就会放水
            FileWriter out =new FileWriter(b);//这里一个水管接上了b.txt,b.txt像池子当我们要write的时候就会将水倒入池子中
            int ch;//这里是ASCILL码的值
            while((ch=in.read())!=-1)
            {
                out.write(ch);
            }
            in.close();
            out.close();
            System.out.println("复制完成");
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e)
         {
            e.printStackTrace();
         }
         
        }
    }

    1.b:

    package week4;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    //采用缓冲区进行逐行读取
    public class Work1 {
        public static void main(String[] args) throws IOException
        {
          //创建文件
            File a=new File("a.txt");
            File b=new File("b.txt");
            //接入水管和池子
            FileReader in =new FileReader(a);
            FileWriter out=new FileWriter(b);
            //用BufferedReader包装,成为一个缓冲区
            BufferedReader b_in=new BufferedReader(in);
            BufferedWriter b_out=new BufferedWriter(out);
            System.out.println("开始文件复制");
            String s;
            while((s=b_in.readLine())!=null)
            {
                b_out.write(s,0,s.length());
                b_out.newLine(); 
            }
            b_in.close();
            b_out.close();
            System.out.println("结束文件复制");
         
         
        }
    }

    1.c

    package week4;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    //采用字符数组来作为缓冲区来复制文件
    public class Work1 {
        public static void main(String[] args) throws IOException
        {
            //创建文件对象
          File a=new File("a.txt");
          File b=new File("b.txt");
          char[] ca=new char[(int)a.length()];
          FileReader in=new FileReader(a);
          FileWriter out=new FileWriter(b);
          System.out.println("开始文件复制");
          in.read(ca);
          out.write(ca);
          in.close();
          out.close();
          System.out.println("文件复制结束");
        }
    }

    2.复制压缩包

    package week4;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    //使用字节流复制压缩包文件
    public class Work1 {
        public static void main(String[] args) throws IOException
        {
            File a=new File("a.zip");
            File b=new File("b.zip");
            FileInputStream in =new FileInputStream(a);
            FileOutputStream out=new FileOutputStream(b);
            int ch;
            while((ch=in.read())!=-1)
            {
                out.write(ch);
            }
            System.out.println("复制完成");
        
        }
    }

    3.

    package week4;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.Scanner;
    //序列化单链表,只有实现Serializable接口,才能序列化,transient修饰的不会被序列化
    public class Work1 {
        public static void main(String[] args) throws IOException
        {
            Node a=new Node(0);
            a.create();
            a.print();
            //先将对象写入输出流,恰好相反
            FileOutputStream fo=new FileOutputStream("serialObj.dat");
            ObjectOutputStream obj_o=new ObjectOutputStream(fo);
            obj_o.writeObject(obj_o);
            obj_o.close();
            //从dat文件中读取
            FileInputStream fi=new FileInputStream("serialObj.dat");
            ObjectInputStream obj_i=new ObjectInputStream(fi);
            try {
                Node b=(Node)obj_i.readObject();
                obj_i.close();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }
    class Node implements Serializable
    {
        Node next;
        int value;
        public Node(int value)
        {
            this.value=value;
        }
        //创建单链表
        public void create()
        {
            int tmpValue; 
            Scanner sc=new Scanner(System.in);
            Node p=this;
            while((tmpValue=sc.nextInt())!=-1)
            {
                Node n=new Node(tmpValue);
                p.next=n;
                p=n;
            }
            p.next=null;
            System.out.println("创建成功");
    
        }
        //打印单链表
        public void print()
        {
            Node p=this;
            while(p!=null)
            {
                System.out.println(p.value);
                p=p.next;
            }
        }
    }
     
  • 相关阅读:
    如何编写一个简单的依赖注入容器
    网站框架的动态编译的实现原理
    Python实现语音识别和语音合成
    OpenCV图像处理以及人脸识别
    Shell教程快速入门
    TensorFlow入门教程
    如何免费使用Google的GPU跑深度学习代码
    高等数学系列文章
    Git系列教程
    一键构造你的博客园目录
  • 原文地址:https://www.cnblogs.com/YenKoc/p/13124738.html
Copyright © 2011-2022 走看看