FileOutputStream追加的视频没有延长
时间: 2023-12-03 21:37:39 浏览: 13
Java中的FileOutputStream类可以用于向文件中写入数据。如果你想要追加数据到一个已经存在的文件中,可以使用FileOutputStream的另一个构造函数,该构造函数接受一个布尔类型的参数,用于指定是否追加数据到文件中。如果该参数为true,则数据将被追加到文件的末尾,否则文件将被覆盖。
如果你使用了追加模式,但是视频没有延长,可能是因为视频文件的格式不支持追加操作。视频文件通常是二进制文件,而不是文本文件,因此在追加数据时需要考虑文件格式的特殊性。如果你想要在视频文件中追加数据,可以考虑使用专门的视频编辑软件来进行操作。
相关问题
FileOutputStream追加
`FileOutputStream` 是 Java 的一个输出流类,可以用来向文件写入数据。
基本用法:
```
FileOutputStream fos = new FileOutputStream("文件名");
fos.write(int b); // 向文件写入一个字节
fos.write(byte[] b); // 向文件写入一个字节数组
fos.write(byte[] b, int off, int len); // 向文件写入字节数组的一部分
fos.close(); // 关闭文件输出流
```
例如:
```java
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamExample {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("example.txt");
String str = "Hello, FileOutputStream!";
byte[] b = str.getBytes();
fos.write(b);
fos.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
```
这段代码会在当前目录下生成一个名为 "example.txt" 的文件,并写入字符串 "Hello, FileOutputStream!"。
注意: 每次使用 `FileOutputStream` 写入数据时,它会覆盖文件原有的内容,如果要在文件末尾添加数据,需要在构造函数中传入第二个参数:
```java
FileOutputStream fos = new FileOutputStream("example.txt", true);
```
这样做会打开一个以追加模式打开文件。
FileOutputStream不是没有缓冲区吗
是的,`FileOutputStream`是没有缓冲区的。每次调用`write()`方法都会直接将数据写入到磁盘中,因此效率较低。如果需要提高写入效率,可以使用`BufferedOutputStream`类来提供缓冲区支持。`BufferedOutputStream`类内部维护一个缓冲区,每次调用`write()`方法时,将数据先写入缓冲区,当缓冲区满时再一次性将数据写入磁盘中,从而提高写入效率。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)