大家好,今天给各位分享outputstream的一些知识,其中也会对FileOutputStream详解进行解释,文章篇幅可能偏长,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在就马上开始吧!
2、FileOutputStream流是指文件字节输出流,专用于输出原始字节流如图像数据等,其继承OutputStream类,拥有输出流的基本特性;
1、FileOutputStream(Filefile):创建文件输出流以写入由指定的File对象表示的文件。
2、FileOutputStream(Filefile,booleanappend)创建文件输出流以写入由指定的File对象表示的文件。
3、FileOutputStream(FileDescriptorfdObj)创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。
4、FileOutputStream(Stringname)创建文件输出流以指定的名称写入文件。
5、FileOutputStream(Stringname,booleanappend)创建文件输出流以指定的名称写入文件
1、voidwrite(byte[]b):将b.length个字节从指定的字节数组写入此文件输出流。
2、oidwrite(byte[]b,intoff,intlen):将len字节从位于偏移量off的指定字节数组写入此文件输出流。
3、voidwrite(intb):将指定的字节写入此文件输出流。
5、练习题:将"hello,java"字符串写入D盘的a.txt文件中
publicstaticvoidmain(String[]args)throwsException{\n\t\t\n\t\tStringpath="D:\\\\a.txt";\n\t\tFileOutputStreamoutputStream=null;\n\t\t\n\t\ttry{\n\t\t\toutputStream=newFileOutputStream(path);\n//单个字节输入到文件中\n\t\t\toutputStream.write('a');\n//字符串形式输入到文件中\n\t\t\toutputStream.write("hello,java".getBytes());\n\t\t}catch(Exceptione){\n\t\t\t//TODO:handleexception\n\t\t}finally{\n\t\t\toutputStream.close();\n\t\t}\n\t}
注意点:newFileOutputStream(path);这种构造方法创建的文件字符输出流每次执行会覆盖源文件里面的内容;FileOutputStream(Stringname,booleanappend)append如果是true,那么字节将被写入文件的末尾,而不是覆盖;
outputstream的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于FileOutputStream详解、outputstream的信息别忘了在本站进行查找哦。