Java I/O流-总结(InputStream,OutputStream,Reader,Writer)
?Java流總結(jié)
一、 流的分類
? 按數(shù)據(jù)流動(dòng)方向
– 輸入流:只能從中讀取字節(jié)數(shù)據(jù),而不能向其寫出數(shù)據(jù)
– 輸出流:只能向其寫入字節(jié)數(shù)據(jù),而不能從中讀取數(shù)據(jù)
? 按照流所處理的數(shù)據(jù)類型
– 字節(jié)流:用于處理字節(jié)數(shù)據(jù)。
– 字符流:用于處理Unicode字符數(shù)據(jù)。
? 按照流所處理的源
– 節(jié)點(diǎn)流:從/向一個(gè)特定的IO設(shè)備讀/寫數(shù)據(jù)的流。(低級(jí)流)
– 處理流:對(duì)已存在的流進(jìn)行連接和封裝的流。(高級(jí)流)
二、 緩沖流
? 緩沖流要“套接”在相應(yīng)的節(jié)點(diǎn)流之上,對(duì)讀寫的數(shù)據(jù)提供了緩
沖的功能,提高了讀寫的效率,同時(shí)增加了一些新的方法。
? J2SDK提供了四種緩存流:
– BufferedReader
– BufferedWriter
– BufferedInputStream s
– BufferedOutputStream
?
?
? 緩沖輸入流支持其父類的mark()和reset()方法:
– mark()用于“標(biāo)記”當(dāng)前位置,就像加入了一個(gè)書簽,可以使用reset()方法返回這個(gè)標(biāo)記重新讀取數(shù)據(jù)。
? BufferedReader提供了readLine()方法用于讀取一行字符串(以\r或\n分隔)。
? BufferedWriter提供了newLine()用于寫入一個(gè)行分隔符。
? 對(duì)于輸出的緩沖流,寫出的數(shù)據(jù)會(huì)先在內(nèi)存中緩存,使用flush()方法將會(huì)使內(nèi)存中的數(shù)據(jù)立刻寫出。
三、 類層次
3.1、InputStream類層次
?
?
?
?
?
3.2、OutputStream類層次
?
3.3、Reader類層次
?
?
?
?
?
3.4、Writer類層次
?
四、 常用的字符流與字節(jié)流的轉(zhuǎn)化
?
說(shuō)明:
1. 字節(jié)流用于讀寫諸如圖像數(shù)據(jù)之類的原始字節(jié)流。
2. 字符流用于讀寫諸如文件數(shù)據(jù)之類的字符流。
3. 低級(jí)流能和外設(shè)交流。
4. 高級(jí)流能提高效率。
5. InputStreamReader 是字節(jié)流通向字符流的橋梁。
6. OutputStreamWriter是字符流通向字節(jié)流的橋梁。
?
?
?
五、 代碼實(shí)例
5.1、常用讀文件:
/**
*以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
*@paramfileName:文件的名
*/
publicstaticvoid readFileByBytes(String fileName) {
File file= new File(fileName);
InputStream in = null;
try {
logger.debug("以字節(jié)為單位讀取文件內(nèi)容,一次讀多個(gè)字節(jié):");
/*一次讀多個(gè)字節(jié)*/
byte[] tempbytes =newbyte[100];
int byteread = 0;
in = new FileInputStream(file);
/*讀入多個(gè)字節(jié)到字節(jié)數(shù)組中,byteread為一次讀入的字節(jié)數(shù)*/
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);
}
}
}
}
?
?
?
?
?
?
?
?
?
/**
*以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件
*@paramfileName:文件名
*/
public staticvoid readFileByChars(StringfileName) {
Reader reader = null;
try {
logger.debug("以字符為單位讀取文件內(nèi)容,一次讀多個(gè)字節(jié):");
/*一次讀多個(gè)字符*/
char[] tempchars =newchar[100];
int charread = 0;
if(fileName!=null&&!"".equals(fileName)){
reader = new InputStreamReader(new FileInputStream(fileName));
/*讀入多個(gè)字符到字符數(shù)組中,charread為一次讀取字符數(shù)*/
while ((charread = reader.read(tempchars)) != -1) {
/*對(duì)于windows下,rn這兩個(gè)字符在一起時(shí),表示一個(gè)換行。*/
/*但如果這兩個(gè)字符分開顯示時(shí),會(huì)換兩次行。*/
/*因此,屏蔽掉r,或者屏蔽n。否則,將會(huì)多出很多空行。*/
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("以行為單位讀取文件內(nèi)容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
/*一次讀入一行,直到讀入null為文件結(jié)束*/
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、常用寫文件:
/**
*把內(nèi)容寫到文件
*@paramfilePathName文件名
*@paramList<String>文件內(nèi)容
*/
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;
}
?
?
/**
*把內(nèi)容寫到文件或追加到文件中
*@paramfilePathName文件名
*@paramList<String>文件內(nèi)容
*/
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:說(shuō)明
? RandomAccessFile是一種特殊的文件流,可以用它在文件的任何地方查找或者插入數(shù)據(jù)
? RandomAccessFile同時(shí)實(shí)現(xiàn)了DataInput和DataOutput接口,所以可以用它來(lái)讀/寫文件
? 構(gòu)造器:
---RandomAccessFile(java.io.File f,String mode)
---RandomAccessFile(String file,String mode)
6.2:代碼示例
/**
*Description:讀取文件最后一行內(nèi)容
*@paramfileName文件路徑名+文件名
*/
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;
}
七、 注意事項(xiàng)
1、將高級(jí)流“套接“在低級(jí)流上,這樣起到緩沖的作用可以提高效率。
2、將使用完的流關(guān)閉,釋放資源。
3、讀取如圖片、聲音、影像等文件用字節(jié)流。
4、讀取如文本等文件用字符流。
5、根據(jù)具體的數(shù)據(jù)格式選擇合適的讀寫方法、如按行讀寫、按照字節(jié)讀寫等。
轉(zhuǎn)載于:https://www.cnblogs.com/wangyuyu/p/3167318.html
總結(jié)
以上是生活随笔為你收集整理的Java I/O流-总结(InputStream,OutputStream,Reader,Writer)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 找某個ColumnName在那些Tabl
- 下一篇: 【原】分享超实用工具给大家