Java - IO文件输出流FileOutputStream

知识点介绍

文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。文件是否可用或能否可以被创建取决于基础平台。特别是某些平台一次只允许一个 FileOutputStream(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。

FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用FileWriter。

请自行查阅相关知识点。

本次演示功能介绍

1. 使用fileoutputstream定义文件并且写入一些字符。

2. 全部替换文件内的字符。

3. 拼接文件内的字符,每次appaend.

全部替换文件内的字符

@Test
    public void writeFile(){
//        创建OutputStream对象
        FileOutputStream fileOutputStream = null;
        String filePath = "C:\\Users\\JinZh\\IdeaProjects\\TestJa\\software\\file02.txt";
        try {
            fileOutputStream = new FileOutputStream(filePath);
//            写入一个字节
            String str = "hello xiaoyu " + System.currentTimeMillis();
            fileOutputStream.write(str.getBytes());
            System.out.println("写文件成功 : " + str);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

拼接文件内的字符

只需要将构造器加一个参数即可。采用以下构造器。

    public FileOutputStream(String name, boolean append)
        throws FileNotFoundException
    {
        this(name != null ? new File(name) : null, append);
    }

那么,在上一份代码中我们只需要改动这一行代码,添加一个true参数。

            fileOutputStream = new FileOutputStream(filePath);
            fileOutputStream = new FileOutputStream(filePath, true);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤实现: 1. 创建一个Map对象,将需要存储的对象放入其中。 例如,假设需要存储一个名为person的对象,可以这样创建Map对象: ``` Map<String, Object> map = new HashMap<>(); map.put("person", person); ``` 2. 创建一个文件输出对象和一个对象输出对象。 ``` FileOutputStream fileOutputStream = new FileOutputStream("map.txt"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); ``` 3. 使用对象输出对象将Map对象写入到文件中。 ``` objectOutputStream.writeObject(map); ``` 4. 关闭对象输出文件输出。 ``` objectOutputStream.close(); fileOutputStream.close(); ``` 完整的代码示例如下: ``` import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { // 创建一个Person对象 Person person = new Person("Tom", 20); // 创建一个Map对象,将Person对象放入其中 Map<String, Object> map = new HashMap<>(); map.put("person", person); try { // 创建文件输出和对象输出 FileOutputStream fileOutputStream = new FileOutputStream("map.txt"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); // 将Map对象写入文件中 objectOutputStream.writeObject(map); // 关闭输出 objectOutputStream.close(); fileOutputStream.close(); } catch(IOException e) { e.printStackTrace(); } } } class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } ``` 执行完上述代码后,文件"map.txt"中将会存储一个包含了Person对象的Map对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值