Java中IO流文件读取、写入和复制的实例

//构造文件File类
File f=new File(fileName);

//判断是否为目录
f.isDirectory();

//获取目录下的文件名
String[] fileName=f.list();

//获取目录下的文件
File[] files=f.listFiles();

1、Java怎么读取文件

 /*

 * 读取文件:

 * 1、找到指定的文件

 * 2、根据文件创建文件的输入流

 * 3、创建字节数组

 * 4、读取内容,放到字节数组里面

 * 5、关闭输入流

 */

public class FileInputStreamDemo2 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            //1.创建IO流对象
            fis = new FileInputStream("d:/hello.txt");
            //2.进行读写操作
            int rs = -1;
            while((rs=fis.read()) != -1){
                System.out.println((char)rs);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //3.关闭
            try {
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

/**
 * 使用FileInputStream读取文件的内容
 */
public class FileInputStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            //1.创建IO流对象
            fis = new FileInputStream("d:/hello.txt");
            //2.进行读写操作
            int rs = -1;
            //一个中文占三个字节
            byte[] b = new byte[5];//加速  设置更大的数组
            //byte[] b = new byte[1024*1024]; //1MB   缓冲区
            while((rs=fis.read(b)) != -1){
                //read() 一下, 只读一个字节  编码  返回的读到内容编码
                //read(byte[] b) 把读到字节缓存到byte数组中 返回的是 读到的字节长度
                //System.out.print((char)rs);
                //把byte[] 转换为字符串
                System.out.println(rs);
                //String(byte[], offset, len)
                // 从byte[] 数组中指定offset下标开始, 读len个字节
                String s = new String(b,0,rs);
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //3.关闭
            try {
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

2、Java怎么写入文件

/*

 * 实现思路:

 * 1、构建源文件与目标文件

 * 2、源文件创建输入流,目标文件创建输出流

 * 3、创建字节数组

 * 4、使用循环,源文件读取一部分内容,目标文件写入一部分内容,直到写完所有内容

 * 5、关闭源文件输入流,目标文件输出流

 */

/**
 * FileOutputStream往文件写入内容
 */
public class FileOutputStreamDemo {
    public static void main(String[] args) {

        FileOutputStream  fos = null;
        try {
            //1.创建IO对象
            fos = new FileOutputStream("d:/a.txt",true);
            //2.写
            String  str = "\nhello world\n";
            //需要把字符串转换为byte数组
            byte[] bytes = str.getBytes();
            fos.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //3.关闭
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }
}

3、Java怎么复制文件

 public static void copy2(File source, File dest) throws IOException {
        //判断源文件是否存在
        if(!source.exists()){
            throw new FileNotFoundException("源文件不存在!!");
        }
        //输入流
        FileInputStream fis  = null;
        FileOutputStream fos  = null;
        fis = new FileInputStream(source);//源文件
        fos = new FileOutputStream(dest);//目标文件
        //一个读,一个写
        int rs = -1;
        byte[] b = new byte[1024*1024]; //1MB   缓冲区
        //读
        while((rs = fis.read(b)) != -1){
            //写
            System.out.println(rs);
            fos.write(b,0,rs);
        }
        //倒序关  先开的后关, 后开的先关
        if(fos != null){
            fos.close();
        }
        if(fis != null){
            fis.close();
        }
    }

4、Java怎么剪贴文件

    //剪贴
    public static void clip(File source, File dest)   {
        //判断源文件是否存在
        if(!source.exists()){
            try {
                throw new FileNotFoundException("源文件不存在!!");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        //输入流
        FileInputStream fis  = null;
        FileOutputStream fos  = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(dest);
            //一个读,一个写
            int rs = -1;
            byte[] b = new byte[1024*1024]; //1MB
            //读
            while((rs = fis.read(b)) != -1){
                //写
                System.out.println(rs);
                fos.write(b,0,rs);

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //倒序关  先开的后关, 后开的先关
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        source.delete();//将源文件删除


    }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值