字节流(具体讲了四个对象)
InputStream此抽象类是表示字节输入流的所有类的超类。
OutputStream此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。
用字节流尝试操作一下.字节…………
BufferedInputStream BufferedOutputStream
用缓冲区来实现以下拷贝mp3
CopyMP3
import java.io.*;
class myBufferedInputStream{
private InputStream in;
private byte[] buf=new byte[1024];
private int pos=0;count=0;
myBufferedInputStream
}
class copymp3{
public static void main(String[] args)throws IOException{
long start=System.currentTimeMillis();
copy();
long end=System.currentTimeMillis();
System.out.print(end-start);
}
public static void copy()throws IOException{
BufferedInputStream buis=new BufferedInputStream(new FileInputStream("D:\\Users\\Jack\\Music\\KuGou\\经典老歌\\吴奇隆 - 祝你一路顺风.mp3"));
BufferedOutputStream buos=new BufferedOutputStream(new FileOutputStream("祝你一路顺风.mp3"));
int len=0;
while((len=buis.read())!=-1){
buos.write(len);
}
if(buis!=null&&buos!=null)
buis.close();
buos.close();
}
}
自定义字节流缓冲区.
import java.io.*;
class myBufferedReader extends Reader{
private Reader r;
myBufferedReader(Reader r){
this.r=r;
}
public String readLine()throws IOException{
StringBuilder sb=new StringBuilder();
int len=0;
while((len=r.read())!=-1){
if(len=='\r')
continue;
if(len=='\n')
return sb.toString();
sb.append((char)len);
}
if(sb.length()!=0)
return sb.toString();
return null;
}
public int read(char[] ch,int off,int end)throws IOException{
return r.read(ch,off,end);
}
public void close()throws IOException{
r.close();
}
}
class myLineNumberReader extends myBufferedReader{
private int num;
myLineNumberReader(Reader r){
super(r);
}
public String readLine()throws IOException{
num++;
return super.readLine();
}
public int getLineNumber(){
return num;
}
public void setLineNumber(int num){
this.num=num;
}
}
class MyBufferedReaderDemo{
public static void main(String[] args)throws IOException{
myLineNumberReader mbr=new myLineNumberReader(new FileReader("StringTest.java"));
mbr.setLineNumber(500);
String line=null;
while((line=mbr.readLine())!=null){
System.out.println(mbr.getLineNumber()+"::"+line);
}
if(mbr!=null){
mbr.close();
}
}
}
import java.io.*;
class BufferedDemo
{
public static void main(String[] args)throws IOException{
/*到复制
//创建一个字符写入流对象和文件关联.............
FileWriter fw=new FileWriter("Demo.txt");
//创建一个缓冲区
BufferedWriter bufw=new BufferedWriter(fw);
for(int x=1;x<6;x++){
//现将数据写到缓冲区,最后通过刷新,再将数据刷到本地
bufw.write("dsfsdf"+x);
//记住,只要用到缓冲区,就要记得刷新..写一次刷一次,相当于自动保存.停电了也不怕
bufw.flush();
//newLine()换行,,自动识别系统该用那个换行符
bufw.newLine();
}
//其实关闭缓冲区,就是在关闭缓冲区中的流对象,所以,只关闭流对象所对的缓冲区即可
bufw.close();
//创建一个读取流对象和文件相关联..............
FileReader fr=new FileReader("Demo.txt");
//为了提高效率,加入缓冲技术,将字符读取流对象作为参数传递给缓冲对象的构造函数.
BufferedReader bure=new BufferedReader(fr);
//该缓冲区提供了,一次读一行的方法,方便对文本数据的获取.当返回null时,表示,
//读到了文件的末尾返回null
String line=null;
while((line=bure.readLine())!=null){
System.out.println(line);
}
*/
BufferedReader bure=null;
BufferedWriter buwr=null;
try{
bure=new BufferedReader(new FileReader("Demo.txt"));
buwr=new BufferedWriter(new FileWriter("Test.txt"));
String line=null;
while((line=bure.readLine())!=null){
buwr.write(line);
buwr.newLine();
buwr.flush();
}
}
catch(IOException e)
{
throw new RuntimeException("读写失败");
}
finally{
try{
if(bure!=null){
bure.close();
}
}
catch(IOException e){
throw new RuntimeException("读取关闭失败");
}
try{
if(buwr!=null)
buwr.close();
}
catch(IOException e){
throw new RuntimeException("写入关闭失败");
}
}
}
}