都是自己敲的。。。
inputStream常用方法:
int read()
int read(byte[] b)
int read(byte[] b,int off, int len)
void close()
outputStream的常用方法:
void write(int b)
void write(byte[] b)
void write(byte[],int off,int len)
void flush();
void close();
inputStream的之类:
ByteArrayInputStream,FileInputStream,FilterInputStream,PipedInputStream,SequenceInputStream,ObjectInputStream
outputStream的子类:
ByteArrayOutputStream,FileOutputStream....
字节流读取文件:
public class Example01{
public void static main(String[] args){
int i = 0;
FileInputStream inputStream = new FileInputStream("test.txt");
while(true){
i = inputStream.read();
if(i = -1){
break;
}
System.out.println(i);
}
}
字节流输出文件:
public class Example02{
public void static main(String[] args){
FileOutputStream outputStream = new FileOutputStream("example.txt);
String str = "黑马程序员";
byte[] bytes = str.getBytes();
for(int i = 0 ; i < bytes.length ; i ++){
outputStream.write(bytes[i]);
}
outputStream.close();
}
}
直接用write(byte b)要首先清空原来的文件
要想在源文件后面加,用write(byte b , boolean append)
同时使用FileInputStream和FileOutputStream实现文件的拷贝:
class Example04{
public void static main(String[] args){
FileInputStream in = new FileInputStream("D:\BaiduYunGuanjian");
FileOutputStream out = new FileOutputStream(E:\新建文件夹);
int len ;
long oldTime = System.currentTimeMillis();
while( (len = in.read() ) != -1 ){
out.write(len);
}
long newTime = System.currentTimeMillis();
long time = newTime - oldTime;
in.close();
out.close();
}
}
字节流的缓冲区
复习两个知识点:
int read(byte[] b):从输入流中读取若干字节,把它们保存在参数b指定的字节数组中,返回的整数表示读取数字节
void write(bytes[] b, int off, int len):将指定byte数组中从偏移量off开始的len个字节写入输入流
public class Example05 {
Public void static main(String[] args){
FileInputStream in = new FileInputStream("D:\BaiduYunGuanjia");
FileOutputStream out = new FileOutputStream("D:\新建文件夹")
;
int len ; //这个值是用来记录每次读取的最大字节数的
byte[] b = new byte[1024];
while((len = in.read(b)) != -1){
out.write(b,0,len);
}
in.close();
out.close();
}
}
装饰设计模式:
通过包装一个类,动态地为它增加功能的一种设计模式
关键在于:
包装类的构造方法中要接收一个被包装类的实例对象,然后再进行“包装”
字节缓冲流BufferedInputStream,BufferedOutputStream就是对inputstream和outputStream的包装
其优点在于,这两个类内部已经定义了一个大小为8192的字节数组,减少了代码量
上面的Example05.java就可以简写为:
class Example06{
public void static main(String[] args){
BuffedInputStream in = new BuffedInputStream(new FileInputStream("D:\BaiduYunGuanjia"));
BuffedOutputStream out = new BuffedOutputStream(new FileOutputStream("D:\新建文件夹");
int len = 0 ;
while((len = in.read())!= -1){
out.write(len);
}
in.close();
out.close();
}
}
字符流:
jdk中提供了字符流(InputStream和OutputStream是字节流)
Reader:BufferedReader,CharArrayReader,InputStreamReader(FileReader),PipedReader
Writer:BufferedWriter,CharArrayWriter,OutputStreamWriter(FileWriter),PrintWriter
reader和writer与inputstream,OutputStream其实差不多,都是利用write()和read()
它们同样有包装类,BufferedReader和BufferuedWriter
bufferedReader有一个重要的方法:readLine()
从此:
(ch = reader.read() )!= - 1
或者:(len = bufferedInputStream.read() ) != -1
或者:(len = inputStream.read(buff))!= -1
变成了
(str = br.readLine() ) != null
关于LineNumberReader:可以获取当前读取的行数
在文件加上行号:
public class Example11 {
public void static main(Sring[] args){
FileReader reader = new FileReader("Example04.java");
FileWriter writer = new FileWriter("copy.java");
LineNumberReader lbreader = new LineNumberReader(reader);
String str;
lbreader.setLineNumber(0);
while(( str = reader.readline() ) != null){
writer.write(lbreader.getLineNumber + ":" +str);
writer.write("\r\n");
}
}
} |
|