您的位置:首页 > 编程语言 > Java开发

[置顶] Java IO之FileInputStream&FileOutputStream

2017-09-18 15:49 363 查看

InputStream和OutputStream之间的对应关系

InputStream与OutputStream及其子类间具有很强的对称性。下图很形象地展现出它们之间的对应关系,只有PrintStream是没有对应的。



1.FileInputStream 和 FileOutputStream

java.io.FileInputStream类从文件系统中的一个文件中获取字节。那些文件依赖于主机环境:

- 这个类是指对原始字节诸如图像数据读出流。

- 对于字符读取流则使用FileReader。

Java.io.FileOutputStream 类是用于将数据写入一个文件或FileDescriptor的输出流。以下是关于文件输出流的要点:

- 这个类是指用于记录的原始字节,例如图像数据流。

- 写字符流,可以使用文件字符 FileWriter

1.1.类声明

FileInputStream

public class FileInputStream extends InputStream


FileOutputStream

public class FileOutputStream extends OutputStream


1.2.构造函数

FileInputStream

SN#构造函数描述
1FileInputStream(File file)通过打开文件系统中的File对象创建一个FileInputStream
2FileInputStream(FileDescriptor fd)使用文件描述符,在文件系统中某个实际文件的现在连接创建一个FileInputStream
3FileInputStream(String name)通过路径名创建一个FileInputStream到实际文件的一个连接
FileOutputStream

SN#构造函数描述
1FileOutputStream(File file)创建一个文件输出流写入到指定的File对象
4FileOutputStream(File file, boolean append)创建文件输出流写入到指定的File对象表示的文件
2FileOutputStream(FileDescriptor fd)这将创建一个输出文件流写入到指定的文件描述符,它代表了文件系统中的某个实际文件的现有连接
3FileOutputStream(String name)这将创建一个输出文件流写入到具有指定名称的文件
3FileOutputStream(String name,boolean append)这将创建一个输出文件流写入到具有指定名称的文件

1.3.方法

FileInputStream

- int available(): 此方法从输入流中通过一个方法的下一次调用阻塞该输入流返回可以读取(或跳过)的剩余字节数的估计值;

- void close(): 此方法关闭此文件输入流并释放与该流关联的所有系统资源;

- protected void finalize(): 此方法可确保此文件输入流的close方法被调用当它没有更多的参考引用;

- FileChannel getChannel(): 此方法返回与此文件输入流关联的唯一文件通道对象;

- FileDescriptor getFD(): 此方法返回FileDescriptor对象,表示连接到正在使用此文件输入流文件系统的实际文件;

- int read(): 该方法返回数据的下一个字节,或如果到达文件的结尾则为-1;

- int read(byte[] b): 该方法将返回读入缓冲区的总字节数,或如果没有更多的数据被读取则返回-1。

- int read(byte[] b, int off, int len): 此方法读取最多len个从这个输入流中数据的字节到字节数组;

- long skip(long n): 此方法跳过并丢弃n个字节从输入流中的数据;

FileOutputStream

- void close(): 此方法关闭此文件输出流并释放与此流有关的所有系统资源;

- protected void finalize(): 这种方法清除连接到文件,并确保此文件输出流的close方法被调用时,此流不再有引用;

- FileChannel getChannel(): 此方法返回与此文件输出流关联的唯一文件通道对象;

- FileDescriptor getFD(): 此方法返回与此流有关的文件描述符;

- void write(byte[] b): 此方法写入b.length个字节从指定的字节数组到该文件输出流;

- void write(byte[] b, int off, int len): 此方法从指定的字节数组开始到该文件输出流关闭写入len字节;

- void write(int b): 此方法写入指定的字节写入此文件输出流;

1.4.实例

FileInputStream

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
* Created by david.tian on 18/09/2017.
*/
public class FileInputStreamDemo {
public static Logger LOG = LoggerFactory.getLogger(FileInputStreamDemo.class);
public static void main(String[] args) throws IOException{
FileInputStream fis = null;
int available = 0;
int i =0;
FileChannel fc = null;
long pos;
char cc;

FileDescriptor fd = null;
boolean bl = false;

byte[] bt = new byte[10];
int j=0;
char aa;

try{
//create new file input stream
fis = new FileInputStream("/Users/david.tian/Documents/Kudu/database/kudu");

fd = fis.getFD(); //get the file descriptor

bl = fd.valid(); // test fi the file is valid or invalid.

//prints
System.out.println("Valid File: "+ bl);

//read bytes to the buffer
j = fis.read(bt);
System.out.print("Number of bytes read: "+ i);
System.out.println(" Bytes read: ");
//for each byte in buffer
for(byte b: bt){
//converts byte to character
aa = (char)b;
//print
System.out.println("aa: =====>"+aa);
}

//read till the end of the stream
while((i=fis.read())!=-1){
//available bytes
available = fis.available();
//get the file channel
fc = fis.getChannel();
pos = fc.position(); //get channel position

//convert integer to character
char c = (char)i;

//prints
System.out.print("Available: ===========>"+available);
System.out.println("; Read: "+c);

System.out.print("NUM of bytes read:======> "+pos);
System.out.println("; Char read: "+c);
}
} catch (Exception e){
e.printStackTrace();
} finally {
if(fis!=null){
fis.close();
}
if(fc!=null){
fc.close();
}
}
}
}


输出: 假如文件/Users/david.tian/Documents/Kudu/database/kudu中含有数据:ABCDEFGHIJKLMNOPQRSTUVWXYZ

Valid File: true
Number of bytes read: 0 Bytes read:
aa: =====>A
aa: =====>B
aa: =====>C
aa: =====>D
aa: =====>E
aa: =====>F
aa: =====>G
aa: =====>H
aa: =====>I
aa: =====>J
Available: ===========>15; Read: K
NUM of bytes read:======> 11; Char read: K
Available: ===========>14; Read: L
NUM of bytes read:======> 12; Char read: L
Available: ===========>13; Read: M
NUM of bytes read:======> 13; Char read: M
Available: ===========>12; Read: N
NUM of bytes read:======> 14; Char read: N
Available: ===========>11; Read: O
NUM of bytes read:======> 15; Char read: O
Available: ===========>10; Read: P
NUM of bytes read:======> 16; Char read: P
Available: ===========>9; Read: Q
NUM of bytes read:======> 17; Char read: Q
Available: ===========>8; Read: R
NUM of bytes read:======> 18; Char read: R
Available: ===========>7; Read: S
NUM of bytes read:======> 19; Char read: S
Available: ===========>6; Read: T
NUM of bytes read:======> 20; Char read: T
Available: ===========>5; Read: U
NUM of bytes read:======> 21; Char read: U
Available: ===========>4; Read: V
NUM of bytes read:======> 22; Char read: V
Available: ===========>3; Read: W
NUM of bytes read:======> 23; Char read: W
Available: ===========>2; Read: X
NUM of bytes read:======> 24; Char read: X
Available: ===========>1; Read: Y
NUM of bytes read:======> 25; Char read: Y
Available: ===========>0; Read: Z
NUM of bytes read:======> 26; Char read: Z


FileOutputStream

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
* Created by david.tian on 18/09/2017.
*/
public class FileOutputStreamDemo {
public static Logger LOG = LoggerFactory.getLogger(FileInputStreamDemo.class);

public static void main(String[] args) throws IOException{
FileOutputStream fos = null;

FileChannel fc = null;
FileDescriptor fd = null;
long pos;
byte b[] ={65,66,67,68,69,70,71,72,73,74,75,76};

try{
//create new file output stream
fos = new FileOutputStream("/Users/david.tian/Documents/Kudu/database/mysql");

//write buffer to the output stream
fos.write(b);

//pushes stream to the file
fos.flush();

//returns file channel associated with this stream
fc = fos.getChannel();

//get file descriptor instance
fd = fos.getFD();
boolean bl = fd.valid();

//returns the number of bytes written
pos = fc.position();

//prints
System.out.println("position==============>"+fc.position());
System.out.println("Is file valid?==============>"+bl);
} catch(Exception e){
e.printStackTrace();
} finally{
if(fos!=null){
fos.close();
}
if(fc!=null){
fc.close();
}
}
}

}


输出:假设/Users/david.tian/Documents/Kudu/database/mysql为空,运行以后:

position==============>12
Is file valid?==============>true

文件中也写入了相应的内容:
ABCDEFGHIJKL
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐