zoukankan      html  css  js  c++  java
  • IO流,Scanner和File对象的方法

    Java数据IO流

    IO流

    IO流就是input和output数据流向,向文件中写入数据和读取数据。

    1. 输入输出流。
    2. 字节字符流
    3. 包装流。
    import com.example.bean.Person;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    public class TestIO {
        public void IO() {
    //        复制文件
            File oldFile = new File("D:/awork/oldFile.txt");
            File newFile = new File("D:/newFile1.txt");
    
    //        创建输入输出流
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                Date d1 = new Date();
                fis = new FileInputStream(oldFile);
                fos = new FileOutputStream(newFile);
    
    //        将输入流里面的数据导入到输出流中
    //        字节输入输出流
                byte[] b = new byte[1024*1024];
                int index=-1;
                while((index=fis.read(b))!=-1){
                    fos.write(b,0,index);
                    System.out.println();
                }
                Date d2 = new Date();
                fos.flush();
                System.out.println("复制完成!耗时:"+(d2.getTime()-d1.getTime())+"毫秒");
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try{
                    if (fis!=null){
                        fis.close();
                    }
                    if (fos!=null){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
    //      字符流输入输出流
            File oldFile1 = new File("D:/awork/oldFile.txt");
            File newFile1 = new File("D:/newFile1.txt");
    //        创建字符输入输出流
            FileReader fr=null;
            FileWriter fw=null;
    
            try {
                fr=new FileReader(oldFile1);
                fw=new FileWriter(newFile1);
    //            通过输入流获取文件数据,通过输出流把数据写到文件中
                char[] c = new char[1024];
                int len = -1;
                while((len=fr.read(c))!=-1){
                    fw.write(c,0,len);
                }
    //        刷新
                fw.flush();
                System.out.println("复制完成");
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if (fr!=null) {
                        fr.close();
                    }
                    if (fw!=null){
                        fw.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            /*
            * 包装流(过滤流):在基本流的基础上提供了一些特殊的概念
            *BufferedReader:一行一行读的字符包装类
            * PrintWrite:一行一行写的字符包装类
            * */
    
            File oldFile2 = new File("D:/awork/oldFile.txt");
            File newFile2 = new File("D:/awork/newFile.txt");
    
            FileReader fr1=null;
            FileWriter fw1=null;
            BufferedReader br = null;
            PrintWriter pw = null;
    
            try {
                fr1 = new FileReader(oldFile2);
                fw1 = new FileWriter(newFile2,true);//true参数要求复制的数据追加到文件后
                br = new BufferedReader(fr1);
                pw = new PrintWriter(fw1,true);//true参数要求复制的数据追加到文件后
                String str = null;
                while ((str=br.readLine())!=null){
                    pw.write(str);
                }
                pw.flush();
                System.out.println("复制完成");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
    //        只需要关闭包装流,基本流会自动关掉
                try{
                    if (pw!=null){
                        pw.close();
                     }
                    if (br!=null){
                        br.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            /*
            * 对象输入输出流
            * ObjectInputStream
            * ObjectOutputStream
            * */
    
            Person p1 = new Person("张三",18,80);
            Person p2 = new Person("李四",18,80);
            Person p3 = new Person("王五",18,80);
    
            List<Person> ps = new ArrayList<Person>();
            ps.add(p1);
            ps.add(p2);
            ps.add(p3);
    
            File file = new File("D:/aword/oldFile.txt");
    
            FileOutputStream fos1 = null;
            ObjectOutputStream oos = null;
    
            try {
                fos = new FileOutputStream(file);
                oos = new ObjectOutputStream(fos);
                oos.writeObject(ps);
                oos.flush();
                System.out.println("保存完成");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try{
                    if (oos!=null){
                        oos.close();
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }
    

    Scanner对象

    Scanner对象可以获取用户的输入。

    Scanner sc = new Scanner(System.in);
    

    通过Scanner类的next()与nextLine()方法获取输入的字符串,再用hasNext()与hasNextLine()判断是否还有输入的数据

    next():

    1. 一定要读取到有效字符后才可以结束输入
    2. 对输入有效字符自谦遇到的空白,next()方法会自动将其去掉
    3. 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
    4. next()不能得到带有空格的字符串

    nextLine():

    1. 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符
    2. 可以获得空白

    nextInt():输入一个整型。

    //创建一个扫描器对象,用于接收键盘数据
    Scanner sc = new Scanner(System.in);
    //判断用户有没有输入字符串
    if(sc.hasNext){
        String str = sc.next();
        System.out.println("输入的内容为:"+str);
    }
    //属于IO流的类如果不关闭会一直占用资源,用完就关掉
    sc.close();
    

    File对象

    File:用于表示硬盘上的一个文件夹或文件。

    对文件进行操作的一些方法。

    import java.io.File;
    import java.io.IOException;
    
    public class TestFile {
        /*
        * File:用于表示硬盘上的一个文件夹或文件
        * */
        //通过构造方法创建文件:表示硬盘上的目录
        File file = new File("D:/awork/File/oldFile.txt");
        public void test1() {
            //新建文件
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void test2() {
            //是否只读
            file.canRead();
            //是否只写
            file.canWrite();
            //查询是否存在,返回地址
            file.getAbsolutePath();
            //获得文件或者目录名
            file.getName();
            //返回父文件或目录名
            file.getParent();
            //获取文件或文件夹路径
            file.getPath();
            //查询是否是目录
            file.isDirectory();
            //查询是否是文件
            file.isFile();
            //最后修改时间
            file.lastModified();
            //文件或文件夹长度,空文件夹长度为0,在win上文件夹不占空间
            file.length();
        }
        public void test3(){
            //获取该目录下所有子目录子文件的名字
            String[] names = file.list();
            for (String str:names) {
                System.out.println(str);
            }
        }
        public void test4() {
            //创建一层目录目录
            File newFileDir = new File("D:/awork/fileDir");
            boolean mkdir = newFileDir.mkdir();
            System.out.println(newFileDir.mkdir());//true
        }
        public void test5(){
            //创建多层目录
            File fileDirs = new File("D:/awork/File/aa/bb/cc");
            fileDirs.mkdirs();
        }
        public void test6(){
            //删除空文件夹
            File fileDleDir = new File("D:/awork/File/aa/");
            fileDleDir.delete();
        }
    }
    
  • 相关阅读:
    pycharm在401跑程序需要每个py文件加一句
    youtube下载视频方法
    服务器重启登陆界面死循环
    matlab2012b_win_install
    ubuntu_matlab2012b_install
    cuda8.0 + cudnn6 + tensorflow1.4 xing
    [BAT] cmd 管理员权限 右键菜单 运行
    Windows下遍历所有GIT目录更新项目脚本
    获取Xshell Xftp等官网下载地址
    Win10 企业版 激活 批处理
  • 原文地址:https://www.cnblogs.com/hermitlee/p/15137371.html
Copyright © 2011-2022 走看看