Java基础---Java---IO流-----LineNumberReader方法及原理、自定义一个LineNumberReader、字节流、图片复制、mp3复制、

时间:2022-01-05 22:32:28

LineNumberReader

跟综行号的缓冲字符输入流,些类定义了setLineNumber(int)和getLineNumber(int),它们可分别用于设置和获取当前行号

import java.io.*;
class LineNumberReaderDemo{
public static void main(String []args) throws IOException{
FileReader fr=new FileReader("PersonDemo.java");
LineNumberReader lnr=new LineNumberReader(fr);
String line=null;
lnr.setLineNumber(100);//可以定义它从100开始
while((line=lnr.readLine())!=null{
System.out.println("lnr.getLineNumber()+":"+line");//调用LineNumberReader方法
}
lnr.close();//关闭流

}
}


自定义一个LineNumberReader并实现里面的方法:
import java.io.*;
class MyLineNumberReader{
private Reader r;
private int lineNumber;
MyLineNumberReader(Reader r){//将被包装的对象传递进来
this.r=r;
}
public String myReadLine() throws IOException{
lineNumber++;
StringBuilder sb=new StringBuilder();
int ch=0;
while((!ch=r.read()!=-1){
if(ch=='\r')
continue;
if(ch=='\n')
else //进行添加到缓冲区里
return sb.toString();
sb.append((char)ch);
}
if(!sb.length()!=0)
return sb.toString();
return null;
}
public void setLineNumber(int lineNumber){
this.lineNumber=lineNumber;
}
public void getLineNumber(){
return lineNumber;
}

public void myclose(){
r.close();
}
}
class MyLineNumberReaderDemo{
public static void main(String[] args) throws IOException{
FileReader fr=new FileReader("buf.java");
MyLineNumberReader mylnr=new MyLineNumberReader(fr);
String line=null;
while((line=mylnr.readLine())!=null){
System.out.println(mylnr.getLineNumber()+":"+line);
}
mylnr.myClose();
}
}


优化后的自定义类:注意区别
import java.io.*;
class MyLineNumberReader extends MyBufferedReader{

private int lineNumber;
MyLineNumberReader(Reader r){//将被包装的对象传递进来
super(r);
}
public String myReadLine() throws IOException{
lineNumber++;
return super.myReadLine(r);
}
public void setLineNumber(int lineNumber){
this.lineNumber=lineNumber;
}
public void getLineNumber(){
return lineNumber;
}


}

字符流:
FileReader
FileWriter
BufferedReader
BufferedWriter

字节流:
InputStream (读)
OutputStream (写)
字符流使用的是字节数组byte[],能操作很多媒体文件。
需求:想要操作图片数据。这时就要用到字节流。

import java.io.*;
class FileStream{
public void static void(String[] args){
writeFile();//结果是写入abcde到fos.txt
readFile();//结果是a,b,c,d,e(不是一行)
readFile1();//结果是abcde(是一行)
readFile2();//结果是5,返回字节流的个数。
}
public static void readFile2()throws IOException{
FileInputStream fis=new FileInputStream("fos.txt");
//int num=fis.available();
//System.out.println(num);
byte[] buf=new byte[fis.available()];//定义一个刚刚好的缓冲区,不用再循环了。
fis.read(buf);
System.out.println(new String(buf);
fis.close();
}

public static void readFile1()throws IOException{
FileInputStream fis=new FileInputStream("fos.txt");
byte[] buf=new byte[1024];//定义这个数组在操作媒体文件是有用的。
int len=0;
while((len=fis.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
fis.close();
}

public static void readFile()throws IOException{
FileInputStream fis=new FileInputStream("fos.txt");
int ch=0;
while((ch=fis.read())!=-1){
System.out.println((char)ch);
}
fis.close();
}

public static void WriteFile() throws IOException{
FileOutputStream fos=new FileOutStream("fos.txt");
String =get
fos.write("abcde".getBytes());
fos.close();//关闭流
}
}

字符流本质上走的是字节,但是它要把字节临时存起来。如一个中文有两个字节。
复制一个图片
1.用字节读取流对象和图片关联
2.用字节写入流对象创建一个图片文件,用于存储到的图片数据
3.通过循环读写,完成数据的存储
4.关闭资源
字符流也是可以复制图片的,但是复制后的图片可能看不了。因为读一段要去编码表里面查表,而字节码在读取的过程中可能会发生变化,所以可能看不了。字符流只用来处理文字数据。

import java.io.*;
class CopyPic{
public static void main(){
FileOutputStream fos=null;
FileIntputStream fis=null;
try{
fos=new FileOutputStream(c:\\2.jpg);
fis=new FileIutputStream(c:\\1.jpg);
byte [] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
}catch(IOException e){
throw new RuntimeException("复制文件失败");
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException e){
throw new RuntimeException("读取关闭失败");
}
try{
if(fos!=null)
fos.close();
}catch(IOException e){
throw new RuntimeException("写入关闭失败");
}
}
}
}

一个mp3文件的复制,通过缓冲区:
import java.io.*;
class CopyMp3{
public static void main(String[] args) throws IOException{
long start=System.out.currentTimeMillis();
copy();
long end=System.out.currentTimeMillis();
System.out.println((end-start)+"毫秒");//输出一共消耗了多长时间
}
public static void copy() throws IOException{
BufferedInputStream bufis=new BufferedInputStream(new FileInputStream(c:\\ylzs.mp3));
BufferedOnputStream bufis=new BufferedOnputStream(new FileOnputStream(D:\\ylzs.mp3));
//上面的缓冲区里面都是数组。
itn by=0;
while((by=bufis.read())!=-1){
bufos.writer(by);//写入
}
bufos.close();
bufis.close();

}
}




import java.io.*;
class MyBufferedInputStream{
private InputStream in;
private byte[] buf=new byte[1024*4];
private int pos=0,count=0;
MyBufferedInputStream (InputStream in){
this.in=in;
}
//一次读取一个字节,从缓冲区(字节数组)获取
public int myRead()throw IOException{
//通过in对象读取硬盘上的数据,并存储在buf中,
if(count==0){
count=in.read(buf);//记录个数
byte b=buf(pos);
pos=0;
count--;
pos++;
return b&0xff;
}else if(count>0){
byte b=buf(pos);
pos=0;
count--;
pos++;
return b&0xff;
}
return -1;
}
public void myClose(){
in.close();
}
}


mp3文件都是由二进制组成的。
一个f是4个1
byte:-1  ----> int :-1
11111111  ---->提升了一个int类型,
那么我只要在前面补0,即可以保留原字节数据不变,又可以避免-1的出现。相与,与后8进行相与。read方法在做提升,write在做强转
                                                   11111111
00000000 00000000 00000000 11111111
--------------------------------------------------------
00000000 00000000 00000000 11111111