zoukankan      html  css  js  c++  java
  • Java--IO流

    1、凡是与输入、 输出相关的类、接口等都定义在java.io包下。

    2、File是一个类,可以用构造器创建其对象,它对应于一个文件或一个文件目录。

    3、路径分为绝对路径和相对路径:1)绝对路径包含盘符在内的完整文件路径;2)相对路径指当前文件目录下的文件的路径。

    4、File类对象是与平台无关的。

    5、File中的方法,只涉及到如何创建、删除、重命名文件等,不能对文件的内容做操作,必须由io流来操作。

    6、File类的对象常作为io流的具体类的构造器的形参。

    7、File类中常用方法:

    --访问文件名

      1)getName();//获取文件名

      2)getPath();//获取文件路径

      3)getAbsoluteFile();//获取绝对文件名

      4)getAbsolutePath();//获取绝对路径

      5)getParent();//获取父目录

      6)renameTo(File newName);//重命名:file1.renameTo(file2), 要求file1必须存在而file2不存在

    --文件检测

      1)exists();//检测文件是否存在

      2)canWrite();//是否可写

      3)canRead();//是否可读

      4)isFile();//是否是文件

      5)isDirectory();//是否是目录

    --文件信息

      1)lastModified();//上次修改时间

      2)length();//文件内容长度 

    --文件、目录操作

      1)createNewFile();//创建新文件

      2)delete();//删除文件

      3)mkDir();//创建文件目录,只有在上级目录存在的情况下才能创建成功

      4)mkDirs();//创建一个文件目录,若上级目录不存在,则先创建上级目录

      5)list();//以字符串形式返回当前目录下所有文件名或目录

      6)listFiles();//以文件形式返回当前目录下所有文件

    8、IO流用来处理设备之间的数据传输;Java程序中,对于数据的输入/输出操作以"流(stream)"的方式进行;java.io包下提供了各种"流"类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。

    9、流的分类:

      1)按操作数据单位不同分为:字节流(8bit),字符流(16bit);

      2)按数据流的流向不同分为:输入流,输出流

      3)按流的角色的不同分为:节点流(4个:FileInputStream,FileOutputStream,FileReader,FileWriter),处理流

    10、IO体系

    抽象基类              节点流(文件流)           缓冲流(处理流的一种)

    InputStream       FileInputStream       BufferedInputStream

    OutputStream     FileOutputStream     BufferedOutputStream

    Reader                FileReader               BufferedReader

    Writer                 FileWriter                BufferedWriter

    11、FileInputSrteam:

      1)read();//读取文件的一个字节,当执行到文件结尾时返回-1

    12、FileOutputStream:

      1)write();//向文件中写入,若文件不存在,则自动创建;若存在,则覆盖原文件

    13、使用FileReader、FileWriter(字节流)可以实现文本文件的复制;对于非文本文件(如视频、音频等),只能使用字节流。

    14、缓冲流的使用:

    public static void testBuffered(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
            
        try{
            //1、提供读入、写出的文件
            File file1 = new File("hello4.txt");
            File file2 = new File("hello5.txt");
            //2、先创建相应的节点流:FileInputStream、FileOutputStream
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);
            //3、将创建的节点流的对象作为参数传递给缓冲流的构造器中
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            //4、实现文件的复制
            byte[] b = new byte[1024];
            int len;
            while((len = bis.read(b)) != -1){
                bos.write(b, 0, len);
                bos.flush();
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            //5、关闭流
            if(bos != null){
                try {
                bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    15、使用缓冲流能够大大提高效率,比节点流快很多。

    16、转换流提供了在字节流和字符流之间的转换;

    --Java API中的两个转换流:InputStreamReader、OutputStreamWriter

    --字节流中的数据都是字符时,转成字符流操作更高效。

    17、标准的输入流:System.in;输出流:System.out

    18、打印流:字节流(PrintStream);字符流(PrintWriter)。

            PrintStream ps = null;
            
            try{
                ps = new PrintStream(new FileOutputStream("print.txt"), true);
                System.setOut(ps);
                String str = "TestPrintStream";
                System.out.println(str);
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(ps != null){
                    ps.close();
                }
            }

    19、数据流:用来处理基本数据类型、String、字节数组的数据。

    --DataOutputStream

        DataOutputStream dos = null;
            try{
                dos = new DataOutputStream(new FileOutputStream("data.txt"));
                dos.writeUTF("hello world!");
                dos.writeInt(123);
                dos.writeBoolean(false);
                dos.writeLong(123456789);
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(dos != null){
                    try {
                        dos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

    --DataInputStream

    DataInputStream dis = null;
            try{
                dis = new DataInputStream(new FileInputStream("data.txt"));
                String str = dis.readUTF();
                System.out.println(str);
                
                int numI = dis.readInt();
                System.out.println(numI);
                
                boolean b = dis.readBoolean();
                System.out.println(b);
                
                long numL = dis.readLong();
                System.out.println(numL);
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(dis != null){
                    try {
                        dis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

    20、对象流:用于存储和读取对象的处理流。

    --ObjectOutputStream

         Person p1 = new Person("AA", 22);
            Person p2 = new Person("BB", 55);
            Person p3 = new Person("CC", 35);
            ObjectOutputStream oos = null;
            
            try{
                oos = new ObjectOutputStream(new FileOutputStream("object.txt"));
                oos.writeObject(p1);
                oos.flush();
                oos.writeObject(p2);
                oos.flush();
                oos.writeObject(p3);
                oos.flush();
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(oos != null){
                    try {
                        oos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

    --ObjectInputStream

         ObjectInputStream ois = null;
            try{
                ois = new ObjectInputStream(new FileInputStream("object.txt"));
                Person a1 = (Person)ois.readObject();
                System.out.println(a1);
                Person a2 = (Person)ois.readObject();
                System.out.println(a2);
                Person a3 = (Person)ois.readObject();
                System.out.println(a3);
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(ois != null){
                    try {
                        ois.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

    --类的序列化:1)实现Serializable接口;2)类的属性也需实现Serializable接口;

    3)提供版本号:private static final long seriaVersionUID = xxx;

    4)使用static或transient修饰的属性,不可实现序列化。

    21、随机存取文件类(RandomAccessFile):

    --public RandomAccessFile(File file, String mode);

    --public RandomAccessFile(String name, String mode);

         RandomAccessFile rcf = null;
            try{
                rcf = new RandomAccessFile("hello.txt", "rw");
                //读取文件内容
                String str = null;
                while((str = rcf.readLine()) != null){
                    System.out.println(str);
                }
                //指定位置写入
                rcf.seek(5);
                rcf.write("TT".getBytes());
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(rcf != null){
                    try {
                        rcf.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    
                }
            }
  • 相关阅读:
    nodejs生成UID(唯一标识符)——node-uuid模块
    python 3.6 关于python的介绍
    Hdu-3333 Turning Tree (离线树状数组/线段树)
    HDU-3333 Turing Tree 分块求区间不同数和
    Hello,World.
    IntelliJ IDEA 自动化工具安装并添加自动化测试框架
    git&sourcetree安装及在IntelliIJ下拉取项目基础使用
    3.jquery在js文件中获取选择器对象
    2.jquery在js中写标准的ajax请求
    操作系统简介
  • 原文地址:https://www.cnblogs.com/tengtao93/p/4680172.html
Copyright © 2011-2022 走看看