• 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
  • 相关阅读:
    AUSU 安装Win10注意事项
    华硕笔记本无法设置U盘启动,快捷启动不能识别
    postgres 得到所有表空间 和 表空间的位置
    python 远程链接、搜索与下载
    python 读取 postgreSQL 数据保存CSV文件
    weka 初练之 文本分类
    基于springMVC+mybatis的实践记录
    简单的springMVC + mybatis 编写程序流程
    sql查询 生成列号
    通过资源文件 验证拦截机制
  • 原文地址:https://www.cnblogs.com/BelieveFish/p/6683782.html
Copyright © 2020-2023  润新知