Java流总结

一、  流的分类

– 输入流:只能从中读取字节数据,而不能向其写出数据 – 输出流:只能向其写入字节数据,而不能从中读取数据 •   按照流所处理的数据类型 – 字节流:用于处理字节数据。 – 字符流:用于处理Unicode字符数据。 •   按照流所处理的源 – 节点流:从/向一个特定的IO设备读/写数据的流。(低级流) – 处理流:对已存在的流进行连接和封装的流。(高级流)

二、  缓冲流

冲的功能,提高了读写的效率,同时增加了一些新的方法。 •  J2SDK提供了四种缓存流: – BufferedReader – BufferedWriter – BufferedInputStream s – BufferedOutputStream – mark()用于“标记”当前位置,就像加入了一个书签,可以使用reset()方法返回这个标记重新读取数据。 •  BufferedReader提供了readLine()方法用于读取一行字符串(以\r或\n分隔)。 •  BufferedWriter提供了newLine()用于写入一个行分隔符。 •  对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()方法将会使内存中的数据立刻写出。

三、  类层次

3.1、InputStream类层次

 

Java I/O流InputStream,OutputStream,Reader,Writer_读取文件

3.2、OutputStream类层次

Java I/O流InputStream,OutputStream,Reader,Writer_读取文件_02

3.3、Reader类层次 

Java I/O流InputStream,OutputStream,Reader,Writer_InputStream_03

3.4、Writer类层次

Java I/O流InputStream,OutputStream,Reader,Writer_Reader_04

四、  常用的字符流与字节流的转化

Java I/O流InputStream,OutputStream,Reader,Writer_读取文件_05

说明:

1.     字节流用于读写诸如图像数据之类的原始字节流。 2.     字符流用于读写诸如文件数据之类的字符流。 3.     低级流能和外设交流。 4.     高级流能提高效率。 5. InputStreamReader 是字节流通向字符流的桥梁。 6. OutputStreamWriter是字符流通向字节流的桥梁。

五、  代码实例

5.1、常用读文件:

/**
*以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*@paramfileName:文件的名
*/
publicstaticvoid readFileByBytes(String fileName) {
File file= new File(fileName);
InputStream in = null;
try {
logger.debug("以字节为单位读取文件内容,一次读多个字节:");
/*一次读多个字节*/
byte[] tempbytes =newbyte[100];
int byteread = 0;
in = new FileInputStream(file);
/*读入多个字节到字节数组中,byteread为一次读入的字节数*/
while ((byteread = in.read(tempbytes)) != -1) {
logger.debug(tempbytes);
logger.debug(0);
logger.debug(byteread);
}
} catch (Exception e1) {
logger.error("读取文本文件异常",e1);
} finally {
if (in !=null) {
try {
in.close();
} catch (IOException e1) {
logger.error("读取文本文件异常",e1);
}
}
}
}
/**
*以字符为单位读取文件,常用于读文本,数字等类型的文件
*@paramfileName:文件名
*/
public staticvoid readFileByChars(StringfileName) {
Reader reader = null;
try {
logger.debug("以字符为单位读取文件内容,一次读多个字节:");
/*一次读多个字符*/
char[] tempchars =newchar[100];
int charread = 0;
if(fileName!=null&&!"".equals(fileName)){
reader = new InputStreamReader(new FileInputStream(fileName));
/*读入多个字符到字符数组中,charread为一次读取字符数*/
while ((charread = reader.read(tempchars)) != -1) {
/*对于windows下,rn这两个字符在一起时,表示一个换行。*/
/*但如果这两个字符分开显示时,会换两次行。*/
/*因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。*/
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] !='r')) {
logger.debug(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] =='r') {
continue;
} else {
logger.debug(tempchars[i]);
}
}
}
}
}
} catch (Exception e1) {
logger.error("读取文本文件异常",e1);
} finally {
if (reader !=null) {
try {
reader.close();
} catch (IOException e1) {
logger.error("读取文本文件异常",e1);
}
}
}
}

/**
*以行为单位读取文件,常用于读面向行的格式化文件
*@paramfileName:文件名
*/
publicstatic List<String> readFileByLines(StringfileName) {
List<String> list = new ArrayList<String>();
if(fileName!=null&&!"".equals(fileName)){
File file = new File(fileName);
BufferedReader reader = null;
try {
logger.debug("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
/*一次读入一行,直到读入null为文件结束*/
while ((tempString = reader.readLine()) !=null) {
logger.debug(tempString);
list.add(tempString);
}
} catch (IOException e) {
logger.error("读取文本文件异常",e);
} finally {
if (reader !=null) {
try {
reader.close();
} catch (IOException e1) {
logger.error("读取文本文件异常",e1);
}
}
}
}
return list;
}

5.2、常用写文件:

/** 
*把内容写到文件
*@paramfilePathName文件名
*@paramList<String>文件内容
*/
publicstaticboolean writerFile(String filePathName,String content){
boolean flag=false;
OutputStreamWriter osw=null;
try {
if(filePathName!=null&&!"".equals(filePathName)){
osw = new OutputStreamWriter(new FileOutputStream(filePathName));
}
} catch (FileNotFoundException e1) {
flag=false;
e1.printStackTrace();
}
if(osw!=null){
BufferedWriter bw=new BufferedWriter(osw);
try {
if(content!=null&&!"".equals(content)){
bw.write(content);
flag= true;
}
} catch (IOException e) {
flag=false;
e.printStackTrace();
}finally{
try {
bw.close();
osw.close();
} catch (IOException e) {
flag=false;
e.printStackTrace();
}
}
}
return flag;
}


/**
*把内容写到文件或追加到文件中
*@paramfilePathName文件名
*@paramList<String>文件内容
*/
publicstaticboolean writerFileIsAppend(String filePathName,String content){
boolean flag=false;
OutputStreamWriter osw=null;
try {
if (filePathName!=null&&!"".equals(filePathName)) {
osw = new OutputStreamWriter(new FileOutputStream(filePathName,true));
}
} catch (Exception e1) {
flag=false;
e1.printStackTrace();
}
if(osw!=null){
BufferedWriter bw=new BufferedWriter(osw);
try {
if(content!=null&&!"".equals(content)){
bw.write(content);
flag= true;
}
} catch (IOException e) {
flag=false;
e.printStackTrace();
}finally{
try {
bw.close();
osw.close();
} catch (IOException e) {
flag=false;
e.printStackTrace();
}
}
}
return flag;
}

六、  RandomAccessFile

6.1:说明

•   RandomAccessFile是一种特殊的文件流,可以用它在文件的任何地方查找或者插入数据

•   RandomAccessFile同时实现了DataInput和DataOutput接口,所以可以用它来读/写文件

•   构造器:

---RandomAccessFile(java.io.File f,String mode)

---RandomAccessFile(String file,String mode)

6.2:代码示例

/**
*Description: 读取文件最后一行内容
*@param fileName文件路径名+文件名
*/
publicstatic String getfinalLineData(StringpathName){
RandomAccessFileraf = null;
StringlastLine = "";
try {
raf = new RandomAccessFile(pathName,"r");
long len = raf.length();
if (len != 0L) {
long pos = len - 1;
while (pos > 0) {
pos--;
raf.seek(pos);
if (raf.readByte() =='\n') {
lastLine = raf.readLine();
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (raf !=null) {
try {
raf.close();
} catch (IOException e1) {
e1.getStackTrace();
}
}
}
return lastLine;
}

七、  注意事项

1、将高级流“套接“在低级流上,这样起到缓冲的作用可以提高效率。

2、将使用完的流关闭,释放资源。

3、读取如图片、声音、影像等文件用字节流。

4、读取如文本等文件用字符流。

5、根据具体的数据格式选择合适的读写方法、如按行读写、按照字节读写等。


•   按数据流动方向
•   缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓
•  缓冲输入流支持其父类的mark()和reset()方法: