Java学习之FileInputStream和FileOutputStream类

若创建的是文件输入流 若文件路径有问题或文件不存在  则抛出异常

若创建的是文件输出流 若文件不存在 则按照路径直接创建一个新文件,但路径错误会抛异常

目录

FileInputStream

构造方法

常用方法

FileOutputStream

构造方法

常用方法

方法实现


FileInputStream

构造方法

1.FileInputStream(File file)//向文件输入流中传入一个文件

2.FileInputStream(String name)//向文件输入流中传入一个文件的路径

3.FileInputStream(FileDescriptor fdObj)//不了解

//1.FileInputStream(File file)
File file = new File("D://test//test.txt");
FileInputStream fis = new FileInputStream(file);


//2.FileInputStream(String name)
FileInputStream fis = new FileInputStream("D://test//test.txt");

常用方法

1. int  available() 返回的是一个文件可以读取的字节数(跳过的、已经读取的都不算)

 

2.void close() 关闭文件输入流 会抛出IO异常,应该在finally中关闭,需要严谨的判断一下

FileInputStream fis = null;
        try {
            fis= new FileInputStream ("D:/test/test.txt");
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally{
            //防止fis在创建时就出现异常导致fis为null
            if(fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

 read方法每次调用都会弹出fis管道中的字节数据

3.int read() 读入一个数据字节,以Unicode码形式返回

4.int read(byte[])读入一组数据字节,根据数组长度而定,返回值是读入的数据字节数

5.int read(byte[] b, int off, int len)

5.long skip(long n)从输入流中跳过并且丢弃掉n个字节的数据 

 

FileOutputStream

构造方法

1.FileOutputStream(File file)//创建一个输出流,可以通过流管道向file对象中输出,通过file的映射关系写入到文件中

无论文件存在与否每次都会创建一个新的文件

2.FileOutputStream(File file, boolean append)//和1相同,但是如果文件存在就不会创建新文件而是向已有的文件内容后面输出内容

3.FileOutputStream(String name)//传入路径名创建对象

4.FileOutputStream(String name, boolean append)//原理同2

5.FileOutputStream(FileDescriptor fdObj)//不了解

常用方法

1. void   close() //关闭输出流

2. void   write(byte[] b)//将数组内的byte值按照Unicode码转换成相应的字符写入文件中

3. void   write(int b)

4.write(byte[] b, int off, int len)//从off索引开始写入len长度的byte数组子集

 

方法实现

package test_file_stream;

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

public class FileTest {
    
    //文件的复制
    public void copy(File sourseFile, String path) {//传一个源文件,给一个新文件的存放路径
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(sourseFile);
            fos = new FileOutputStream(path+"//"+sourseFile.getName(),true);
            byte[] b = new byte[1024];
            int count = fis.read(b);
            while(count != -1) {
                fos.write(b,0,count);//读取有效的元素写入
//                fos.flush();
                count = fis.read(b);
            }
            System.out.println("文件复制成功:" + sourseFile.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件路径出错!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fis != null) {
                fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    
    //文件加密
    public void encryption(File file,String path) {
        File newFile = new File(path);
        newFile.mkdir();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file);
            fos = new FileOutputStream(new File(path + "//" + file.getName()));
            byte[] b = new byte[1024];
            int count = fis.read(b);
            while(count != -1) {
                //加密算法  交换元素,再次调用可以解密
                int temp = b[10];
                b[10] = b[9];
                b[9] = (byte) temp;
                fos.write(b,0,count);
                fos.flush();
                count = fis.read(b);
            }
            System.out.println("加密成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件路径出错!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fis != null) {
                fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    //文件夹的复制
    public void copyDirectory(File file, String path) {
        
        if(file.exists()) {         //如果file文件或文件夹存在
            File[] files = file.listFiles();
            
            if(files!=null) {       //说明file是文件夹
                File copyFile = new File(path + "//" + file.getName());
                if(copyFile.mkdirs()) {
                    System.out.println("文件夹复制成功:" + copyFile.getName() );
                }
                if(files.length!=0) {       //说明file不是空文件夹,需要继续查看file的内部元素
                    for(File f : files) {
                        copyDirectory(f, copyFile.getAbsolutePath());//递归调用
                    }
                }
            }else {//说明file是文件
                this.copy(file, path);//复制文件到目录下
            }
        }else
            System.out.println("文件或路径不存在");
        
    }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值