一、缓冲流
1)缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。
2)Java提供了四种缓冲流,其常用的构造方法为:
3)缓冲输入流支持其父类的mark和reset方法。
4)BufferedReader提供了readLine方法用于读取一行字符串(以\r或\n分隔)。
5)BufferedWriter提供了newLine方法用于写入一个行分隔符。
6)对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush方法将会使内存中的数据立刻写出。
二、BufferedInputStream
举例:
三、BufferedWriter和BufferedReader
举例:
1)缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。
2)Java提供了四种缓冲流,其常用的构造方法为:
public BufferedReader(Reader in)
public BufferedReader(Reader in, int sz)
public BufferedWriter(Writer out)
public BufferedWriter(Writer out, int sz)
public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in, int size)
public BufferedOutputStream(OutputStream out)
public BufferedOutputStream(OutputStream out, int size)
3)缓冲输入流支持其父类的mark和reset方法。
4)BufferedReader提供了readLine方法用于读取一行字符串(以\r或\n分隔)。
5)BufferedWriter提供了newLine方法用于写入一个行分隔符。
6)对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush方法将会使内存中的数据立刻写出。
二、BufferedInputStream
举例:
package com.hpe.io;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TestBufferStream1 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("T:\\1. Java\\code\\JustAJoke\\src\\com\\hpe\\io\\TestBufferStream1.java");
BufferedInputStream bis = new BufferedInputStream(fis);
int c = 0;
System.out.println((char)bis.read());
System.out.println((char)bis.read());
bis.mark(100); //标记当前位置,限制读取100个字符
for(int i=0; i<=10 && (c = bis.read()) != -1; i++) {
System.out.print((char)c + " ");
}
System.out.println();
bis.reset();
for(int i=0; i<=10 && (c = bis.read()) != -1; i++) {
System.out.print((char)c + " ");
}
bis.close();
fis.close();
} catch (FileNotFoundException e) {
System.out.println("文件没有找到!");
} catch (IOException e) {
System.out.println("文件读取错误!");
}
}
}
运行结果:
p
a
c k a g e c o m . h
c k a g e c o m . h
三、BufferedWriter和BufferedReader
举例:
package com.hpe.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
public class TestBufferStream2 {
public static void main(String[] args) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("V:\\text.txt"));
BufferedReader br = new BufferedReader(new FileReader("V:\\text.txt"));
String str = null;
for(int i=0; i<=100; i++) {
str = String.valueOf(Math.random());
bw.write(str);
bw.newLine();
}
bw.flush();
while((str = br.readLine()) != null) {
System.out.println(str);
}
bw.close();
br.close();
} catch (IOException e) {
}
}
}
运行结果:
0.8050144191506169
0.5277244257877269
0.42990360311913867
0.43228114424610475
0.15688375502383267
0.4452742428231439
0.9307418412068111
0.7767640292567234
0.6277957882402495
0.5704895253880855
0.20265845436771845
0.1449739994790591
0.334276534667864
0.1465307383845288
0.3793753645433219
0.781958346461455
0.29285322582019957
0.3162976492608752
0.030539332574756717
0.9126878357350507
0.24071731659171003
0.37683779682435403
0.8482148631274146
0.4332676956482896
0.4005794210210031
0.30525587978518787
0.8824428422746716
0.8619457608799925
0.6047857492974168
0.7165543572044196
0.8742349236487098
0.9569306398443014
0.4681797921609655
0.7273131238631549
0.649413467837918
0.4871343395197858
0.10485664884253221
0.2938422960359963
0.9829952487275014
0.002305623553783498
0.22087228008743898
0.4694183179447622
0.821356583730791
0.09062142692800779
0.2831329146538395
0.68244116209821
0.25820647262256
0.5346559146793558
0.8641050630063746
0.5758256672025793
0.20043351682280997
0.9807474815347218
0.19264967009032852
0.167667908036071
0.23118113713540156
0.22213642209590556
0.40319861026358705
0.0942030201358316
0.7800451350158021
0.9628467599356726
0.9686557416076813
0.4782980571563461
0.3440762516937159
0.9771382210045655
0.3268182727490484
0.5301998710387201
0.16830987394996633
0.908454533885522
0.7657790858401662
0.3319844495415404
0.490833552397563
0.562147515988892
0.22068384973938882
0.38313095005138753
0.18494281762206755
0.4378564724640629
0.752381809065642
0.39186983453683044
0.3897238616592895
0.4202747621417374
0.12792196477147222
0.48543178146972576
0.5104418266274302
0.0011368131668060277
0.10351169384336856
0.482049535686603
0.7914015067585797
0.14061050282162746
0.72246799583603
0.11854262451783903
0.5164869945847523
0.8928809957223017
0.18385600395019663
0.15954047752718448
0.7763754895162401
0.2185642479041635
0.611923459539444
0.1278766184658151
0.6564598660396653
0.9856895835532645
0.3621039385124495