Java核心API之FileOutputStream使用介绍
紧接着小编上一篇《Java核心API之RandomAccessFile使用介绍》,这次进阶介绍Java核心API中文件的输入输出字节流
文件的输入输出字节流和RandomAccessFile的功能基本一样,都可以以字节的方式读取文件,那么它们之间有什么不同呢?
字节流底层是基于流式的,流就像“流水”一样,只会向前,不会倒退,同时字节流可以与其他高级流结合使用。而RandomAccessFile是基于指针读取文件的,RandomAccessFile可以移动指针读取文件的任意位置,但不会像流与其他流结合使用。
输入输出字节流的介绍
讲到输入和输出的概念,小编在这里必须啰嗦一句,输入和输出对不同参考系概念不同,对于程序而言,输入就是读,也就是用来读取数据;输出就是写,也就是用来写入数据。
字节流可以从特定的数据源读取数据(字节),也可以向一个特定的地方写入数据(字节)。
InputStream是所有字节输入流的父类,常用方法有read()和read(byte[] b);OutputStream是所有字节输出流的父类,常用方法有write(int d)和write(byte b)
FileOutputStream的介绍和使用
显然,FileOutputStream的父类是OutputStream,OutputStream有许多子类,FileOutputStream只是其中之一,该类可以以字节为单位将数据写入文件。
示例代码如下:
/*
* 常用两种构造方法:
* FileOutputStream(File file)
* 该流向File对象表示的文件写出数据
*
* FileOutputStream(String filename)
* 该流向指定文件名的文件写出数据
*/
@Test
public void testFileOutputStreamConstructor() throws Exception{
File file = new File("test_stream.txt");
//FileOutputStream(File file)构造方法
FileOutputStream fos = new FileOutputStream(file);
//FileOutputStream(String filename)构造方法
FileOutputStream fos2 = new FileOutputStream("test_stream.txt");
System.out.println("构造方法创造成功");
}
分析:上方代码可以看出FileOutputStream与RandomAccessFile就构造方法而言有许多相似之处。
注意:FileOutputStream(String filename)构造方法创建对象,如果文件不存在会自动创建。
FileOutputStream的write方法介绍
示例代码:
/*
* FileOutputStream继承父类的几个常用方法:
* void write(int d)
* 将指定字节写入此文件输出流,参数是只给定的int值的"低八位",也就是一个字节
*
* void write(byte[] b)
* 将指定的byte数组中的数据写入此文件输出流中
*
* void write(byte[],int offset,int len)
* 将指定byte数组中从偏移量offset开始的len个字节
*/
@Test
public void testWrite() throws IOException{
File file = new File("test_stream.txt");
FileOutputStream fos = new FileOutputStream(file);
//方法一:void write(int d)
fos.write(1);
//方法二:void write(byte[] b)
//将字符串转换成字节数组
byte[] b = "HelloWorld".getBytes();
fos.write(b);
//方法三:void write(byte[],int offset,int len)
fos.write(b,0,b.length);
System.out.println("写入完成");
}
FileOutputStream的write方法也与RandomAccessFile的write方法相同。
FileOutputStream的追加模式
FileOutputStream用通常的构造方法创造的对象,对文件进行写操作会覆盖文件中原有数据。如果想在文件的原有数据后追加新数据需要在构造方法中添加另一个参数。
示例代码如下:
/*
* FileOutputStream(File file,boolean append)
*
* FileOutputStream(File filename,boolean append)
*
* 上面两个构造方法中,第二个参数传入true,那么通过FOS对象写出的数据都是文件末尾追加的。
*/
@Test
public void testWriteByAppend() throws Exception{
FileOutputStream fos = new FileOutputStream("test_stream.txt",true);
byte[] buf = "HelloWorld".getBytes();
fos.write(buf);
}
FileInputStream的构造方法
示例代码:
/*
* FileInputStream(File file)
* 从一个指定File对象表示的文件中读取数据
*
* FileInputStream(String pathname)
* 根据给定的文件系统中的路径名pathname所指向的文件中读取数据
*/
@Test
public void testRead() throws Exception{
File file = new File("test_stream.txt");
//FileInputStream(File file)构造方法
FileInputStream fis = new FileInputStream(file);
//FileInputStream(String pathname)构造方法
FileInputStream fis2 = new FileInputStream("test_stream.txt");
System.out.println("构造方法创造成功");
}
注意:使用FileInputStream创建对象,如果所要读取的文件不存在,它不会创建该文件,而程序会抛出FileNotFoundException异常。
FileInputStream的read方法介绍
示例代码
/*
* FileInputStream继承父类常用的几个read方法
* int read()
* 从该输入流中读取一个数据字节,若返回值是-1表示读到文件末尾
*
* int read(byte[] b)
* 从该输入流中读取将最多数组长度的字节数读入到字节数组中
*/
@Test
public void testReadMethod() throws Exception{
//test_inputstream.txt的内容是 Hello,everyone!My name is dragonhegler.
FileInputStream fis = new FileInputStream("test_inputstream.txt");
int d = -1;
System.out.print("文件一的内容是:");
while((d=fis.read())!=-1){
System.out.print((char)d);
}
fis.close();
System.out.print("\n");
//test_inputstream_2.txt的内容是I love programming,I am good at JAVA, Web.
FileInputStream fis2 = new FileInputStream("test_inputstream_2.txt");
byte[] buf = new byte;
int len = -1;
System.out.print("文件二的内容是:");
while((len=fis2.read(buf))!=-1){
System.out.print(new String(buf,"UTF-8"));
}
fis2.close();
}
注意:String(byte[],"编码集") 可以将字节数组按照指定的编码及转换为字符
版权声明:本站【趣百科】文章素材来源于网络或者用户投稿,未经许可不得用于商用,如转载保留本文链接:https://www.qubaik.com/life/185611.html