java中io流 字节流,字符流

io流

1文件file类

File类是java.io包中很重要的一个类;
File类的对象可以表示文件,还可以表示目录,在程序中一个File类对象可以代表一个文件或目录;
File对象可以对文件或目录的属性进行操作,如:文件名、最后修改日期、文件大小等;

File对象无法操作文件的具体数据,即不能直接对文件进行读/写操作。

public class FileDemo {
    public static void main(String[] args) {
        //三种构造方法
        File file=new File("G:/demo.txt");

//        String p="G:\\";
//        File file1=new File(p,"demo1.txt");

//        File file2=new File("G:\\");
//        File file3=new File(file2,"demo.txt");
        System.out.println(file.canExecute());//可执行
        System.out.println(file.canRead());//可读
        System.out.println(file.canWrite());//可写
        System.out.println(file.exists());//文件是否存在
        System.out.println(file.getAbsolutePath());//获取相对路径的文件绝对路径
        System.out.println(file.getName());
        System.out.println(file.getParent());
        System.out.println(file.length());
        System.out.println(file.isAbsolute());//是否为绝对路径
        System.out.println(file.isDirectory());//是否为文件夹
        System.out.println(file.isHidden());//是否为隐藏

      time(file.lastModified(),"yyyy-dd-dd HH:mm:ss");

    }
    public static void time(long a,String b){
        Date date=new Date(a);
        SimpleDateFormat s=new SimpleDateFormat(b);
        String str=s.format(date);
        System.out.println(str);

    }
  public static void main(String[] args) {
        /*
        creatNewFile()创建一个文件
         若存在返回fasle
        不存在 返回true
        如果盘符/地址在计算机没有 直接抛出异常
         */
//        File file=new File("G:/demo.txt");
//        try {
//            System.out.println(file.createNewFile());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

      //  System.out.println(file.delete());//删除文件存在返回true
//        File file=new File("G:/demo/demo");
//        //System.out.println(file.mkdir());//只能创建单文件夹
//        System.out.println(file.mkdirs());//可以创建多级文件夹
//        File file=new File("G:/demo/demo");
//        System.out.println( file.delete());//删除文件夹 删除时文件夹必须为空
       File f=new File("G:\\WeGameApps\\英雄联盟\\Cross");
//        //获取指定路径下所有子集的文件或目录 以字符串的形式输出
//               String []farray=      f.list();
//        for (String str:farray) {
//            System.out.println(str);
//        }

        //添加过滤文件 筛选文件
        String[]array=f.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                   return name.substring(name.lastIndexOf(".")+1,name.length()).equalsIgnoreCase("dll");
            }
        });
        for (String arr:array
             ) {
            System.out.println(arr);
        }


    }

2输入输出

把电脑硬盘上的数据读到程序中,称为输入,即input,进行数据的read操作
从程序往外部设备写数据,称为输出,即output,进行数据的write操作

流按着数据的传输方向分为: 输入流 输出流

​ 输入流:往程序中读叫输入流。
​ 输出流:从程序中往外写叫输出流。

从数据流编码格式上划分为 字节流 字符流
1.字节流
InputStream和OutputStream的子类都是字节流

-可以读写二进制文件,主要处理音频、图片、歌曲、字节流,处理单元为1个字节。

InputStream的基本方法

读取一个字节并以整数的形式返回(0~255),如果返回-1已到输入流的末尾。
int read() throws IOException

读取一系列字节并存储到一个数组buffer,
返回实际读取的字节数,如果读取前已到输入流的末尾返回-1
int read(byte[] buffer) throws IOException

关闭流释放内存资源
void close() throws IOException

OutputStream的基本方法

向输出流中写入一个字节数据,该字节数据为参数b的低8位 void write(int b) throws IOException

将一个字节类型的数组中的从指定位置(off)开始的 len个字节写入到输出流 void write(byte[] b, int off, int len) throws IOException

关闭流释放内存资源 void close() throws IOException

 public static void main(String[] args) {
        FileInputStream in=null;
        FileOutputStream out=null;
        try {

             in=new FileInputStream("G:\\demo.txt");//创建FileInputStream对象 指定要读入的文件 若文件不存在 则抛出异常
            out=new FileOutputStream("E:\\demo.txt");//创建FileOutputStream对象 自动创建输出的文件
            //int b=0;//记录每次得到的字节值
           // while ((b=in.read())!=-1){
             //   System.out.println(b);
            //    out.write(b);//向指定文件中输出字节
           // }
              byte b[]=new byte[10];//第二种方式
               int length=0;
               while ((length=in.read(b))!=-1){
                   System.out.println(length);
                   out.write(b,0,length);
                }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

字节流中常用类
字节输入流 FileInputStream
字节输出流 FileOutputStream

2.字符流

Reader和Writer的子类都是字符流
只能处理文本文件

字符流中常用类
字符输入流 FileReader
字符输出流 FileWriter

    public static void main(String[] args) throws IOException {
        /*
        字符流以字符为单位读写数据  纯文本
        Reader
               转换流 InputStreamReader 从字节流到字符流的桥
               FileReader
               BufferedReader
        Writer
                转换流 我们直接用不到他 OutputStreamWriter
               FileWriter
               BufferedWriter
         */
        FileReader reader=new FileReader("G:\\demo.txt");
        FileWriter writer=new FileWriter("E:\\dd.txt");
//      int c=' ';
//       while ((c=reader.read())!=-1){
//           System.out.println();
//           writer.write(c);
//       }
        char[]c=new char[5];
        int length=0;
        while((length=reader.read(c))!=-1){
            System.out.println(length);
            writer.write(c,0,length);
        }
       reader.close();
       writer.close();
    }
根据封装类型不同流又分为 节点流 处理流
节点流:

​ 如果流封装的是某种特定的数据源,如文件、字符串、字符串数组等,则称为节点流。

处理流。

​ 如果流封装的是其它流对象,称为处理流。
​ 处理流提供了缓冲功能,提高读写效率,同时增加了一些新的方法。

节点流中常用类

​ 字节输入流 FileInputStream
字节输出流 FileOutputStream
字符输入流 FileReader
字符输出流 FileWriter

处理流中常用类
	缓冲字节输出流 BufferedOutputStream
	缓冲字节输入流 BufferedInputStream
	缓冲字符输入流 BufferedReader
	缓冲字符输出流 BufferedWriter
 public static void main(String[] args) throws IOException {
        //创建输入节点流 直接负责文件的读写
        FileInputStream in=new FileInputStream("G:\\TP3Helper.exe");
      //处理流 处理流包装节点流 提供额外的缓冲功能
        // 创建处理对象 内部有一个缓存数组 默认大小为8192字节
        BufferedInputStream bin=new BufferedInputStream(in);
        FileOutputStream out=new FileOutputStream("E:\\TP3Helper.exe");
        BufferedOutputStream bout=new BufferedOutputStream(out);

//        int b=0;
//        while((b=bin.read())!=-1){
//            bout.write(b);
//        }//底层有一个byte数组

        byte []b=new byte[1024];
        int length=0;
        while((length=bin.read(b))!=-1){
            bout.write(b,0,length);
            //当我们调用write是会判断 当缓冲数组满了之后 才会向目标文件输出
            //当我们定义的数组大小  小于 缓冲区数组的大小时 他是有效的 起缓冲作用
            //当我们的数组大于缓冲区数组是 缓冲区数组就失效了
        }
        bout.flush();
        bout.close();
    }
  public static void main(String[] args) throws IOException {
        /*
        字符流以字符为单位读写数据  纯文本
        Reader
               转换流 InputStreamReader 从字节流到字符流的桥
               FileReader
               BufferedReader
        Writer
                转换流 我们直接用不到他 OutputStreamWriter
               FileWriter
               BufferedWriter
         */
        FileReader reader=new FileReader("G:\\demo.txt");
        BufferedReader bufferedReader=new BufferedReader(reader);
        FileWriter writer=new FileWriter("E:\\dd.txt",true);//追加
        BufferedWriter bufferedWriter=new BufferedWriter(writer);
//      int c=' ';
//       while ((c=reader.read())!=-1){
//           System.out.println();
//           writer.write(c);
//       }
//      char c[]=new char[10];
//      bufferedReader.read();
      String line=null;
      while((line=bufferedReader.readLine())!=null){
          bufferedWriter.write(line);
          bufferedWriter.newLine();
      }
      bufferedWriter.flush();
      bufferedReader.close();
      bufferedWriter.close();
    }

打印流

PrintWriter

单向的输出,程序向外输出

案例:从服务器程序中向客户端浏览器输出网页内容

 public static void main(String[] args) throws FileNotFoundException {
        /*
        打印流  单向的从程序中向外输出数据
        PrintWriter打印字符流
        案例 例如从服务器端向 客户端浏览器 输出信息
         */
        PrintWriter out=new PrintWriter("G:\\demo.html");
        //print的底层还是使用的是write 只是重载了多个 可以处理多种的数据类型

        out.print("<h1>这是从服务器端相应回来的数据");
        out.print("<h1>这是从服务器端相应回来的数据");
        out.print("<h1>这是从服务器端相应回来的数据");
        out.close();
    }

对象输入输出流

为什么需要输出对象?

持久的保存对象信息.

使用输出流将对象输出到文件(对象序列化)

使用输入流将对象输入到内存中(反序列化) 也是java中创建对象的方式 new

需要被序列化的对象的类要实现序列化接口(Serializable),生成一个ID号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值