本文實(shí)例講述了java輸入輸出流。分享給大家供大家參考,具體如下:
字節(jié)輸出流,輸出到文件中(寫)
outputstream抽象類
選好文件
子類實(shí)例化,需要拋異常
稍后傳輸?shù)臄?shù)據(jù)覆蓋原內(nèi)容
1
|
outputstream output = new fileoutputstream(file); |
稍后傳輸?shù)臄?shù)據(jù)追加在原數(shù)據(jù)之后
1
|
outputstream output = new fileoutputstream(file, true ); |
選好數(shù)據(jù),轉(zhuǎn)換為字節(jié)數(shù)組
1
2
|
string msg = "你好\r\n世界" ; byte data[] = msg.getbytes(); |
-
windows系統(tǒng)txt文件換行用
\r\n
輸出到文件(向文件中寫數(shù)據(jù))
1
|
output.write(data); |
寫入部分?jǐn)?shù)據(jù)
1
|
output.write(data, 3 , 8 ); |
- 從字節(jié)數(shù)組data的腳標(biāo)為3開始8個(gè)字節(jié)寫入此輸出流。
關(guān)閉輸出流
1
|
output.close(); |
字節(jié)輸入流,文件中數(shù)據(jù)讀取到程序中(讀)
inputstream抽象類
選好文件
1
|
file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "123.txt" ); |
子類實(shí)例化,需要拋異常
1
|
fileinputstream input = new fileinputstream(file); |
讀取數(shù)據(jù),并將數(shù)據(jù)保存到指定字節(jié)數(shù)組中
1
2
|
byte data[] = new byte [ 100 ]; int len = input.read(data); |
- 字節(jié)數(shù)組的長(zhǎng)度需要選的合適,如果從文件中讀取的內(nèi)容的的字節(jié)數(shù)超過(guò)了指定字節(jié)數(shù)組的長(zhǎng)度,那么超過(guò)的部分將無(wú)法保存到指定的字節(jié)數(shù)組中,這樣內(nèi)容就會(huì)丟失,如果遇到正文這樣一個(gè)字占多個(gè)字節(jié)的,也可能出現(xiàn)亂碼。
三種讀取數(shù)據(jù)方式
1
2
|
byte data[] = new byte [ 100 ]; int len = input.read(data); |
從此輸入流中將最多 data.length
個(gè)字節(jié)的數(shù)據(jù)讀入data
數(shù)組中。在某些輸入可用之前,此方法將阻塞。
返回:讀入緩沖區(qū)的字節(jié)總數(shù),如果因?yàn)橐呀?jīng)到達(dá)文件末尾而沒有更多的數(shù)據(jù),則返回 -1。
1
2
|
byte data[] = new byte [ 100 ]; int len = input.read(data, 5 , 14 ); |
從此輸入流中將最多14個(gè)字節(jié)的數(shù)據(jù)讀入data數(shù)組中,讀入位置為data數(shù)組后偏移5個(gè)位置,即讀入的開始位置是數(shù)組腳標(biāo)為5的位置
返回:讀入緩沖區(qū)的字節(jié)總數(shù),如果因?yàn)橐呀?jīng)到達(dá)文件末尾而沒有更多的數(shù)據(jù),則返回 -1。
1
2
3
4
5
6
7
|
byte data[] = new byte [ 100 ]; int i = 0 ; int read = input.read(); while (read != - 1 ){ data[i++] = ( byte )read; read = input.read(); } |
從此輸入流中讀取一個(gè)數(shù)據(jù)字節(jié)。如果沒有輸入可用,則此方法將阻塞。
返回:下一個(gè)數(shù)據(jù)字節(jié);如果已到達(dá)文件末尾,則返回 -1。
注意有參數(shù)的和無(wú)參數(shù)的read方法的返回值是不一樣的,有參數(shù)的返回的是讀取字節(jié)的個(gè)數(shù),無(wú)參的返回的是字節(jié)的int值,如果要保存到字節(jié)數(shù)組,還需要強(qiáng)轉(zhuǎn)為byte型。
將字節(jié)數(shù)組轉(zhuǎn)換為字符串輸出到控制臺(tái)
1
2
|
string x = new string(data); system.out.println(x); |
關(guān)閉輸入流
1
|
input.close(); |
字符輸出流:writer
1
2
3
4
5
6
7
8
9
10
11
|
import java.io.file; import java.io.filewriter; import java.io.writer; public class hello { public static void main(string[] args) throws exception{ file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "123.txt" ); writer writer = new filewriter(file); writer.write( "你好\r\nworld" ); writer.close(); } } |
write
方法的參數(shù)直接寫字符串即可。
必須要關(guān)閉字符輸出流,不然數(shù)據(jù)只是保存在緩沖區(qū)中,并沒有寫入文件。
字符輸入流:reader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.io.file; import java.io.filereader; import java.io.reader; public class hello { public static void main(string[] args) throws exception { file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "123.txt" ); char data[] = new char [ 100 ]; reader reader = new filereader(file); reader.read(data); string x = new string(data); system.out.println(x); reader.close(); } } |
read
方法是將讀取的數(shù)據(jù)保存到字符數(shù)組中。
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/shuair/article/details/81984299