File操作-InputStream/OutputStream及是否创建文件

处理的都是字节流,写入的时候也为字节流,不能直接写入中文字符

1.实现文件复制

示例代码如下:

public void copyFile(String src, String dest) {
        // 1.提供读入、写出的文件
        File file1 = new File(src);
        File file2 = new File(dest);
        // 2.提供相应的流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            // 3.实现文件的复制
            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1) {
                // fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
                fos.write(b, 0, len);
            }
        } catch (Exception 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();
                }
            }

        }
    }

2.FileOutputStream写入文件

写入的时候一定要转换为字节,getBytes();如果目标文件不存在,则FileOutputStream会创建目标文件(前提是父路径文件对象必须存在)。

如果文件存在,会将原有的文件覆盖。若加上参数true,则为append。

@Test
    public void testFileOutputStream() {
        // 1.创建一个File对象,表明要写入的文件位置。
        File file = new File("test7.txt");
        // 2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
        FileOutputStream fos = null;
        try {
            // 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
            //若加上参数true,则为append;不加,则为覆盖
            //fos = new FileOutputStream(file,true);
            fos = new FileOutputStream(file);
            // 3.写入的操作--将String转换为字节数组
            fos.write(new String("I love China,你呢!").getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 4.关闭输出流
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

3.FileInputStream读出文件

只能处理字节流,英文字符,不能处理中文字符


    @Test
    public void testFileInputStream3() { // abcdefgcde
        FileInputStream fis = null;
        try {
            File file = new File("test7.txt");
            fis = new FileInputStream(file);
            byte[] b = new byte[5];// 读取到的数据要写入的数组。
            int len;// 每次读入到byte中的字节的长度,若无则返回-1
            while ((len = fis.read(b)) != -1) {
                // for (int i = 0; i < len; i++) {
                // System.out.print((char) b[i]);
                // }
                String str = new String(b, 0, len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



/*可以处理中文字符和英文字符*/
    @Test
    public void testFileInputStream4() { // abcdefgcde
        FileInputStream fis = null;
        try {
            File file = new File("hello.txt");
            fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            char[] b = new char[5];// 读取到的数据要写入的数组。
            int len;// 每次读入到char[]中的长度,若无则返回-1
            while ((len = isr.read(b)) != -1) {
                 System.out.println(len);
                 for (int i = 0; i < len; i++) {
                 System.out.print((char) b[i]);
                 }
                String str = new String(b, 0, len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }
    }

【Tips】:

在Java中,new File("name")不会创建新文件,在new File()之后需要调用createNewFile()才能创建新文件;

new FileOutputStream("name")会产生一个新文件;

new FileInputStream("name")不会创建新文件,若文件不存在会报错;

file=new File("name")+new FileInputStream(file)

不会创建新文件,若文件不存在也会报错;

file=new File("name")+new FileOutputStream(file),会创建新文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值