zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVAIO操作学习笔记:字节流与字符流操作

    import java.io.* ;
    public class Copy{
        public static void main(String args[]){
            if(args.length!=2){        // 判断是否是两个参数
                System.out.println("输入的参数不正确。") ;
                System.out.println("例:java Copy 源文件路径 目标文件路径") ;
                System.exit(1) ;    // 系统退出
            }
            File f1 = new File(args[0]) ;    // 源文件的File对象
            File f2 = new File(args[1]) ;    // 目标文件的File对象
            if(!f1.exists()){
                System.out.println("源文件不存在!") ;
                System.exit(1) ;
            }
            InputStream input = null ;        // 准备好输入流对象,读取源文件
            OutputStream out = null ;        // 准备好输出流对象,写入目标文件
            try{
                input = new FileInputStream(f1) ;
            }catch(FileNotFoundException e){
                e.printStackTrace() ;
            }
            try{
                out = new FileOutputStream(f2) ;
            }catch(FileNotFoundException e){
                e.printStackTrace() ;
            }
            if(input!=null && out!=null){    // 判断输入或输出是否准备好
                int temp = 0 ;    
                try{
                    while((temp=input.read())!=-1){    // 开始拷贝
                        out.write(temp) ;    // 边读边写
                    }
                    System.out.println("拷贝完成!") ;
                }catch(IOException e){
                    e.printStackTrace() ;
                    System.out.println("拷贝失败!") ;
                }
                try{
                    input.close() ;        // 关闭
                    out.close() ;        // 关闭
                }catch(IOException e){
                    e.printStackTrace() ;
                }
            }
        }    
    }
    import java.io.File ;
    import java.io.InputStream ;
    import java.io.FileInputStream ;
    public class InputStreamDemo01{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            InputStream input = null ;    // 准备好一个输入的对象
            input = new FileInputStream(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行读操作
            byte b[] = new byte[1024] ;        // 所有的内容都读到此数组之中
            input.read(b) ;        // 读取内容
            // 第4步、关闭输出流
            input.close() ;                        // 关闭输出流
            System.out.println("内容为:" + new String(b)) ;    // 把byte数组变为字符串输出
        }
    };
    import java.io.File ;
    import java.io.InputStream ;
    import java.io.FileInputStream ;
    public class InputStreamDemo02{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            InputStream input = null ;    // 准备好一个输入的对象
            input = new FileInputStream(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行读操作
            byte b[] = new byte[1024] ;        // 所有的内容都读到此数组之中
            int len = input.read(b) ;        // 读取内容
            // 第4步、关闭输出流
            input.close() ;                        // 关闭输出流
            System.out.println("读入数据的长度:" + len) ;
            System.out.println("内容为:" + new String(b,0,len)) ;    // 把byte数组变为字符串输出
        }
    };
    import java.io.File ;
    import java.io.InputStream ;
    import java.io.FileInputStream ;
    public class InputStreamDemo03{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            InputStream input = null ;    // 准备好一个输入的对象
            input = new FileInputStream(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行读操作
            byte b[] = new byte[(int)f.length()] ;        // 数组大小由文件决定
            int len = input.read(b) ;        // 读取内容
            // 第4步、关闭输出流
            input.close() ;                        // 关闭输出流
            System.out.println("读入数据的长度:" + len) ;
            System.out.println("内容为:" + new String(b)) ;    // 把byte数组变为字符串输出
        }
    };
    import java.io.File ;
    import java.io.InputStream ;
    import java.io.FileInputStream ;
    public class InputStreamDemo04{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            InputStream input = null ;    // 准备好一个输入的对象
            input = new FileInputStream(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行读操作
            byte b[] = new byte[(int)f.length()] ;        // 数组大小由文件决定
            for(int i=0;i<b.length;i++){
                b[i] = (byte)input.read() ;        // 读取内容
            }
            // 第4步、关闭输出流
            input.close() ;                        // 关闭输出流
            System.out.println("内容为:" + new String(b)) ;    // 把byte数组变为字符串输出
        }
    };
    import java.io.File ;
    import java.io.InputStream ;
    import java.io.FileInputStream ;
    public class InputStreamDemo05{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            InputStream input = null ;    // 准备好一个输入的对象
            input = new FileInputStream(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行读操作
            byte b[] = new byte[1024] ;        // 数组大小由文件决定
            int len = 0 ; 
            int temp = 0 ;            // 接收每一个读取进来的数据
            while((temp=input.read())!=-1){
                // 表示还有内容,文件没有读完
                b[len] = (byte)temp ;
                len++ ;
            }
            // 第4步、关闭输出流
            input.close() ;                        // 关闭输出流
            System.out.println("内容为:" + new String(b,0,len)) ;    // 把byte数组变为字符串输出
        }
    };
    import java.io.File ;
    import java.io.OutputStream ;
    import java.io.FileOutputStream ;
    public class OutputStreamDemo01{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            OutputStream out = null ;    // 准备好一个输出的对象
            out = new FileOutputStream(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行写操作
            String str = "Hello World!!!" ;        // 准备一个字符串
            byte b[] = str.getBytes() ;            // 只能输出byte数组,所以将字符串变为byte数组
            out.write(b) ;                        // 将内容输出,保存文件
            // 第4步、关闭输出流
            out.close() ;                        // 关闭输出流
        }
    };
    import java.io.File ;
    import java.io.OutputStream ;
    import java.io.FileOutputStream ;
    public class OutputStreamDemo02{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            OutputStream out = null ;    // 准备好一个输出的对象
            out = new FileOutputStream(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行写操作
            String str = "Hello World!!!" ;        // 准备一个字符串
            byte b[] = str.getBytes() ;            // 只能输出byte数组,所以将字符串变为byte数组
            for(int i=0;i<b.length;i++){        // 采用循环方式写入
                out.write(b[i]) ;    // 每次只写入一个内容
            }
            // 第4步、关闭输出流
            out.close() ;                        // 关闭输出流
        }
    };
    import java.io.File ;
    import java.io.OutputStream ;
    import java.io.FileOutputStream ;
    public class OutputStreamDemo03{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            OutputStream out = null ;    // 准备好一个输出的对象
            out = new FileOutputStream(f,true)  ;    // 此处表示在文件末尾追加内容
            // 第3步、进行写操作
            String str = "Hello World!!!" ;        // 准备一个字符串
            byte b[] = str.getBytes() ;            // 只能输出byte数组,所以将字符串变为byte数组
            for(int i=0;i<b.length;i++){        // 采用循环方式写入
                out.write(b[i]) ;    // 每次只写入一个内容
            }
            // 第4步、关闭输出流
            out.close() ;                        // 关闭输出流
        }
    };
    import java.io.File ;
    import java.io.OutputStream ;
    import java.io.FileOutputStream ;
    public class OutputStreamDemo04{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            OutputStream out = null ;    // 准备好一个输出的对象
            out = new FileOutputStream(f,true)  ;    // 此处表示在文件末尾追加内容
            // 第3步、进行写操作
            String str = "
    Hello World!!!" ;        // 准备一个字符串
            byte b[] = str.getBytes() ;            // 只能输出byte数组,所以将字符串变为byte数组
            for(int i=0;i<b.length;i++){        // 采用循环方式写入
                out.write(b[i]) ;    // 每次只写入一个内容
            }
            // 第4步、关闭输出流
            out.close() ;                        // 关闭输出流
        }
    };
    import java.io.File ;
    import java.io.OutputStream ;
    import java.io.FileOutputStream ;
    public class OutputStreamDemo05{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            OutputStream out = null ;    // 准备好一个输出的对象
            out = new FileOutputStream(f)  ;    // 实例化
            // 第3步、进行写操作
            String str = "Hello World!!!" ;        // 准备一个字符串
            byte b[] = str.getBytes() ;            // 只能输出byte数组,所以将字符串变为byte数组
            out.write(b) ;        // 写入数据
            // 第4步、关闭输出流
            // out.close() ;                        // 关闭输出流
        }
    };
    import java.io.File ;
    import java.io.Reader ;
    import java.io.FileReader ;
    public class ReaderDemo01{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            Reader input = null ;    // 准备好一个输入的对象
            input = new FileReader(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行读操作
            char c[] = new char[1024] ;        // 所有的内容都读到此数组之中
            int len = input.read(c) ;        // 读取内容
            // 第4步、关闭输出流
            input.close() ;                        // 关闭输出流
            System.out.println("内容为:" + new String(c,0,len)) ;    // 把字符数组变为字符串输出
        }
    };
    import java.io.File ;
    import java.io.Reader ;
    import java.io.FileReader ;
    public class ReaderDemo02{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            Reader input = null ;    // 准备好一个输入的对象
            input = new FileReader(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行读操作
            char c[] = new char[1024] ;        // 所有的内容都读到此数组之中
            int temp = 0 ;    // 接收每一个内容
            int len = 0 ;        // 读取内容
            while((temp=input.read())!=-1){
                // 如果不是-1就表示还有内容,可以继续读取
                c[len] = (char)temp ;
                len++ ;
            }
            // 第4步、关闭输出流
            input.close() ;                        // 关闭输出流
            System.out.println("内容为:" + new String(c,0,len)) ;    // 把字符数组变为字符串输出
        }
    };
    import java.io.File ;
    import java.io.Writer ;
    import java.io.FileWriter ;
    public class WriterDemo01{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            Writer out = null ;    // 准备好一个输出的对象
            out = new FileWriter(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行写操作
            String str = "Hello World!!!" ;        // 准备一个字符串
            out.write(str) ;                        // 将内容输出,保存文件
            // 第4步、关闭输出流
            out.close() ;                        // 关闭输出流
        }
    };
    import java.io.File ;
    import java.io.Writer ;
    import java.io.FileWriter ;
    public class WriterDemo02{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            Writer out = null ;    // 准备好一个输出的对象
            out = new FileWriter(f,true)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行写操作
            String str = "
    LIXINGHUA
    Hello World!!!" ;        // 准备一个字符串
            out.write(str) ;                        // 将内容输出,保存文件
            // 第4步、关闭输出流
            out.close() ;                        // 关闭输出流
        }
    };
    import java.io.File ;
    import java.io.Writer ;
    import java.io.FileWriter ;
    public class WriterDemo03{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            Writer out = null ;    // 准备好一个输出的对象
            out = new FileWriter(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行写操作
            String str = "Hello World!!!" ;        // 准备一个字符串
            out.write(str) ;                        // 将内容输出,保存文件
            // 第4步、关闭输出流
            // out.close() ;                        // 此时,没有关闭
        }
    };
    import java.io.File ;
    import java.io.Writer ;
    import java.io.FileWriter ;
    public class WriterDemo04{
        public static void main(String args[]) throws Exception{    // 异常抛出,不处理
            // 第1步、使用File类找到一个文件
            File f= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
            // 第2步、通过子类实例化父类对象
            Writer out = null ;    // 准备好一个输出的对象
            out = new FileWriter(f)  ;    // 通过对象多态性,进行实例化
            // 第3步、进行写操作
            String str = "Hello World!!!" ;        // 准备一个字符串
            out.write(str) ;                        // 将内容输出,保存文件
            // 第4步、关闭输出流
            out.flush() ;    // 强制性清空缓冲区中的内容
            // out.close() ;                        // 此时,没有关闭
        }
    };
    import java.io.* ;
    public class InputStreamReaderDemo01{
        public static void main(String args[]) throws Exception{
            File f = new File("d:" + File.separator + "test.txt") ;    
            Reader reader = null ;
            reader = new InputStreamReader(new FileInputStream(f)) ;    // 将字节流变为字符流
            char c[] = new char[1024] ;
            int len = reader.read(c) ;    // 读取
            reader.close() ;    // 关闭
            System.out.println(new String(c,0,len)) ;
        }
    };
    import java.io.* ;
    public class OutputStreamWriterDemo01{
        public static void main(String args[]) throws Exception    {    // 所有异常抛出
            File f = new File("d:" + File.separator + "test.txt") ;    
            Writer out = null ;    // 字符输出流
            out = new OutputStreamWriter(new FileOutputStream(f)) ;    // 字节流变为字符流
            out.write("hello world!!") ;    // 使用字符流输出
            out.close() ;
        }
    };
  • 相关阅读:
    设计模式_享元设计模式(flyweight)
    eclipse的使用
    Bank项目
    反射练习
    反射接口
    反射
    32-一笔画(欧拉图)
    67-蓝桥省赛-2014
    13-STL-二分查找
    31-最长公共子序列
  • 原文地址:https://www.cnblogs.com/tszr/p/12161216.html
Copyright © 2011-2022 走看看