IO流之FileOutputStream

IO流之FileOutputStream

普通写入文件

以下代码使用FileOutputStream把数据写入硬盘文件:

package com.javalearn.io.fileoout;

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

public class TestForFileOutputStream {
    public static void main(String[] args) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("myfile");  // 先将原文件清空,再写入,谨慎使用。若无此文件则新建。在文件名后添加参数true,则是在原文件末尾追加内容,不会清空。
            byte[] bytes = {97,98,99,100};
            fileOutputStream.write(bytes);  
            fileOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

输出文件内容:

abcd

把字符串写入文件

把字符串写入文件只需把字符串转化为字节数组即可:

package com.javalearn.io.fileoout;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class TestForFileOutputStream {
    public static void main(String[] args) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("myfile",true);  // myfile文件中有abcdabcd 8个字符
            String s = "今天晚上吃火锅怎么样?";
            byte[] bytes = s.getBytes(StandardCharsets.UTF_8);  // 字符串转为字节数组
            fileOutputStream.write(bytes);
            fileOutputStream.flush();  // output别忘了flush
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

输出文件内容:

abcdabcd今天晚上吃火锅怎么样?

文件复制

以下代码使用FileInputStream和FileOutputStream进行文件复制:

package com.javalearn.io.copy;

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

public class TestForCopy {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            // 任何文件都能复制
            fileInputStream = new FileInputStream("D:\\typora笔记\\java\\io流\\IO流之FileInputStream.md");
            fileOutputStream = new FileOutputStream("D:\\typora笔记\\java\\io流\\临时文件夹\\复制到这个文件.md");  // 把数据复制到一个新的文件内,而不是复制到一个文件夹中,需要具体到文件类型
            byte[] bytes = new byte[1024*1024];  // 一次复制1M
            int count = 0;
            while((count = fileInputStream.read(bytes))!=-1) {  // 读到内存
                fileOutputStream.write(bytes,0,count);  // 写到硬盘
            }
            fileOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {  //input的close和output的close需要分开try-catch,因为如果其中1个出现异常会让另一个关闭不了。
            if(fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream!=null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

posted on 2021-12-04 15:09  菜小疯  阅读(481)  评论(0)    收藏  举报