首頁 > 農業
JAVA 中的 IO流、位元組流、字元流、緩衝流、轉換流、序列流、列印流
由 紙鶴視界 發表于 農業2021-07-01
簡介使用步驟:建立OutputStreamWriter物件,構造方法中傳入位元組輸出流和指定的編碼使用OutputStreamWriter物件中的write方法,把字元轉換成位元組儲存到緩衝區中使用OutputStreamWriter中的flu
列印輸出語句是什麼
JAVA 中的 IO流、位元組流、字元流、緩衝流、轉換流、序列流、列印流
IO 概述
一切檔案資料(文字、圖片、影片等)在儲存時,都是以二進位制的形式儲存,傳輸時同樣如此。所以位元組流可以傳輸任意資料。 在操作流的時候,無論使用什麼樣的流物件,底層傳輸的始終為二進位制資料。
流的分類
根據資料的流向分為:
輸入流
和
輸出流
輸入流 :硬碟 ——> 記憶體
輸出流 :記憶體 ——> 硬碟
根據資料的型別分為:
位元組流
和
字元流
。
另外還有:
緩衝流
、
轉換流
、
序列流
、
列印流
。
第一章:位元組流
1。1 位元組輸出流(OutputStream)
簡述:java。io。OutputStream 抽象類是表示位元組輸出流的所有類的父類(超類),將指定的位元組資訊寫到目的地。
位元組輸出流的基本共性功能方法:
public
void
close
(
)
// 關閉此輸出流並釋放與此流相關聯的任何系統資源。
public
void
flush
(
)
// 重新整理此輸出流並強制任何緩衝的輸出位元組被寫出。
public
void
write
(
byte
[
]
b
)
// 將b。length位元組從指定的位元組陣列寫入此輸出流。
public
void
write
(
byte
[
]
b
,
int
off
,
int
len
)
// 從指定的位元組陣列寫入 len位元組,從偏移量 off開始輸 出到此輸出流。
public
abstract
void
write
(
int
b
)
// 將指定的位元組輸出流。
FileOutputStream
簡述: java。io。FileOutputStream 類是檔案輸出流,用於將資料寫出到檔案。
public
FileOutputStream
(
File file
)
// 建立檔案輸出流以寫入由指定的 File物件表示的檔案。
public
FileOutputStream
(
String name
)
// 建立檔案輸出流以指定的名稱寫入檔案
使用步驟:
建立FileOutputStream物件,構造方法中傳遞檔案路徑
呼叫FileOutputStream物件中的方法write,把資料寫入到檔案中
釋放資源(關閉檔案)
練習:
public
static
void
main
(
String
[
]
args
)
throws
IOException
{
FileOutputStream fos1
=
new
FileOutputStream
(
“E:\\test\\abc。txt”
)
;
FileOutputStream fos2
=
new
FileOutputStream
(
new
File
(
“E:\\test\\abc。txt”
)
)
;
// 寫入單個位元組
fos1
。
write
(
97
)
;
// 寫入位元組陣列
byte
[
]
bytesArray
=
“你好”
。
getBytes
(
)
;
System
。
out
。
println
(
Arrays
。
toString
(
bytesArray
)
)
;
// [-28, -67, -96, -27, -91, -67]
fos2
。
write
(
bytesArray
)
;
// 釋放資源
fos1
。
close
(
)
;
fos2
。
close
(
)
;
}
1。2 位元組輸入流(InputStream)
簡述:java。io。InputStream 抽象類是表示位元組輸入流的所有類的父類(超類),可以讀取位元組資訊到記憶體中。
位元組輸出流的基本共性功能方法:
public
void
close
(
)
// 關閉此輸入流並釋放與此流相關聯的任何系統資源。
public
abstract
int
read
(
)
// 從輸入流讀取資料的下一個位元組。
public
int
read
(
byte
[
]
b
)
// 從輸入流中讀取一些位元組數,並將它們儲存到位元組陣列 b中
FileIntputStream
簡述:java。io。FileInputStream類是檔案輸入流,從檔案中讀取位元組。
使用步驟:
建立FileInputStream物件,構造方法中傳入要讀取的資料來源檔案
使用FileInputStream物件中的方法read,讀取檔案
釋放資源
練習:
# 檔案的複製FileInputStream fis
=
new
FileInputStream
(
“E:\\test\\a\\1。jpg”
)
;
FileOutputStream fos
=
new
FileOutputStream
(
“E:\\test\\b\\1。jpg”
)
;
int
len
=
0
;
byte
[
]
bytes
=
new
byte
[
1024
]
;
while
(
(
len
=
fis
。
read
(
bytes
)
)
!=
-
1
)
{
fos
。
write
(
bytes
,
0
,
len
)
;
}
// 釋放資源
fos
。
close
(
)
;
fis
。
close
(
)
;
第二章:字元流
簡述:當使用位元組流讀取文字檔案時,可能會有一個小問題。就是遇到中文字元時,可能不會顯示完整的字元,因為一箇中文字元佔用多個位元組儲存(GBK佔用兩個位元組,UTF-8佔用三個位元組)。所以JAVA提供了字元流類,以字元為單位讀寫資料,專門用於處理文字檔案。
1。1 字元輸入流(Reader)
簡述:java。io。Reader抽象類是字元流最頂層的父類(超類),可以讀取字元資訊到記憶體中。
public
void
close
(
)
// 關閉此流並釋放與此流相關聯的任何系統資源。
public
int
read
(
)
//從輸入流讀取一個字元。
public
int
read
(
char
[
]
cbuf
)
// 從輸入流中讀取一些字元,並將它們儲存到字元陣列 cbuf中 。
FileReader 構造方法
FileReader(File file)
FileReader(String fileName)
練習
FileReader fr1
=
new
FileReader
(
“E:\\test\\a。txt”
)
;
FileReader fr2
=
new
FileReader
(
“E:\\test\\b。txt”
)
;
// 讀取一個字元
int
len
=
0
;
while
(
(
len
=
fr1
。
read
(
)
)
!=
-
1
)
{
System
。
out
。
println
(
len
)
;
}
// 讀取多個字元
int
len
=
0
;
char
[
]
cs
=
new
char
[
1024
]
;
while
(
(
len
=
fr1
。
read
(
cs
)
)
!=
-
1
)
{
System
。
out
。
println
(
“長度”
+
len
)
;
System
。
out
。
println
(
new
String
(
cs
,
0
,
len
)
)
;
}
// 釋放資源
fr1
。
close
(
)
;
fr2
。
close
(
)
;
1。2 字元輸出流(Writer)
簡述:java。io。Writer 字元輸出流,是所有字元輸出流的父類,是一個抽象類。
FileWriter 構造方法
FileWriter(File file)
FileWriter(String fileName)
使用步驟
建立FileWriter物件,構造方法中傳入要寫入資料的目的地
使用FileWriter中的方法write,把資料寫入到記憶體緩衝區中(字元轉換成位元組的過程)
使用FileWriter中的flush,把緩衝區中的資料寫入到硬碟
釋放資源(會先把記憶體緩衝區中的資料重新整理到硬碟中)
練習
FileWriter fw1
=
new
FileWriter
(
“E:\\test\\c。txt”
)
;
FileWriter fw2
=
new
FileWriter
(
“E:\\test\\c。txt”
,
false
)
;
// 寫入資料
fw1
。
write
(
97
)
;
char
[
]
cs
=
{
‘a’
,
‘b’
,
‘c’
,
‘d’
,
‘e’
}
;
fw1
。
write
(
cs
)
;
fw1
。
write
(
cs
,
1
,
3
)
;
// 換行: windows:\r\n Linux:/n Unix: /r
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
fw1
。
write
(
“HelloWorld”
+
i
+
“\r\n”
)
;
// 換行寫入
}
// 將緩衝區的資料重新整理到硬碟
fw1
。
flush
(
)
;
// 釋放資源
fw1
。
close
(
)
;
第三章:緩衝流
簡述: 功能更強大的流,能夠高效讀寫的緩衝流,能夠轉換編碼的轉換流,能夠持久化儲存物件的序列化流等等。這些都是在基本的流物件基礎之上建立而來的,是對基本流物件的一種增強
1。1 位元組緩衝流
繼承自父類的共性成員方法:
public
void
close
(
)
public
void
flush
(
)
public
void
write
(
byte
[
]
b
)
public
void
write
(
byte
[
]
b
,
int
off
,
int
len
)
public
abstract
void
write
(
int
b
)
1。11 BufferedOutputStream
構造方法:
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out, int size)
使用步驟:
建立FileOutputStream物件,構造方法中繫結要輸出的目的地
建立BufferedOutputStream物件,構造方法中傳遞FileOutputStream物件,提高FileOutputStream物件效率
使用BufferedOutputStream物件中的write方法,把資料寫入到內部緩衝區中
使用BufferedOutputStream物件中的flush方法,把內部緩衝區中的資料重新整理到檔案中
釋放資源(會先呼叫flush方法重新整理資料)
練習
FileOutputStream fos
=
new
FileOutputStream
(
“E:\\test\\c。txt”
)
;
BufferedOutputStream bos
=
new
BufferedOutputStream
(
fos
)
;
bos
。
write
(
“把資料寫到緩衝區中”
。
getBytes
(
)
)
;
bos
。
flush
(
)
;
bos
。
close
(
)
;
1。12 BufferedIntputStream
構造方法:
BufferedIntputStream(OutputStream out)
BufferedIntputStream(OutputStream out, int size)
使用步驟:
建立FIleInputStream物件,構造方法中傳入讀取的資料來源
建立BufferedInputStream物件,構造方法中傳入FIleInputStream
使用BufferedInputStream中的read方法,讀取檔案
釋放資源
練習
FileInputStream fis
=
new
FileInputStream
(
“E:\\test\\c。txt”
)
;
BufferedInputStream bis
=
new
BufferedInputStream
(
fis
)
;
byte
[
]
cs
=
new
byte
[
1024
]
;
int
len
=
0
;
while
(
(
len
=
bis
。
read
(
cs
)
)
!=
-
1
)
{
System
。
out
。
println
(
new
String
(
cs
,
0
,
len
)
)
;
}
bis
。
close
(
)
;
1。2 字元緩衝流
1。21 BufferedWriter
構造方法:
BufferedWriter(Writer out)
BufferedWriter(Writer out, int sz)
特有的成員方法:
void newLine() 寫入一個行分隔符。會根據不同的作業系統,獲取不同的行分隔符
使用步驟:
建立BufferedWriter物件,構造方法中傳入寫入的資料來源
呼叫BufferedWriter中的write方法,把資料寫入到記憶體緩衝區中
使用BufferedWriter中的flush方法,把記憶體緩衝區中的資料,重新整理到檔案中
釋放資源
練習
BufferedWriter bw
=
new
BufferedWriter
(
new
FileWriter
(
“E:\\test\\b。txt”
)
)
;
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
bw
。
write
(
“啦啦啦”
)
;
bw
。
newLine
(
)
;
}
bw
。
flush
(
)
;
bw
。
close
(
)
;
1。22 BufferedReader
構造方法:
BufferReader(Reader in)
BufferReader(Reader in, int sz)
特有的成員方法:
String readline() 讀取一行資料
返回值:包含該行內容的字串,不包含任何行終止符,如果已到達流末尾,則返回null
使用步驟:
建立緩衝字元輸入流物件,構造方法中傳入字元輸入流
使用緩衝字元輸入流物件中的read/readline讀取文字
釋放資源
練習
BufferedReader br
=
new
BufferedReader
(
new
FileReader
(
“E:\\test\\b。txt”
)
)
;
String line
;
// 建立讀取的文字行,結尾返回null
while
(
(
line
=
br
。
readLine
(
)
)
!=
null
)
{
System
。
out
。
println
(
line
)
;
}
br
。
close
(
)
;
位元組流 VS 緩衝流
public
static
void
main
(
String
[
]
args
)
throws
IOException
{
long
currentTimeMillis
=
System
。
currentTimeMillis
(
)
;
// FileOutputStream
FileInputStream fis1
=
new
FileInputStream
(
“E:\\test\\a\\a。jpg”
)
;
FileOutputStream fos1
=
new
FileOutputStream
(
“E:\\test\\b\\a。jpg”
)
;
byte
[
]
bytes
=
new
byte
[
1024
]
;
int
len
=
0
;
// 讀取到的有效位元組個數
while
(
(
len
=
fis1
。
read
(
bytes
)
)
!=
-
1
)
{
fos1
。
write
(
bytes
,
0
,
len
)
;
}
fos1
。
close
(
)
;
fis1
。
close
(
)
;
long
doneTimeMillis
=
System
。
currentTimeMillis
(
)
;
System
。
out
。
println
(
“File 複製檔案使用了:”
+
(
doneTimeMillis
-
currentTimeMillis
)
+
“毫秒”
)
;
// BufferedOutputStream
long
currentTimeMillis2
=
System
。
currentTimeMillis
(
)
;
BufferedInputStream bis1
=
new
BufferedInputStream
(
new
FileInputStream
(
“E:\\test\\a\\b。jpg”
)
)
;
BufferedOutputStream bos1
=
new
BufferedOutputStream
(
new
FileOutputStream
(
“E:\\test\\b\\b。jpg”
)
)
;
byte
[
]
bytes2
=
new
byte
[
1024
]
;
int
len1
=
0
;
while
(
(
len1
=
bis1
。
read
(
bytes2
)
)
!=
-
1
)
{
bos1
。
write
(
bytes2
,
0
,
len1
)
;
}
bos1
。
close
(
)
;
bis1
。
close
(
)
;
long
doneTimeMillis2
=
System
。
currentTimeMillis
(
)
;
System
。
out
。
println
(
“Buffered 複製檔案使用了:”
+
(
doneTimeMillis2
-
currentTimeMillis2
)
+
“毫秒”
)
;
}
# 輸出File 複製檔案使用了:
38
毫秒Buffered 複製檔案使用了:
8
毫秒
第四章:轉換流
簡述:在檔案的讀取和寫入中如果編碼不一致有可能導致亂碼的問題。轉換流中可以指定編碼。
1。1 OutputStreamWriter
構造方法:
OutputStreamWriter(OutputStream out) 建立使用預設字元編碼(UTF-8 )的OutputStreamWriter物件
OutputStreamWriter(OutputStream out, String charsetName) 建立使用指定字符集的OutputStreamWriter物件。
使用步驟:
建立OutputStreamWriter物件,構造方法中傳入位元組輸出流和指定的編碼
使用OutputStreamWriter物件中的write方法,把字元轉換成位元組儲存到緩衝區中
使用OutputStreamWriter中的flush方法,把緩衝區中的位元組重新整理到檔案中
釋放資源
練習
OutputStreamWriter osw
=
new
OutputStreamWriter
(
new
FileOutputStream
(
“E:\\test\\utf-8。txt”
)
,
“UTF-8”
)
;
osw
。
write
(
“你好”
)
;
osw
。
flush
(
)
;
osw
。
close
(
)
;
1。2 InputStreamReader
是位元組流通向字元流的橋樑,它使用指定的charset讀取位元組將其解碼為字元
構造方法:
InputStreamReader(InputStream in)
InputStreamReader(InputStream in, String charsetName)
使用步驟:
建立InputStreamReader物件
呼叫read方法
釋放資源
練習
InputStreamReader isr
=
new
InputStreamReader
(
new
FileInputStream
(
“E:\\test\\utf-8。txt”
)
,
“UTF-8”
)
;
String line
;
int
len
=
0
;
while
(
(
len
=
isr
。
read
(
)
)
!=
-
1
)
{
System
。
out
。
(
(
char
)
len
)
;
}
isr
。
close
(
)
;
PS:編碼換成“GBK”則會亂碼
第五章:序列流
1。1 ObjectOutputStream
把物件以流的方式寫入到檔案中
構造方法:
ObjectOutputStream(OutputStream out)
特有的成員方法:
void writeObject(Object obj) 將指定物件寫入ObjectOutputStream
使用步驟:
建立ObjectOutputStream物件,傳入位元組輸出流
使用ObjectOutputStream中的writeObject方法,把物件寫入檔案中
釋放資源
# Person類
public
class
Person
implements
Serializable
{
private
String name
;
private
int
age
;
public
Person
(
)
{
super
(
)
;
}
public
Person
(
String name
,
int
age
)
{
super
(
)
;
this
。
name
=
name
;
this
。
age
=
age
;
}
public
String
getName
(
)
{
return
name
;
}
public
void
setName
(
String name
)
{
this
。
name
=
name
;
}
public
int
getAge
(
)
{
return
age
;
}
public
void
setAge
(
int
age
)
{
this
。
age
=
age
;
}
@Override
public
String
toString
(
)
{
return
“Person [name=”
+
name
+
“, age=”
+
age
+
“]”
;
}
}
# main
public
static
void
main
(
String
[
]
args
)
throws
IOException
{
ObjectOutputStream oos
=
new
ObjectOutputStream
(
new
FileOutputStream
(
“E:\\test\\obj。txt”
)
)
;
oos
。
writeObject
(
new
Person
(
“趙麗穎”
,
18
)
)
;
oos
。
close
(
)
;
}
注意:Person類需要
implements
Serializable
類才能序列和反序列化
1。2 ObjectInputStream
簡述:把檔案中儲存的物件以流的方式讀取出來
構造方法:
ObjectInputStream(InputStream in)
特有的成員方法:
Object readObject()
使用步驟:
建立ObjectInputStream物件,傳入位元組輸入流
使用ObjectInputStream中的readObject方法讀取儲存物件的檔案
釋放資源
使用讀取出來的物件
練習
// 讀取上個練習存入的物件(重寫了toString方法)
ObjectInputStream ois
=
new
ObjectInputStream
(
new
FileInputStream
(
“E:\\test\\obj。txt”
)
)
;
Object readObject
=
ois
。
readObject
(
)
;
ois
。
close
(
)
;
System
。
out
。
println
(
readObject
)
;
// 將讀取出來的物件強轉成Person型別,然後進行使用
Person p
=
(
Person
)
readObject
;
System
。
out
。
println
(
p
。
getName
(
)
+
“, ”
+
p
。
getAge
(
)
)
;
# 輸出Person
[
name
=
趙麗穎
,
age
=
18
]
趙麗穎
,
18
第六章:列印流
簡述:平時我們呼叫 print 和 println 方法完成的,這兩個方法都來自於 java。io。PrintStream 該類能夠方便地列印各種資料型別的值,是一種便捷的輸出方式。
1。1 PrintStream
構造方法:
PrintStream
(
File file
)
// 輸出的目的地是一個檔案
PrintStream
(
OutputStream out
)
// 輸出的目的地是一個位元組輸出流
PrintStream
(
String fileName
)
// 輸出的目的地是一個檔案路徑
練習
public
static
void
main
(
String
[
]
args
)
throws
FileNotFoundException
{
PrintStream ps
=
new
PrintStream
(
“E:\\test\\p。txt”
)
;
ps
。
write
(
97
)
;
// 使用繼承的write方法寫入,結果:a
ps
。
println
(
97
)
;
// 97
ps
。
println
(
“HelloWorld”
)
;
ps
。
println
(
8。8
)
;
ps
。
println
(
true
)
;
// 釋放資源
ps
。
close
(
)
;
}
改變列印流向:
PrintStream p2
=
new
PrintStream
(
“E:\\test\\p。txt”
)
;
System
。
out
。
println
(
“在控制檯輸出”
)
;
System
。
setOut
(
p2
)
;
// 把輸出語句的目的地改變為列印流的目的地
System
。
out
。
println
(
“在列印流的目的地輸出”
)
;
// 釋放資源
p2
。
close
(
)
;
總結
,https://blog。csdn。net/weixin_40025107/article/details/116097411