FileOutputStream类属于字节流,以单个字节的形式存储数据。它可以用来创建文本文件。文件表示在第二存储介质(如硬盘或CD)上存储数据。文件是否可用或是否可以创建取决于底层平台。尤其是某些平台,一次只允许一个FileOutputStream(或其他文件写入对象)打开一个文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造函数将失败。
FileOutputStream用于写入原始字节流,如图像数据。要编写字符流,请考虑使用文件写入器。
重要方法:
- void close(): 关闭此文件输出流并释放与此流关联的所有系统资源。
- 受保护的void finalize(): 清除与文件的连接,并确保在不再引用此文件输出流时调用此文件输出流的close方法。
- 无效写入(字节[]b): 将指定字节数组中的b.length字节写入此文件输出流。
- 无效写入(字节[]b,int off,int len): 从偏移量off开始,将指定字节数组中的len字节写入此文件输出流。
- 无效写入(int b): 将指定字节写入此文件输出流。
要创建存储某些字符(或文本)的文本文件,请遵循以下步骤:
- 读取数据: 首先,应该从键盘读取数据。为此,请将键盘与某个输入流类关联。使用DataInputsRealm类从键盘读取数据的代码如下:
DataInputStream dis =new DataInputStream(System.in);
这里是系统。in表示与DataInputStream对象链接的键盘
- 将数据发送到OutputStream: 现在,将存储数据的文件关联到某个输出流。为此,请借助FileOutputStream,它可以向文件发送数据。附加文件。txt到FileOutputStream的转换方式如下:
FileOutputStream fout=new FileOutputStream(“file.txt”);
- 从DataInputStream读取数据: 下一步是从DataInputStream读取数据并将其写入FileOutputStream。这意味着从dis对象读取数据并将其写入fout对象,如下所示:
ch=(char)dis.read(); fout.write(ch);
- 关闭文件: 最后,在对任何文件执行输入或输出操作后,都应该关闭该文件,否则该文件的数据可能会损坏。关闭文件是通过关闭关联的流来完成的。例如,fout。close():将关闭FileOutputStream,因此无法将数据写入文件。
实施:
//Java program to demonstrate creating a text file using FileOutputStream import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; class Create_File { public static void main(String[] args) throws IOException { //attach keyboard to DataInputStream DataInputStream dis= new DataInputStream(System.in); // attach file to FileOutputStream FileOutputStream fout= new FileOutputStream( "file.txt" ); //attach FileOutputStream to BufferedOutputStream BufferedOutputStream bout= new BufferedOutputStream(fout, 1024 ); System.out.println( "Enter text (@ at the end):" ); char ch; //read characters from dis into ch. Then write them into bout. //repeat this as long as the read character is not @ while ((ch=( char )dis.read())!= '@' ) { bout.write(ch); } //close the file bout.close(); } } |
如果程序再次执行,文件的旧数据将被删除。txt将丢失,任何最近的数据仅存储在该文件中。如果我们不想丢失文件中以前的数据,只需将新数据附加到现有数据的末尾,这可以通过将true与文件名一起写入来实现。
FileOutputStream fout=new FileOutputStream(“file.txt”,true);
使用BufferedOutputStream提高效率
通常,每当我们使用FileOutputStream将数据写入文件时:
fout.write(ch);
这里,调用FileOutputStream将字符写入文件。让我们估算一下从键盘读取100个字符并将其全部写入文件所需的时间。
- 假设使用DataInputStream将数据从键盘读入内存,将1个字符读入内存需要1秒,而FileOutputStream又花费1秒将该字符写入文件。
- 所以读写一个文件需要200秒。这是浪费很多时间。另一方面,如果使用Buffered classed,则它们提供一个缓冲区,该缓冲区首先由缓冲区中的字符填充,这些字符可以立即写入文件。缓冲类应与其他流类连接使用。
- 首先,DataInputStream通过为每个字符花费1秒从键盘读取数据。这个字符被写入缓冲区。因此,要将100个字符读入缓冲区,需要100秒的时间。现在,FileOutputStream将在一个步骤中写入整个缓冲区。因此,读写100个字符只需101秒。同样,阅读课也被用来提高阅读操作的速度。将FileOutputStream附加到BufferedOutputStream作为:
BufferedOutputStream bout=new BufferedOutputStream(fout,1024);
这里,缓冲区大小声明为1024字节。如果未指定缓冲区大小,则使用512字节的默认大小
BufferedOutputStream类的重要方法:
- 无效刷新(): 刷新此缓冲输出流。
- 无效写入(字节[]b,int off,int len): 从offset off开始,将指定字节数组中的len字节写入该缓冲输出流。
- 无效写入(int b): 将指定字节写入此缓冲输出流。
输出:
C:> javac Create_File.java C:> java Create_File Enter text (@ at the end): This is a program to create a file @ C:/> type file.txt This is a program to create a file
相关文章:
本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。