zoukankan      html  css  js  c++  java
  • 文章标题

    java中IO类的各种操作【转】

    java的IO类操作主要包括如下几类

    (1)、File类的使用。

    (2)、字节操作流:OutputStream、InputStream

    (3)、字符操作流:Reader、Writer

    (4)、对象序列化:serializable

    1)File类
    Java代码

    [java] view plaincopyprint?
    
        public class File extends Object implements Serizliable Comparable<File>  
     从定义看,File类是Object的直接子类,同时它继承了Comparable接口可以进行数组的排序。
    
    File类的操作包括文件的创建、删除、重命名、得到路径、创建时间等,以下是文件操作常用的函数。
    

    这里写图片描述

    File类的操作:

    (1)创建文件,注意File.separator可以解决跨操作系统的问题。

        下面的例子是一创建一个文件,如果该文件存在则删除,否则创建该文件。
    

    Java代码

     public class FileDemo1 {   
            public static void main(String[] args) {   
                File file = new File("D:" + File.separator + "test.txt");   
                if (file.exists()) {   
                    file.delete();   
                } else {   
                    try {   
                        file.createNewFile();   
                    } catch (IOException e) {   
                        // TODO Auto-generated catch block   
                        e.printStackTrace();   
                    }   
                }   
            }   
        }  

    (2)文件的类型函数

       file.isFile(); //判断是不是文件
    
       file.isDirectory();//判断是不是目录
    

    (3)列出目录的内容

        pulbic String[] list();//列出所有文件名和目录名
    
        public File[] listFiles();//列出所有文件和目录
    

    2)、字节操作流(btyle)

    (1)字节输出流OutputStream
    这里写图片描述

    Java代码

        public class FileDemo1 {   
            public static void main(String[] args) {   
                File file = new File("D:" + File.separator + "test.txt");//指定要操作的文件   
                OutputStream out=null;//定义字节流输出对象   
                try {   
                    //out= new FileOutputStream(file,true);//是否字节追加函数   
                    out= new FileOutputStream(file);//获取实际的字节流输出对象,内容覆盖   
                } catch (FileNotFoundException e) {   
                    e.printStackTrace();   
                }   
                String  info="hello";//要输入的内容   
                byte[] b=info.getBytes();//将字符转化为字节数组   
                try {   
                    out.write(b);   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                try {   
                    out.close();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
            }   
        }  

    (2)字节输入流InputStream
    这里写图片描述

    Java代码

     public class FileDemo1 {   
            public static void main(String[] args) {   
                File file = new File("D:" + File.separator + "test.txt");//指定要操作的文件   
                InputStream In=null;//定义字节流输入对象   
                try {   
                    //out= new FileOutputStream(file,true);//是否字节追加函数   
                    In= new FileInputStream(file);//获取实际的字节流输入对象   
                } catch (FileNotFoundException e) {   
                    e.printStackTrace();   
                }   
                int len=0;//输入数组长度   
                byte[] b=new byte[1024];//开辟空间,读取内容   
                //byte[] b=new byte[(int)file.length()];//根据文件大小开辟空间   
                try {   
                    len=In.read(b);//读取   
                } catch (IOException e1) {   
                    e1.printStackTrace();   
                }   
                try {   
                    In.close();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                System.out.println(new String(b,0,len));   
            }   
        }

    (3)字符输出流Write
    这里写图片描述

    Java代码

     public class FileDemo1 {   
            public static void main(String[] args) {   
                File file = new File("D:" + File.separator + "test.txt");// 指定要操作的文件   
                Writer write = null;// 定义字符输出流   
                try {   
                    write = new FileWriter(file);   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                String infor = "hello,heiehiehieh";   
                try {   
                    write.write(infor);   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                try {   
                    write.close();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
            }   
        }  

    (4)字符输入流Reader
    这里写图片描述

    Java代码

     public class FileDemo1 {   
            public static void main(String[] args) {   
                File file = new File("D:" + File.separator + "test.txt");// 指定要操作的文件   
                Reader read = null;// 定义字符输入流   
                try {   
                    read = new FileReader(file);   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                String infor = "hello,heiehiehieh";   
                char[] b=new char[1024];//设置字符的长度   
                try {   
                    int len=read.read(b);   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                try {   
                    read.close();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
            }   
        }  

    (5)字节流和字符流的区别(重点)

      字节流没有缓冲区,是直接输出的,而字符流是输出到缓冲区的。因此在输出时,字节流不调用colse()方法时,信息已经输出了,而字符流只有在调用close()方法关闭缓冲区时,信息才输出。要想字符流在未关闭时输出信息,则需要手动调用flush()方法。
    

    (6)转换流:在io中还存在一类是转换流,将字节流转换为字符流,同时可以将字符流转化为字节流。

    OutputStreamWriter(OutStream out):j将字节流以字符流输出。

    InputStreamReader(InputStream in):将字节流以字符流输入。

    (7)打印流 PrintStream

      在操作中要求输出信息时,可以采用PrintStream进行输出,它包括PrintWrite和PrintReader
    

    3)对象序列化

       对象序列化是指将一个对象可以转化为二进制的byte流,可以以文件的方式进行保存。
    
       将对象保存在文件的操作叫做对象的序列化操作。
    
       将对象从文件中恢复的操作叫做反序列化操作。
    

    一个对象如果要能序列化,它必须继承Serizliable。在实现序列化是则需要ObjectOurputStream完成,而需要反序列化时则采用ObjectInputStream。

    这里写图片描述
    这里写图片描述

    transient关键字:变量声明为Transient后,该变量不可序列化。

    4)内存流

        在项目的开发过程中,有时希望只产生临时文件,将信息输出的内存中,此时会用到内存流,内存流基本方法如下:
    

    这里写图片描述

    Java代码

     public class FileDemo1 {   
            public static void main(String[] args) {   
                String infor = "hello";   
                // 所有的内容向内存中输入   
                InputStream input = new ByteArrayInputStream(infor.getBytes());   
                // 所有内存的内容由outputStream输出   
                OutputStream out = new ByteArrayOutputStream();   
                int temp = 0;   
                try {   
                    while ((temp = input.read()) != -1) {   
                        char c = Character.toUpperCase((char) temp);   
                        out.write(c);//从内存中输出,所有的内容都保存在ByteArrayOutputStream中   
                    }   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                try {   
                    input.close();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                try {   
                    out.close();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                System.out.println(out.toString());   
            }   
        }  

    5)System类对IO的支持
    这里写图片描述

    6)缓存读取

    这里写图片描述

  • 相关阅读:
    c语言 数组名是常量指针
    c语言 动态数组
    c语言 指针的值
    c语言 &取地址运算符的理解
    c语言 指针与地址的区别
    c语言 指针与数组
    linux cheese摄像机工具在window电脑上显示
    C#实现简单的 Ping 的功能,用于测试网络是否已经联通
    c# 扫描局域网IP列表的几种方法
    c# 遍历局域网计算机(电脑)获取IP和计算机名称
  • 原文地址:https://www.cnblogs.com/lllini/p/4433170.html
Copyright © 2011-2022 走看看