IO流练习

来源:互联网 发布:unity3d cg动画 编辑:程序博客网 时间:2024/05/22 14:42

练习题

01.字符流复制文本文件的5种方式
    public class io_testDemo {    public static void main(String[] args)throws IOException {        String srcfile="a.txt";        String destfile="d.txt";        //method1(srcfile,destfile);        //method2(srcfile, destfile);        //method3(srcfile, destfile);        //method4(srcfile, destfile);        method5(srcfile, destfile);    }    private static void method5(String srcfile, String destfile) throws IOException {        // TODO Auto-generated method stub        BufferedReader br=new BufferedReader(new FileReader(srcfile));        BufferedWriter bWriter=new BufferedWriter(new FileWriter(destfile));        String line=null;        while((line=br.readLine())!=null){            bWriter.write(line);            bWriter.newLine();            bWriter.flush();        }        br.close();        bWriter.close();    }    private static void method2(String srcfile, String destfile) throws IOException {        // TODO Auto-generated method stub        FileReader fr=new FileReader(srcfile);        FileWriter fw=new FileWriter(destfile);        int len=0;        char[] chs=new char[1024];        while((len=fr.read(chs))!=-1){            fw.write(chs, 0, len);        }        fr.close();        fw.close();    }    private static void method1(String srcfile, String destfile) throws IOException {        // TODO Auto-generated method stub        FileReader fr=new FileReader(srcfile);        FileWriter fw=new FileWriter(destfile);        int ch=0;        while((ch=fr.read())!=0){            fw.write((char)ch);        }        fr.close();        fw.close();    }    private static void method3(String srcfile, String destfile) throws IOException {        // TODO Auto-generated method stub        BufferedReader fr=new BufferedReader(new FileReader(srcfile));        BufferedWriter fw=new BufferedWriter(new FileWriter(destfile));        int len=0;        char[] chs=new char[1024];        while((len=fr.read(chs))!=-1){            fw.write(chs, 0, len);        }        fr.close();        fw.close();    }    private static void method4(String srcfile, String destfile) throws IOException {        // TODO Auto-generated method stub        BufferedReader fr=new BufferedReader(new FileReader(srcfile));        BufferedWriter fw=new BufferedWriter(new FileWriter(destfile));        int ch=0;        while((ch=fr.read())!=0){            fw.write((char)ch);        }        fr.close();        fw.close();    }}
02.复制图片
    public class CopyImageDemo {    public static void main(String[] args) throws IOException {        // 使用字符串作为路径        // String srcString = "c:\\a.jpg";        // String destString = "d:\\b.jpg";        // 使用File对象做为参数        File srcFile = new File("c:\\a.jpg");        File destFile = new File("d:\\b.jpg");        // method1(srcFile, destFile);        // method2(srcFile, destFile);        // method3(srcFile, destFile);        method4(srcFile, destFile);    }    // 字节缓冲流一次读写一个字节数组    private static void method4(File srcFile, File destFile) throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                srcFile));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream(destFile));        byte[] bys = new byte[1024];        int len = 0;        while ((len = bis.read(bys)) != -1) {            bos.write(bys, 0, len);        }        bos.close();        bis.close();    }    // 字节缓冲流一次读写一个字节    private static void method3(File srcFile, File destFile) throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                srcFile));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream(destFile));        int by = 0;        while ((by = bis.read()) != -1) {            bos.write(by);        }        bos.close();        bis.close();    }    // 基本字节流一次读写一个字节数组    private static void method2(File srcFile, File destFile) throws IOException {        FileInputStream fis = new FileInputStream(srcFile);        FileOutputStream fos = new FileOutputStream(destFile);        byte[] bys = new byte[1024];        int len = 0;        while ((len = fis.read(bys)) != -1) {            fos.write(bys, 0, len);        }        fos.close();        fis.close();    }    // 基本字节流一次读写一个字节    private static void method1(File srcFile, File destFile) throws IOException {        FileInputStream fis = new FileInputStream(srcFile);        FileOutputStream fos = new FileOutputStream(destFile);        int by = 0;        while ((by = fis.read()) != -1) {            fos.write(by);        }        fos.close();        fis.close();    }}
 03.需求:把ArrayList集合中的字符串数据存储到文本文件
   public class ArrayListToFileDemo {    public static void main(String[] args) throws IOException {        // 封装数据与(创建集合对象)        ArrayList<String> array = new ArrayList<String>();        array.add("hello");        array.add("world");        array.add("java");        // 封装目的地        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));        // 遍历集合        for (String s : array) {            // 写数据            bw.write(s);            bw.newLine();            bw.flush();        }        // 释放资源        bw.close();    }}
 04.需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
    public static void main(String[] args) throws IOException {        // 封装数据源        BufferedReader br = new BufferedReader(new FileReader("b.txt"));        // 封装目的地(创建集合对象)        ArrayList<String> array = new ArrayList<String>();        // 读取数据存储到集合中        String line = null;        while ((line = br.readLine()) != null) {            array.add(line);        }        // 释放资源        br.close();        // 遍历集合        for (String s : array) {            System.out.println(s);        }    }}
   05. 需求:我有一个文本文件中存储了几个名称,请大家写一个程序实现随机获取一个人的名字。
    public class GetName {    public static void main(String[] args) throws IOException {        // 把文本文件中的数据存储到集合中        BufferedReader br = new BufferedReader(new FileReader("b.txt"));        ArrayList<String> array = new ArrayList<String>();        String line = null;        while ((line = br.readLine()) != null) {            array.add(line);        }        br.close();        // 随机产生一个索引        Random r = new Random();        int index = r.nextInt(array.size());        // 根据该索引获取一个值        String name = array.get(index);        System.out.println("该幸运者是:" + name);    }}
 06. 需求:复制单极文件夹
public class CopyFolderDemo {    public static void main(String[] args) throws IOException {        // 封装目录        File srcFolder = new File("e:\\demo");        // 封装目的地        File destFolder = new File("e:\\test");        // 如果目的地文件夹不存在,就创建        if (!destFolder.exists()) {            destFolder.mkdir();        }        // 获取该目录下的所有文本的File数组        File[] fileArray = srcFolder.listFiles();        // 遍历该File数组,得到每一个File对象        for (File file : fileArray) {            // System.out.println(file);            // 数据源:e:\\demo\\e.mp3            // 目的地:e:\\test\\e.mp3            String name = file.getName(); // e.mp3            File newFile = new File(destFolder, name); // e:\\test\\e.mp3            copyFile(file, newFile);        }    }    private static void copyFile(File file, File newFile) throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                file));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream(newFile));        byte[] bys = new byte[1024];        int len = 0;        while ((len = bis.read(bys)) != -1) {            bos.write(bys, 0, len);        }        bos.close();        bis.close();    }}
07.需求:复制指定目录下的指定文件,并修改后缀名。  指定的文件是:.java文件。  指定的后缀名是:.jad  指定的目录是:jad
public class CopyFolderDemo {    public static void main(String[] args) throws IOException {        // 封装目录        File srcFolder = new File("e:\\java");        // 封装目的地        File destFolder = new File("e:\\jad");        // 如果目的地目录不存在,就创建        if (!destFolder.exists()) {            destFolder.mkdir();        }        // 获取该目录下的java文件的File数组        File[] fileArray = srcFolder.listFiles(new FilenameFilter() {            @Override            public boolean accept(File dir, String name) {                return new File(dir, name).isFile() && name.endsWith(".java");            }        });        // 遍历该File数组,得到每一个File对象        for (File file : fileArray) {            // System.out.println(file);            // 数据源:e:\java\DataTypeDemo.java            // 目的地:e:\\jad\DataTypeDemo.java            String name = file.getName();            File newFile = new File(destFolder, name);            copyFile(file, newFile);        }        // 在目的地目录下改名        File[] destFileArray = destFolder.listFiles();        for (File destFile : destFileArray) {            // System.out.println(destFile);            // e:\jad\DataTypeDemo.java            // e:\\jad\\DataTypeDemo.jad            String name =destFile.getName(); //DataTypeDemo.java            String newName = name.replace(".java", ".jad");//DataTypeDemo.jad            File newFile = new File(destFolder,newName);            destFile.renameTo(newFile);        }    }    private static void copyFile(File file, File newFile) throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                file));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream(newFile));        byte[] bys = new byte[1024];        int len = 0;        while ((len = bis.read(bys)) != -1) {            bos.write(bys, 0, len);        }        bos.close();        bis.close();    }}
08.需求:复制多极文件夹
  public class CopyFoldersDemo {    public static void main(String[] args) throws IOException {        // 封装数据源File        File srcFile = new File("E:\\JavaSE\\day21\\code\\demos");        // 封装目的地File        File destFile = new File("E:\\");        // 复制文件夹的功能        copyFolder(srcFile, destFile);    }    private static void copyFolder(File srcFile, File destFile)            throws IOException {        // 判断该File是文件夹还是文件        if (srcFile.isDirectory()) {            // 文件夹            File newFolder = new File(destFile, srcFile.getName());            newFolder.mkdir();            // 获取该File对象下的所有文件或者文件夹File对象            File[] fileArray = srcFile.listFiles();            for (File file : fileArray) {                copyFolder(file, newFolder);            }        } else {            // 文件            File newFile = new File(destFile, srcFile.getName());            copyFile(srcFile, newFile);        }    }    private static void copyFile(File srcFile, File newFile) throws IOException {        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(                srcFile));        BufferedOutputStream bos = new BufferedOutputStream(                new FileOutputStream(newFile));        byte[] bys = new byte[1024];        int len = 0;        while ((len = bis.read(bys)) != -1) {            bos.write(bys, 0, len);        }        bos.close();        bis.close();    }}
09.键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
    public class Student {    // 姓名    private String name;    // 语文成绩    private int chinese;    // 数学成绩    private int math;    // 英语成绩    private int english;    public Student() {        super();    }    public Student(String name, int chinese, int math, int english) {        super();        this.name = name;        this.chinese = chinese;        this.math = math;        this.english = english;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getChinese() {        return chinese;    }    public void setChinese(int chinese) {        this.chinese = chinese;    }    public int getMath() {        return math;    }    public void setMath(int math) {        this.math = math;    }    public int getEnglish() {        return english;    }    public void setEnglish(int english) {        this.english = english;    }    public int getSum() {        return this.chinese + this.math + this.english;    }}
public class StudentDemo {    public static void main(String[] args) throws IOException {        // 创建集合对象        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {            @Override            public int compare(Student s1, Student s2) {                int num = s2.getSum() - s1.getSum();                int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;                int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;                int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;                int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName())                        : num4;                return num5;            }        });        // 键盘录入学生信息存储到集合        for (int x = 1; x <= 5; x++) {            Scanner sc = new Scanner(System.in);            System.out.println("请录入第" + x + "个的学习信息");            System.out.println("姓名:");            String name = sc.nextLine();            System.out.println("语文成绩:");            int chinese = sc.nextInt();            System.out.println("数学成绩:");            int math = sc.nextInt();            System.out.println("英语成绩:");            int english = sc.nextInt();            // 创建学生对象            Student s = new Student();            s.setName(name);            s.setChinese(chinese);            s.setMath(math);            s.setEnglish(english);            // 把学生信息添加到集合            ts.add(s);        }        // 遍历集合,把数据写到文本文件        BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));        bw.write("学生信息如下:");        bw.newLine();        bw.flush();        bw.write("姓名,语文成绩,数学成绩,英语成绩");        bw.newLine();        bw.flush();        for (Student s : ts) {            StringBuilder sb = new StringBuilder();            sb.append(s.getName()).append(",").append(s.getChinese())                    .append(",").append(s.getMath()).append(",")                    .append(s.getEnglish());            bw.write(sb.toString());            bw.newLine();            bw.flush();        }        // 释放资源        bw.close();        System.out.println("学习信息存储完毕");    }}
原创粉丝点击