Java I/O流-总结(InputStream,OutputStream,Reader,Writer)
?Java流總結
一、 流的分類
? 按數據流動方向
– 輸入流:只能從中讀取字節數據,而不能向其寫出數據
– 輸出流:只能向其寫入字節數據,而不能從中讀取數據
? 按照流所處理的數據類型
– 字節流:用于處理字節數據。
– 字符流:用于處理Unicode字符數據。
? 按照流所處理的源
– 節點流:從/向一個特定的IO設備讀/寫數據的流。(低級流)
– 處理流:對已存在的流進行連接和封裝的流。(高級流)
二、 緩沖流
? 緩沖流要“套接”在相應的節點流之上,對讀寫的數據提供了緩
沖的功能,提高了讀寫的效率,同時增加了一些新的方法。
? J2SDK提供了四種緩存流:
– BufferedReader
– BufferedWriter
– BufferedInputStream s
– BufferedOutputStream
?
?
? 緩沖輸入流支持其父類的mark()和reset()方法:
– mark()用于“標記”當前位置,就像加入了一個書簽,可以使用reset()方法返回這個標記重新讀取數據。
? BufferedReader提供了readLine()方法用于讀取一行字符串(以\r或\n分隔)。
? BufferedWriter提供了newLine()用于寫入一個行分隔符。
? 對于輸出的緩沖流,寫出的數據會先在內存中緩存,使用flush()方法將會使內存中的數據立刻寫出。
三、 類層次
3.1、InputStream類層次
?
?
?
?
?
3.2、OutputStream類層次
?
3.3、Reader類層次
?
?
?
?
?
3.4、Writer類層次
?
四、 常用的字符流與字節流的轉化
?
說明:
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:讀取文件最后一行內容
*@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;
}
七、 注意事項
1、將高級流“套接“在低級流上,這樣起到緩沖的作用可以提高效率。
2、將使用完的流關閉,釋放資源。
3、讀取如圖片、聲音、影像等文件用字節流。
4、讀取如文本等文件用字符流。
5、根據具體的數據格式選擇合適的讀寫方法、如按行讀寫、按照字節讀寫等。
轉載于:https://www.cnblogs.com/wangyuyu/p/3167318.html
總結
以上是生活随笔為你收集整理的Java I/O流-总结(InputStream,OutputStream,Reader,Writer)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 找某個ColumnName在那些Tabl
- 下一篇: 【原】分享超实用工具给大家