FileInputStream、OutTestAndCopy

package com.javastudy.example10;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOTEST {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\JavaStudy\\src\\com\\javastudy\\example10\\test.txt");

            byte[] b=new byte[4];
            //!!!注意要用临时变量代替读到的内容或者返回的内容,不然再次使用read指针会继续移动,导致结果出问题!!!例如16.21行
            while(true){
                int r=fis.read(b);//读取到的字节数量
                if(r==-1){break;}
                System.out.print(new String(b,0,r));}//将数组转换成字符串并输出
    /*        while(true)
            {
            int r=fis.read();  //read()表示读到的内容
            if(r==-1){break;}
                System.out.println(r);
            }*/



        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //available
        System.out.println();
        FileInputStream fis1=null;
        try{
            fis1 = new FileInputStream("D:\\JavaStudy\\src\\com\\javastudy\\example10\\test.txt");
            System.out.println("总字节数量"+fis1.available());
            byte []b1=new byte[fis1.available()];
            int rc=fis1.read(b1);
            System.out.println(new String(b1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutTestAndCopy {
    public static void main(String[] args) {
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("test",true);//以追加方式写,没有文件会新建
            byte[] bytes={99,100,101};
            String s="文件写入";
            byte[]b2=s.getBytes();
            fos.write(bytes);
            fos.write(b2);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try{
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //文件复制
        FileInputStream fis=null;
        FileOutputStream fos2=null;
        try {
            fis=new FileInputStream("D:\\西南交通大学活动通用模板");
            fos2=new FileOutputStream("D:\\JavaStudy\\西南交通大学活动通用模板.pptx");
            byte[] bytes=new byte[1024*1024];
            int r=0;
            while((r=fis.read(bytes))!=-1){
                fos.write(bytes,0,r);
            }
            fos2.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(fos2!=null){
                    fos2.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try{
                if(fis!=null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


代码二运行结果
image

posted @ 2022-08-29 17:31  零基础科班  阅读(34)  评论(0)    收藏  举报