• 2-字节流


    package com.io;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.junit.Test;
    /**
     * 
     * @author Administrator
     *1、流的分类
     *(1)按照数据流向的不同,分为输入流和输出流
     *(2)按照处理数据单位的不同,分为字节流和字符流
     *(3)按照角色的不同,分为节点流(直接作用于文件,所有带File的InputStream OutputStream)、处理流(作用于节点流上面,提高效率)
     *2、IO体系
     *抽象基类                    节点流(即文件流)            缓总流(处理流的一种)
     *InputStream   FileInputStream         BufferedInputStream
     *OutputStream  FileOutputStream         BufferedOutputStream
     *Reader        FileReader                BufferedReader
     *Writer        FileWriter                BufferedWriter
     *3、所有的处理流都作用于上面四种节点流,才能进行对文件操作
     */
    public class FileInputOurPutStreamTest {
        
        /**
         * 每次读一个字节
         */
        @Test
        public void fileInputStreamTest1(){
            FileInputStream fis = null;
            //1、先创建file对象
            try { //有异常在这里捕获,因为fis使用完后一定要关闭
                File file1 = new File("hello1.txt");
                //2、创建FileInputStream对象
                fis = new FileInputStream(file1);
                /**
                int b = fis.read();//read方法,一次读取一个字节,读到文件最后返回-1
                while(b != -1){
    //                System.out.println(b);//因为读取的是字节,这里用int接收,所以打印出来是unicode码,要显示字母,加char强制转化
                    System.out.println((char)b);
                    b = fis.read();
                }**/
            //上面代码可以简写成
                int b ;
                while( (b = fis.read()) != -1 ){
                    System.out.print((char)b);
                }
            } catch ( IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        
        /**
         * 每次读一个数组长度的字节
         */
        @Test
        public void fileInputStreamTest2(){
            File file = new File("hello1.txt");
            FileInputStream fis = null;
            try{
                fis = new FileInputStream(file);
                byte[] b = new byte[5];
                int len;//len是每次读出放到数组中的字节数,当到文件末尾的时候返回-1,前几个len都返回的是数组的长度,最后一个返回的len<=数组长度
                StringBuffer strTmp = new StringBuffer("");
                while( (len = fis.read(b)) != -1 ){
                    System.out.println(len);
                    String str = new String(b, 0, len);//这里要注意new String的三个参数,第一个是数组,第二个是从数组的第几个下标开始读,最后一个,是最后一个数组的长度
                    System.out.println(str);
                    strTmp.append(str);
                }
                System.out.println("strTmp-===" + strTmp);
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        @Test
        public void fileOutPutStreamTest(){
            File file = new File("hello_out.txt");
            FileOutputStream fos = null;
            
            try{
                fos = new FileOutputStream(file);
                byte[] strTmp = new String("I love china").getBytes();
                fos.write(strTmp);
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 从一个文件读取内容,写入到另外一个文件
         * 即文件的复制
         */
        @Test
        public void fileInputOutputStreamTest(){
            FileInputStream fis = null;
            FileOutputStream fos = null;
            File fileIn = new File("hello1.txt");
            File fileout = new File("hello2.txt");
            try{
                fis = new FileInputStream(fileIn);
                fos = new FileOutputStream(fileout);
                int len;
                byte [] b = new byte[20];//根据文件大小来设定数组的大小
                while((len = fis.read(b)) != -1){
                    fos.write(b, 0, len);//0是从字节数组b的的第0位开始
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            
        }
    
    }
  • 相关阅读:
    python 正则
    python qrcode二维码生成与识别
    python 轮询,长轮询
    Python随心记--module模块和包
    PHP-AOP简介
    PHP三层结构(下)——PHP实现AOP
    PHP三层结构(上)——简单三层结构
    PHP--翻牌活动(概率中奖..............)
    MySQL(表锁)、PHP(文件锁)锁机制及应用场景
    TP5一对一、一对多关联模型的使用
  • 原文地址:https://www.cnblogs.com/fubaizhaizhuren/p/5026116.html
Copyright © 2020-2023  润新知