JavaIO流----练习代码!

来源:互联网 发布:java差8小时时区 编辑:程序博客网 时间:2024/06/07 07:51

实体类

public class Person implements Serializable{    private String name;    private int age;    private String sex;    private String context;    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 getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getContext() {        return context;    }    public void setContext(String context) {        this.context = context;    }    @Override    public String toString() {        return "Person [name=" + name + ", age=" + age + ", sex=" + sex                + ", context=" + context + "]";    }    public Person() {        super();        // TODO Auto-generated constructor stub    }    public Person(String name, int age, String sex, String context) {        super();        this.name = name;        this.age = age;        this.sex = sex;        this.context = context;    }}

练习代码

package com.csl.test;import java.io.*;import java.util.Scanner;import org.junit.Test;public class Tests {        @Test        public void show(){            File fileFdisk = new File("F:/");            File[] files = fileFdisk.listFiles();            for (File f : files) {                if(f.isFile()){                    System.out.println(f);                }            }//          String[] files2 = fileFdisk.list();//          for (String s : files2) {//              System.out.println(s);//          }        }        @Test        public void show2() throws IOException{            File f = new File("F:/A.txt");            if(!f.exists()){                f.createNewFile();            }            String sb = new String("hello wolrd! test the IOstream !");             OutputStream os = new FileOutputStream(f, true);            os.write(sb.getBytes());            os.close();        }        @Test        public void show3() throws IOException{            File f = new File("F:/A.html");            if(!f.exists()){                f.createNewFile();            }            InputStream os = new FileInputStream(f);            byte[] sb = new byte[10240];            os.read(sb);            String str = new String(sb);            System.out.println(str);            os.close();        }        @Test        public void show4() throws IOException{            File f = new File("F:/bean.xml");            if(!f.exists()){                f.createNewFile();            }            InputStream is = new FileInputStream(f);            OutputStream os = new FileOutputStream(new File("F:/A.xml"));            byte[] sb = new byte[(int) f.length()];            while(is.read(sb)!=-1){                os.write(sb);            }            is.close();            os.close();        }        @Test        public void show5() throws IOException{            File f = new File("F:/惑.wma");            if(!f.exists()){                f.createNewFile();            }            InputStream is = new FileInputStream(f);            OutputStream os = new FileOutputStream(new File("F:/A.wma"));            byte[] sb = new byte[(int) f.length()];            while(is.read(sb)!=-1){                os.write(sb);            }            is.close();            os.close();        }        @Test        public void show6() throws IOException{            File f = new File("F:/bean.xml");            if(!f.exists()){                f.createNewFile();            }            Reader is = new FileReader(f);            Writer os = new FileWriter(new File("F:/A.xml"));//          char[] sb = new char[(int) f.length()];//          //          is.read(sb);    //          os.write(sb);            int b = 0 ;            while((b=is.read())!=-1){                os.write(b);            }            is.close();            os.close();        }        @Test        public void show7() throws IOException{            File f = new File("F:/B.txt");            if(!f.exists()){                f.createNewFile();            }            PrintStream ps = new PrintStream(f);            Person p = new Person("Chen", 23, "男", "一些测试内容!");            Person p1 = new Person("Jhen", 27, "女", "一些测试内容!");            ps.print(p);            ps.print(p1);            ps.close();        }        @Test        public void show7_1() throws IOException, ClassNotFoundException{            File f = new File("F:/D.txt");            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));            Person[] p  =  (Person[]) ois.readObject();            for (Person per : p) {                System.out.println(per.getName());            }        }        @Test        public void show7_2() throws IOException, ClassNotFoundException{            File f = new File("F:/D.txt");            f.createNewFile();            Person[] per = {new Person("Chen", 23, "男", "一些测试内容!"),new Person("Jhen", 27, "女", "一些测试内容!")};            ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream(f));            ois.writeObject(per);            ois.close();        }        @Test        public void show8() throws IOException{            OutputStream os = System.out;            os.write("12312313132".getBytes());            os.close();        }        @Test        public void show9() throws IOException{            File f = new File("F:/bean.xml");            if(!f.exists()){                f.createNewFile();            }            Scanner scan = new Scanner(f);            while(scan.hasNext()){                System.out.println(scan.nextLine());            }        }        @Test        public void show10() throws IOException{            File f = new File("F:/bean.xml");            File f1 = new File("F:/A.txt");            File f3 = new File("F:/C.txt");            f3.createNewFile();            InputStream is1 = new FileInputStream(f);            InputStream is2 = new FileInputStream(f1);            OutputStream os = new FileOutputStream(f3);            SequenceInputStream sis = new SequenceInputStream(is2, is1);            int b = 0 ;            int a = 0 ;            while((b=sis.read())!=-1){//              os.write(b);                System.out.println(b);                a++;            }            System.out.println(a);            is1.close();            is2.close();            sis.close();            os.close();        }        @Test        public void show11() throws IOException{            System.out.println(System.getProperty("file.encoding"));        }}
原创粉丝点击