zoukankan      html  css  js  c++  java
  • IO—Data

    DataOuputStream类:继承了OutputStream类,利用其可以操作基本数据类型
      构造函数:
        DataOutputStream(OutputStream out) 创建一个新的数据输出流,将数据写入指定基础输出流。
      常用的方法:
        writeXxx(Xxx x):Xxx为数据类型:将一个 x值写出到基础输出流中。
        writeByte(byte b) writeInt(int i) writeDouble(Double d)
        writeUTF(String str):以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。

    public class TestDataOutputStream {
        public static void main(String[] args) {
            try {
                FileOutputStream fos = new FileOutputStream("D:/ttt.txt");
                DataOutputStream dos = new DataOutputStream(fos);
                dos.writeInt(10);
                dos.writeDouble(3.14);
                dos.writeChar('A');
                dos.writeUTF("你好");
                dos.flush();
                
                dos.close();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }

    DataInputStream类:继承了InputStream类,利用其可以操作基本数据类型
      构造函数:
        DataInputStream(InputStream in) 创建一个新的数据输入流,将数据读入指定基础输入流。
      常用的方法:
        readXxx():Xxx为数据类型:将一个 x值写出到基础输出流中。
        readInt() readDouble() readChar()...
        readUTF():从包含的输入流中读取此操作需要的字节。

    public class TestDataInputStream {
        public static void main(String[] args) {
            try {
                FileInputStream fis = new FileInputStream("D:/ttt.txt");
                DataInputStream dis = new DataInputStream(fis);
                int num = dis.readInt();
                double pi = dis.readDouble();
                char c = dis.readChar();
                String str = dis.readUTF();
                System.out.println(num);
                System.out.println(pi);
                System.out.println(c);
                System.out.println(str);
                dis.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }

    练习:

    /**
     * 1.使用输入流读取标准答案和每个学生的答案
     * 2.比较标准答案和学生答案进行判卷,并统计学员分数
     * 3.将学员名称和分数输出到指定的文件中
     * 技能点:
     * File 类
     * FileReader/BufferedReader
     * FileWriter/BufferedWriter
     * String 类
     *     equals方法:比较内容是否相同
     *     substring(int begin,int end):字符串截取[begin,end)
     *     indexOf(String s):索引指定字符第一次出现的下标位置
     */
    public class Homework2 {
        public static void main(String[] args) {
            try {
                File answerFile = new File("D:/test/answer/answer.txt");//保存标准答案的文件
                File stusFile = new File("D:/test");//保存学生答案的文件夹 
                File resultFile = new File("D:/test/result/result.txt");//保存答案的文件
                FileWriter fw = new FileWriter(resultFile);
                BufferedWriter bw = new BufferedWriter(fw);
                //获取学生答案
                File[] stus = stusFile.listFiles();
                for (File file : stus) {
                    int score=0;//统计学生分数
                    if(file.isFile()){//判断是否为文件
                        FileReader anwserFr = new FileReader(answerFile);
                        BufferedReader answerBr = new BufferedReader(anwserFr);
                        String studentName = file.getName();
                        String name = studentName.substring(0,studentName.indexOf("."));
                        FileReader stuFr = new FileReader(file);
                        BufferedReader stuBr = new BufferedReader(stuFr);
                        String stuAnswer;
                        while((stuAnswer=stuBr.readLine())!=null){//读取学生答案
                            String answer = answerBr.readLine();//读取标准答案
    //                        System.out.println(stuAnswer+"	"+answer);
                            if(answer.equals(stuAnswer)){
                                score+=10;//计算学生得分
                            }
                        }
                        //将学生的成绩和姓名写入到result.txt中
                        bw.write(name+"	"+score);
                        bw.newLine();
                        bw.flush();
                        //释放资源
                        stuBr.close();
                        stuFr.close();
                        answerBr.close();
                        anwserFr.close();
                    }
                }
                bw.close();
                fw.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }

    练习—文件复制

    /**
     *完成文件的夹的复制(该文件下的所有文件或文件夹)
     *技能:流+递归+文件的操作
     *1.文件夹的复制(创建)
     *2.文件夹下文件的复制(IO的操作)
     * 流:字节流
     * 
     *
     */
    public class Homework1 {
        /**
         * 文件的复制
         * @param sourceFile:源文件 D:/mp3/aa.mp3
         * @param destFile:目标文件 F:/mp3/aa.mp3
         */
        public static void copyFile(File sourceFile,File destFile){
            FileInputStream fis =null;
            FileOutputStream fos=null;
            BufferedInputStream bis =null;
            BufferedOutputStream bos=null;
            try {
                fis = new FileInputStream(sourceFile);
                fos = new FileOutputStream(destFile);
                bis = new BufferedInputStream(fis);
                bos = new BufferedOutputStream(fos);
                byte[] bs = new byte[1024];
                int len;
                while((len=bis.read(bs))!=-1){
                    bos.write(bs, 0, len);
                }
                bos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    bos.close();
                    fos.close();
                    bis.close();
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
        }
        /**
         * 文件夹的复制
         * @param sourceDir:源文件夹
         * @param destDir:目标文件夹
         */
        public static void copyDir(File sourceDir,File destDir){
            //获取源文件夹的名称
            String sourceName=  sourceDir.getName();
            //在目标文件夹中创建目录
            File file = new File(destDir,sourceName);
            file.mkdirs();//创建文件夹
            //获取源文件夹中的所有子文件(子目录)
            File[] sourceFiles = sourceDir.listFiles();
            for (File sourceFile : sourceFiles) {
                //判断该文件是否为目录
                if(sourceFile.isDirectory()){
                    copyDir(sourceFile,file);
                }
                if(sourceFile.isFile()){
                    File newFile = new File(file,sourceFile.getName());
                    //复制文件到目标文件夹中
                    copyFile(sourceFile,newFile);
                }
            }
        }
        
        public static void main(String[] args) {
            File sourceDir = new File("D:/mp3");
            File desctDir = new File("F:/");
            copyDir(sourceDir,desctDir);
        }
    }
  • 相关阅读:
    dock 安装部署和初级管理命令
    3 gogs+jenkins 触发远程构建及tomcat管理war包替换
    2 gitlab+jenkins maven自动打包更新
    1 gitlab+jenkins 自动化部署 持续集成
    Day7 面向对象进阶
    Day 6 面向对象初级
    day4 迭代器&生成器&递归&json&正则
    day5 常用模块
    day3 函数
    day2 数据及文件操作
  • 原文地址:https://www.cnblogs.com/fwdsz/p/6792425.html
Copyright © 2011-2022 走看看