zoukankan      html  css  js  c++  java
  • 1-java中的IO操作

    1.使用字节流写、读、复制数据

    package com.example;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    
    public class MyClass {
        public static void main(String []args){
            try {
                FileOutputStream fos = new FileOutputStream("test2.txt");
    
                String str = "hello";
                byte fosByte[] = str.getBytes("UTF-8");
                fos.write(fosByte);
    
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        }
    }
    View Code
    package com.example;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class MyClass {
        public static void main(String []args){
            try {
                FileInputStream fis = new FileInputStream("test1.txt");
    
                byte fisByte[] = new byte[20];
               // fis.read(fisByte);
                int l = 0;
                while ((l =fis.read(fisByte))!= -1){
                    String str = new String(fisByte,"UTF-8");
                    System.out.println(str);
                }
    
    
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        }
    }
    View Code
    package com.example;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class MyClass {
        public static void main(String []args){
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("test.gif");
                FileOutputStream fos = new FileOutputStream("test2.gif");
    
                byte fisByte[] = new byte[50];
                while (fis.read(fisByte) != -1){
                    fos.write(fisByte);
                }
    
                fos.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    View Code

    2.使用带缓冲的字节流写、读、复制文件

    package com.example;
    
    import java.io.BufferedOutputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    
    public class MyClass {
        public static void main(String []args){
    
            try {
                FileOutputStream fos = new FileOutputStream("test4.txt");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
    
                String str = "hello wangyu";
                byte b[] = str.getBytes("UTF-8");
                bos.write(b);
    
                bos.close();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        }
    }
    View Code
    package com.example;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class MyClass {
        public static void main(String []args){
            try {
                FileInputStream fis = new FileInputStream("test3.txt");
                BufferedInputStream bis = new BufferedInputStream(fis);
    
                byte b[] = new byte[7];
                bis.read(b);
                String str = new String(b,"UTF-8");
    
                System.out.println(str);
                bis.close();
                fis.close();
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    View Code
    package com.example;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class MyClass {
        public static void main(String []args){
            try {
                FileInputStream fis = new FileInputStream("test.gif");
                BufferedInputStream bis = new BufferedInputStream(fis);
                FileOutputStream fos = new FileOutputStream("test3.gif");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
    
                byte b[] = new byte[100];
                while (bis.read(b) != -1 ){
                    bos.write(b);
                }
    
                bos.close();
                fos.close();
                bis.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    View Code

    3.使用字符流写、读、复制数据

    package com.example;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    
    public class MyClass {
        public static void main(String []args){
            try {
                FileOutputStream fos = new FileOutputStream("test6.txt");
                OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
                FileInputStream fis = new FileInputStream("test5.txt");
                InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
    
    
    
                char c[] = new char[10];
                int l = 0;
                while ((l = isr.read(c)) != -1){
                    osw.write(c,0,l);
                }
    
                osw.close();
                fos.close();
                isr.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    View Code

    4.使用带缓冲的字符流写、读、复制数据

    package com.example;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    
    public class MyClass {
        public static void main(String []args){
            try {
                FileInputStream fis = new FileInputStream("test6.txt");
                InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
                BufferedReader br = new BufferedReader(isr);
    
                FileOutputStream fos = new FileOutputStream("test7.txt");
                OutputStreamWriter osw = new OutputStreamWriter(fos);
                BufferedWriter bw = new BufferedWriter(osw);
    
                String str = new String();
    
                while ((str = br.readLine()) != null){
                    //System.out.println(str);
                    bw.write(str);
                }
    
                bw.close();
                osw.close();
                fos.close();
                br.close();
                isr.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    View Code
  • 相关阅读:
    Spring知识点回顾(09)异步调用
    Spring知识点回顾(07)事件发布和监听
    Spring知识点回顾(08)spring aware
    Spring知识点回顾(06)Profile 和 条件注解 @Conditional
    Spring知识点回顾(04)el 和资源使用
    Spring知识点回顾(05)bean的初始化和销毁
    Spring知识点回顾(01)Java Config
    Spring知识点回顾(02)AOP
    Spring知识点回顾(03)Bean的 Scope
    HTTPS 原理
  • 原文地址:https://www.cnblogs.com/BelieveFish/p/6683782.html
Copyright © 2011-2022 走看看