• java===IO : 基本概念


    package IO基本概念;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    /**IO流用来处理设备间数据的传输:例如 硬盘存储数据(存储时间长),放到内存中读取(临时存储,但是处理效率快)
     * 1、从硬盘向内存中输入 就是reader-->输入流   2、 从内存向硬盘写入 就是Writer-->输出流
     * 输入输出流相对于内存设备而言;将外设设备上的设备读取到内存中叫做输入,将内存的数据写入到外设设备上叫做输出
     * 按照操作数据分类:字节流、字符流
     * 字节流:可以操作一切数据
     * 字符流:字节流读取文字字节数据后,查找指定的编码表获取对应的文字再对这个文字进行操作;简单说:字节流+编码表
     * 
     * 字节流的顶层父类:
     * 1、InputStream 2、OutputStream
     * 字符流的顶层父类:
     * 1、Reader 2、Writer
     * 如果要操作文件优先考虑字符流;既然确定一个子类用来实例化Writer;
     * 这个子类就是FileWriter;
     * */
    public class IODemo_1 {
    
        public static void main(String[] args) throws IOException {
            //创建一个可以往文件写入字符数据的字符流对象。
            /*
             * 既然是王一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据)
             * 
             * */
            String LINE_SEPARATOR=System.getProperty("line.separator");
            FileWriter fw1 = new FileWriter("demo.txt",true);//如果加上true可以续写
            fw1.write("老父亲"+LINE_SEPARATOR+"老母亲");
            fw1.write("平安");
            //这个时候数据在临时存储缓冲区。
            fw1.flush();
            //刷新后,数据写入预期文件
            fw1.close();
            //关闭流,同时会执行一个flush方法。
    
        }
    
    }
    package IO基本概念;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    /*字符读取流 FileReader*/
    public class ReaderDemo_1 {
    
        public static void main(String[] args) {
            //1、创建一个能够读取字符数据的流对象
            FileReader f1 = null;
            try {
                f1=new FileReader("demo.txt");
                try {
                    int ch=0;
                    while((ch=f1.read())!=-1)
                     System.out.println(ch);
                } catch (IOException e) {
                    
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                
                e.printStackTrace();
            }
    
        }
    
    }
    package IO基本概念;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    /*字符读取流 FileReader*/
    public class ReaderDemo_2 {
    
        public static void main(String[] args) {
            //1、创建一个能够读取字符数据的流对象
            FileReader f1 = null;
            try {
                f1=new FileReader("demo.txt");
                try {
                    //创建字符数组
                    char[]ch=new char[1024*5];
                    int num=f1.read(ch);
                    System.out.println(num+":"+new String(ch,0,num));
                } catch (IOException e) {
                    
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                
                e.printStackTrace();
            }
    
        }
    
    }
    package IO基本概念;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    
    public class IOExceptionDemo {
    
        public static void main(String[] args)  {
            
            String LINE_SEPARATOR=System.getProperty("line.separator");
            FileWriter fw1=null;
            try {
                fw1 = new FileWriter("demo.txt");
                fw1.write("老父亲" + LINE_SEPARATOR + "老母亲");
                
            } catch (Exception e) {
                System.out.println(e.toString());
            }finally{
                try {
                    if(fw1!=null)
                       fw1.close();
                } catch (IOException e) {
                    
                    throw new RuntimeException("关闭失败!!");
                }
            }
            
    
        }
    
    }
    package IO基本概念;
    
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /**将E盘propertise.txt文件复制到D盘
     * 思路:
     * 1、读取propertise.txt文件
     * 2、D盘创建文件,将读取的内容写入
     * 3、关闭流
     * */
    public class IOTest_1 {
    
        public static void main(String[] args) throws IOException {
            test_2();
        }
        private static void test_2() {
            FileReader fr2 = null;
            FileWriter fw2 = null;
            try{
                fr2 = new FileReader("E:\propertise.txt");
                fw2 = new FileWriter("D:\propertise.txt");
                char[]chs = new char[1024*5];
                int len = 0;
                while((len=fr2.read(chs))!=-1) 
                  fw2.write(chs,0,len);
            }catch(IOException e){
                System.out.println(e.toString());
            }finally {
                if(fr2!=null)
                    try {
                        fr2.close();
                    } catch (IOException e) {
                        
                        e.printStackTrace();
                    }
                if(fw2!=null)
                    try {
                        fw2.close();
                    } catch (IOException e) {
                        
                        e.printStackTrace();
                    }
            }
        }
        public static void test_1()throws IOException{
             FileReader fr1 = new FileReader("E:\propertise.txt");
             FileWriter fw1 = new FileWriter("D:\propertise.txt");
             int ch=0;
             while((ch=fr1.read())!=-1){
                 fw1.write(ch);
             }
             fr1.close();
             fw1.close();
        }
    
    }
  • 相关阅读:
    腾讯实习前端工程师面经-一面-腾讯看点
    Redux的createStore实现
    GNU ARM 汇编基础
    python爬虫学习04-爬取贴吧
    python学习03-使用动态ua
    Python爬虫学习02--pyinstaller
    python爬虫学习01--电子书爬取
    简单的SQL语句学习
    微信小程序的五个生命周期函数
    python学习(12)使用正则表达式
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/6859308.html
Copyright © 2020-2023  润新知