zoukankan      html  css  js  c++  java
  • I/O流

    InputStream/OutinputStream

    文件:

    FileInputStream/FileInputStream

      InputStream(输入流)
                数据从文件到java代码中.
               int read();//读取一个字节
               int read(byte[]);//读取一串字节
               long avaliable;//文件长度


     FileInputStream(字节文件输入流)
               new FileInputStrea(File);
               new FileInputStream(“文件路径+文件名”)

    OutputStream(输出流)
                数据从java代码中,写到文件或者其他介质中,
                void write(字节);//写入一个字节
                void write(byte[]);//写入字节数组
    FileOutputStream
               new FileOutputStream(File);
               new FileOutputStream(“文件路径+文件名”);
               new FileOutputStream(“文件路径+文件名”,boolean);
             注意:

    a.boolean:表示是否向文件末尾追加,如果是true,表示
                     追加,false表示不追加(也就是覆盖),默认值为false

     b.创建FileOutputStream实例时,
         如果相应的文件并不存在,则会自动创建一个空的文件

    package ioday01;
    
    import java.io.File;
    import java.io.IOException;
    
    public class Text {
    
        public static void main(String[] args) {
             //TODO Auto-generated method stub
            //读取a.txt文件,在哪个地方,\
            File f = new File("文件路径");
            // "\":表示转义
             //"/":表示/
            File f = new File("E:/a.txt");
            //System.out.println(f);
            //文件是否存在
            boolean flag = f.exists();
            System.out.println(flag);
            //是否是文件
            flag = f.isFile();
            System.out.println(flag);
            //是否是目录
            flag  = f.isDirectory();
            System.out.println(flag);
            //获取文件相对路径
            String path = f.getPath();
            System.out.println(path);
            //获取文件绝对路径
             path = f.getAbsolutePath();
             System.out.println(path);
             //获取名字
             String name = f.getName();
             System.out.println(name);
             //删除文件或者目录
             flag = f.delete();
             System.out.println(flag);
             //创建(检查异常,IOException,SOLException)
             try {
                flag = f.createNewFile();
                System.out.println(flag);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             System.out.println(f.length());
             //普通java项目,都是项目名为节点
             File f = new File("src/ioday01/Text.java");
             System.out.println(f.exists());
             System.out.println(f.getPath());
             System.out.println(f.getAbsolutePath());
            }
    
    }
    View Code

    Reader/Writer(字符流)
               //能够用文本编辑器打开的文件,不乱码就是字符文件
               //用文本编辑器打开乱码的,就是字节文件

     FileReader            

    int b = fr.read();//读取一个字符            

    int length = fr.read(char[]);//读取字符数组  

          

    package io.day02;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    
    public class TestReaderFile {
    
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            File f = new File("src/a.txt");
            InputStream is = new FileInputStream(f);
    
            int b;
            while((b = is.read()) != -1){
                System.out.print((char)b);
            }
        }
    
    }
    View Code

    FileWriter            

    fw.write(char);//写一个字符            

    fw.write(char[])//写字符数组    

        

    package io.day02;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class TestWriterFile {
    
        public static void main(String[] args) throws Exception {
            int[] ary = {97,98,99,100,101};
            File f = new File("src/a.txt");
            OutputStream os = new FileOutputStream(f);
            for(int i :ary){
                os.write(i);
            }
            os.close();
            //复制文件a.txt的内容到b.txt
            File f1 = new File("src/b.txt");
            boolean f2 = f1.exists();
            System.out.println(f2);
             try {
                f2 = f1.createNewFile();
                System.out.println(f2);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
        }
        
        
    }
    View Code

     BufferedReader(字符输入缓冲流)            

    BufferedReader br = new BufferedReader(new FileReader("文件路径"))            

    String str = br.readLine();//读取一个字符     

       

    BufferedWriter(字符输出缓冲流)            

    BufferedWriter bw = new BufferedWriter(new FileWriter("文件路径"))            

    bw.write(字符串)     

    DataInputStream/DataOutputStream

    读写字符文件            

    BufferedReader br = new BufferedReader (new FileReader(文件));            

    BufferedWriter bw = new BufferedWriter(new FileWriter(文件,boolean));  

     读写字节文件             DataInputStream dis = new DataInputStream(new FileInputStream(文件));            

    DataOutputStream dos = new DataOutputStream(new FileOutputStrem(文件));  

    package io.day04;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class TestBuffer {
    
        public static void main(String[] args) throws Exception {
    //        buReader();
            buWriter();
        }
    
        private static void buWriter() throws Exception {
            // TODO Auto-generated method stub
            BufferedWriter bw = new BufferedWriter(new FileWriter("src/a.txt"));
            bw.write("我是刘美猪");
            //刷新缓存
    //        bw.flush();
            //默认执行flush(),关闭管道.
            bw.close();
        }
    
        private static void buReader() throws Exception {
            // TODO Auto-generated method stub
            BufferedReader br = new BufferedReader(new FileReader("src/c.txt"));
            //读取一行记录
    //        String str = br.readLine();
    //        str = br.readLine();
    //        System.out.println(str);
            String str;
            while((str = br.readLine())!=null){
                System.out.println(str);
            }
        }
    
    }
    View Code

    读取整个字符文件            

    String str = null;            

    while((str=br.readLine())!= null){   System.out.println(str);    }  

    读取整个字节文件      

    int b;            

    while((b=br.read())!= -1){   System.out.println(b);    }

  • 相关阅读:
    关于VGG网络的介绍
    nvidia-docker 安装
    test
    ARTS-S EN0002-London HIV patient's remission spurs hope for curing AIDS
    ARTS-S EN0001-In tech race with China, US universities may lose a vital edge
    ARTS-S Why do India and Pakistan keep fighting over Kashmir?
    ARTS-S sed指定行添加
    ARTS-S linux查看进程打开的文件数
    ARTS-S centos查看端口被哪个进程占用
    ARTS-S centos修改hostname
  • 原文地址:https://www.cnblogs.com/liumeilin/p/6832483.html
Copyright © 2011-2022 走看看