IO流02--毕向东JAVA基础教程视频学习笔记

提要

08 自定义装饰类
09 LineNumberReader
10 MyLineNumberReader
11 字节流File读写操作
12 拷贝图片
13 字节流的缓冲区
14 自定义字节流的缓冲区-read和write的特点
15 读取键盘录入

08 自定义装饰类

 /*自定义装饰设计类*/
import java.io.*;
class MyBufferedReader2 extends Reader
{
private Reader r;
public MyBufferedReader2(Reader r)
{
this.r=r;
}
public String MyReadLine()throws IOException
{
int ch;
StringBuilder sb=new StringBuilder();
while((ch=r.read())!=-1)
{
if(ch=='\r')
continue;
if(ch=='\n')
return sb.toString();
else
sb.append((char)ch);
}
if(sb.length()!=0)
return sb.toString();
return null;
}
public void MyClose()throws IOException
{
r.close();
}
//重写父类的抽象方法
public void close()throws IOException
{
r.close();
}
public int read(char[] cbuf, int off, int len)throws IOException
{
return r.read(cbuf,off,len);
}
} public class MyBufferedReaderDemo2
{
public static void main(String[] args)throws IOException
{
FileReader fw=new FileReader("buf.txt");
MyBufferedReader2 mr=new MyBufferedReader2(fw);
String line;
while((line=mr.MyReadLine())!=null)
{
System.out.println(line);
}
mr.MyClose();
}
}

09 LineNumberReader

 /*带行号的缓冲区,lineNumberReader
是BufferedReader的直接子类,此类定义了setLineNumber(int)和getLineNumber)(),
分别用来设置和获取当前行号。
*/
import java.io.*;
public class LineNumberReaderDemo
{
public static void main(String[] args)
{
try
{
FileReader fw=new FileReader("PersonDemo.java");
LineNumberReader lr=new LineNumberReader(fw);
String line;
lr.setLineNumber(100);
while((line=lr.readLine())!=null)
{
System.out.println(lr.getLineNumber()+":"+line);
}
lr.close(); }
catch (IOException ie)
{
ie.printStackTrace();
} }
}

10 MyLineNumberReader

 /*
练习,自定义一个类,实现LineNumberReader的功能 */
import java.io.*;
class MyLineNumberReader extends MyBufferedReader2
{
private int lineNumber;
public MyLineNumberReader(Reader r)
{
super(r);
}
public String readLine()throws IOException
{
lineNumber++;
return super.MyReadLine();
}
//自定义设置行号的方法
public void setLineNumber(int lineNumber)
{
this.lineNumber=lineNumber;
}
//自定义获取行号的方法
public int getLineNumber()
{
return lineNumber;
} }
public class MyLineNumberReaderDemo
{
public static void main(String[] args)
{
try
{
FileReader fr=new FileReader("BufferedReaderDemo.java");
MyLineNumberReader mlnr=new MyLineNumberReader(fr);
String line=null;
mlnr.setLineNumber(99); while((line=mlnr.readLine())!=null)
{
System.out.println(mlnr.getLineNumber()+" "+line);
}
mlnr.close(); }
catch (IOException ie)
{
ie.printStackTrace();
} } }

11 字节流File读写操作

 /*
字符流:
FileReader
FileWriter BufferedReader
BufferedWriter 字节流:
InputStream 读
OutPutStream 写 需求:想要操作图片数据,这就需要用到字节流。 */
import java.io.*;
public class FileStream
{
public static void main(String[] args)throws IOException
{
writeFile();
readFile_1();
readFile_2();
readFile_3(); }
//把读到的存放到一个字节数组中,然后一起输出
public static void readFile_3()throws IOException
{
FileInputStream fis=new FileInputStream("fos.txt");
//int num=fis.available();
//使用avaliable方法,定义一个刚刚好的缓冲区,不用再循环了
//如果文件太大,不建议使用,会出现内存溢出
byte[] buf=new byte[fis.available()];
fis.read(buf);
System.out.println(new String(buf)); fis.close();
}
//把读到的存放到一个字节数组中,然后一起输出
public static void readFile_2()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_1()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 FileOutputStream("fos.txt");
fos.write("abcde".getBytes());
//对最小单位字节操作,不需要刷新
fos.close();
}
}

12 拷贝图片

void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。

int read(byte[] b) 从此输入流中,将最多b.length个字节的数据读入byte数组中。返回读入缓冲区的字节总数,如果达到文件末尾没有更多数据,则返回-1.
          

 /*
复制一个图片
思路:
1.用字节读取流对象和图片相关联
2.用字节写入流对象创建一个图片文件,用于存储获取到的图片数据
3.通过循环读写,完成数据的存储
4.关闭资源
*/
import java.io.*;
public class CopyPic
{
public static void main(String[] args)
{
FileOutputStream fos=null;
FileInputStream fis=null;
try
{
fis=new FileInputStream("d:\\十字路口.bmp");
fos=new FileOutputStream("d:\\路口.bmp"); 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("写入关闭失败!");
}
}
}
}

13 字节流的缓冲区

 /*
演示mp3的复制,通过缓冲区
BufferedOutputStream
BufferedInputStream
*/
import java.io.*;
public class CopyMp3
{
public static void main(String[] args)throws IOException
{
long start=System.currentTimeMillis();
copy();
long end=System.currentTimeMillis(); System.out.println((end-start)+"毫秒"); }
//通过字节流的缓冲区完成复制
public static void copy()throws IOException
{
BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("D:\\安又琪-那你呢.mp3"));
BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("d:\\那你呢.mp3")); int by=0;
while((by=bufis.read())!=-1)
{
bufos.write(by);
}
bufis.close();
bufos.close(); }
}

14 自定义字节流的缓冲区-read和write的特点

 /*
通过自定义的缓冲区,拷贝Mp3 注意,把myRead方法的返回值设为int型,而不是byte型
因为当连续读取8个1时,可能错误结束main函数中的读取循环,
提升了一个int型(byte型1个字节,int型32位,4个字节),还是-1的原因是因为在8个1前面补1造成的。
那么如果能在前面补0,既可以保留原字节数据不变,又可以避免-1的出现。 补0的方法:
和255相与 最后文件大小并没有变成原来的4倍的原因是,write方法只写入后8位,对int型又做了强制转换 运行发现,自定义的缓冲区的拷贝时间比原来的提高了大约200毫秒 */
import java.io.*;
class MyBufferedInputStream
{
private InputStream in;
private byte[] buf=new byte[1024];
private int pos=0,count=0;
public MyBufferedInputStream(InputStream in)
{
this.in=in;
}
//一次读一个字节,从缓冲区(字节数组)获取。
public int myRead()throws IOException
{
//通过in对象读取硬盘上数据,并存储在buf中。
if(count==0)
{
count=in.read(buf);
pos=0;
byte b=buf[pos]; count--;
pos++;
return b&255;
}
else if(count>0)
{
byte b=buf[pos];
count--;
pos++;
return b&255;
}
return -1; }
public void myClose()throws IOException
{
in.close();
}
}
public class CopyMp3_2
{
public static void main(String[] args)throws IOException
{
long start=System.currentTimeMillis();
copy_1();
long end=System.currentTimeMillis(); System.out.println((end-start)+"毫秒"); }
//通过字节流的缓冲区完成复制
public static void copy_1()throws IOException
{
MyBufferedInputStream bufis=new MyBufferedInputStream(new FileInputStream("D:\\安又琪-那你呢.mp3"));
BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("d:\\那你呢.mp3")); int by=0;
while((by=bufis.myRead())!=-1)
{ bufos.write(by);
}
bufis.myClose();
bufos.close(); }
}

15 读取键盘录入

需求:
通过键盘录入数据
当录入一行数据后,就将该行数据转变为大写形式再进行打印
如果录入的数据是over,则停止录入。

 /*
字符流:
FileReader
FileWriter BufferedReader
BufferedWriter 字节流:
FileInputStream
FileOutputStream BufferedInputStream
BufferedOutputStream 读取键盘录入:
System.out :对应的是标准输出设备,控制台
System.in :对应的标准输入设备:键盘 */
import java.io.*;
public class ReadIn
{
public static void main(String[] args) throws IOException
{
InputStream in=System.in;
//建立一个读入数据的缓冲区
StringBuilder sb=new StringBuilder(); while(true)
{
//read方法是阻塞式方法,没有录入,就会一直等待
int ch=in.read();
if(ch=='\r')
continue;
else if(ch=='\n')
{
String s=sb.toString();
if("over".equals(s))
break;
System.out.println(s.toUpperCase());
//清空缓冲区
sb.delete(0,sb.length()); }
else
sb.append((char)ch); } }
}
上一篇:MySQL多表查询,Navicat使用,pymysql模块,sql注入问题


下一篇:javascript匿名函数应用