标签:des c style class blog code
转自:http://blog.csdn.net/johnny901114/article/details/8710403
其实BufferReader也是差不多的,这里就不多讲
1
2
3
4
5
6
7 |
FileReader fr = new
FileReader( "test.txt" ); BufferedReader br = new
BufferedReader(fr); String line = null ; //注意readLine方法读取的内容不包括换行符 while ((line=br.readLine())!= null ){ System.out.println(line); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
package
i18n; public class test { } class Person { public
void eat() { System.out.println( "吃饭" ); } } public
class PersonEnhance { private
Person p; // 把需要增强 的类传进去初始化 public
PersonEnhance(Person p) { this .p = p; } public
void enhanceEat() { System.out.println( "开胃酒" ); p.eat(); System.out.println( "甜点" ); } } |
setLineNumber(int)
andgetLineNumber()
方法分别设置获取行号
1
2
3
4
5
6
7
8 |
public static void main(String[] args) throws
IOException { FileReader fr = new
FileReader( "test.txt" ); LineNumberReader lnr = new
LineNumberReader(fr); String num = null ; while
((num = lnr.readLine()) != null ) { System.out.println(lnr.getLineNumber() + ":"
+ num); }<br>} |
控制台输出:
1
2
3
4
5
6
7
8
9
10
11 |
public static void main(String[] args) throws
IOException { FileReader fr = new
FileReader( "test.txt" ); LineNumberReader lnr = new
LineNumberReader(fr); String num = null ; // 设置行号的开始值为100 lnr.setLineNumber( 100 ); while
((num = lnr.readLine()) != null ) { System.out.println(lnr.getLineNumber() + ":"
+ num); } } |
输出结果为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
public String readLine() throws
IOException { int
lineNumber= 0 ; lineNumber++; StringBuilder buffer = new
StringBuilder(); int
i = 0 ; while
((i =reader.read()) != - 1 ) { if
(( char ) i == ‘\r‘ ) { continue ; } if
(( char ) i == ‘\n‘ ) { return
buffer.toString(); } else
{ buffer.append(( char ) i); } } if
(buffer.length() != 0 ) { return
buffer.toString(); } return
null ; } |
1
2
3
4 |
public static void writeData() throws
Exception{ OutputStream out = new
FileOutputStream( "D:\\test2.txt" ); out.write( "hello inputStream!" .getBytes()); } |
1
2
3
4
5
6
7 |
public static void readData() throws
Exception { InputStream is = new
FileInputStream( "D:\\test2.txt" ); int
num = 0 ; while
((num = is.read()) != - 1 ) { System.out.println(( char ) num); } } |
但是这样效率比较低,因为读取一次写一次,我们可以使用缓冲:
1
2
3
4
5
6
7
8
9 |
public static void readData2() throws
Exception { InputStream is = new
FileInputStream( "D:\\test2.txt" ); int
num = 0 ; byte [] buffer =newbyte[ 1024 ]; //把读取到的数据放进字节数组里面 while
((num = is.read(buffer)) != - 1 ) { System.out.println( new
String(buffer, 0 , num)); } } |
在InputStream类中有这样一个方法available()返回int 他的作用是返回文件内容的长度 那么我们就可以这样读取数据,而不用while循环了
public static void readData3()throws Exception { InputStream is =new FileInputStream("D:\\test2.txt"); //返回文件的长度 int num = is.available(); 把字节数组的长度定义成文件长度,那么这个数组就刚好装下这个文件了 byte[] buffer =newbyte[num]; is.read(buffer); System.out.println(new String(buffer)); }
1
2
3
4
5
6
7
8
9 |
InputStream is = new
FileInputStream( "D:\\imagetest\\desk.jpg" ); OutputStream os = new
FileOutputStream( "E:\\desk1.jpg" ); byte [] buffer = new
byte [ 1024 ]; int readNum = 0 ; int a= 0 ; while ((readNum=is.read(buffer))!=- 1 ){ System.out.println(a++); os.write(buffer, 0 , readNum); } |
使用java缓冲输出流
1
2
3
4
5
6
7
8 |
BufferedOutputStream buffOs = new
BufferedOutputStream( new
FileOutputStream( "F:\\KuGou\\baby2 - baby one more time.mp3" )); BufferedInputStream buffIs = new
BufferedInputStream( new
FileInputStream( "F:\\KuGou\\baby - baby one more time.mp3" )); int len = 0 ; while ((len=buffIs.read())!=- 1 ){ buffOs.write(len); } buffOs.close(); buffIs.close(); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
public static void main(String[] args) throws
IOException { InputStream is = System.in; StringBuilder buffer = new
StringBuilder(); int
i = 0 ; while
( true ) { i = is.read(); if
( ‘\r‘ == i) continue ; if
( ‘\n‘ == i) { String value = buffer.toString(); // 如果录入的是over那么则退出 if
( "over" .equals(buffer.toString())) break ; System.out.println(value); // 清空缓冲区 以免下次录入时不会和前面录入的汇合 buffer.delete( 0 , buffer.length()); } else
{ buffer.append(( char ) i); } } } |
1
2
3
4
5
6
7
8
9
10
11
12 |
public static void main(String[] args) throws
IOException { InputStream is = System.in; InputStreamReader isr = new
InputStreamReader(is); BufferedReader br = new
BufferedReader(isr); String line = null ; while
((line = br.readLine()) != null ) { if
(line.equals( "over" )) { break ; } System.out.println(line.toUpperCase()); } } |
对应的OutputStreamWriter是字符流向字节流转换的桥梁 也就是读进来的是字符,写进去的是字节,在上面的基础上我们可以这样改写:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
public static void main(String[] args) throws
IOException { InputStream is = System.in; InputStreamReader isr = new
InputStreamReader(is); BufferedReader br = new
BufferedReader(isr); OutputStreamWriter osw = new
OutputStreamWriter(System.out); BufferedWriter bw = new
BufferedWriter(osw); String line = null ; while
((line = br.readLine()) != null ) { if
(line.equals( "over" )) { break ; } bw.write(line); // 注意使用字符流要注意flush bw.flush(); // System.out.println(line.toUpperCase()); } } |
Java I/O流操作(二)---缓冲流[转],布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/laj12347/p/3777549.html