提要

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); } }
}

IO流02--毕向东JAVA基础教程视频学习笔记的更多相关文章

  1. 多线程--毕向东java基础教程视频学习笔记

    目录 1.多线程运行的安全问题 2.多线程同步代码块 3.同步方法的锁是this 4.静态同步方法的锁是Class对象 5.单例设计模式(面试中的考点) 6.死锁(一个发生死锁的例子) 多线程运行的安 ...

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

    提要 01 IO流(BufferedWriter)02 IO流(BufferedReader)03 IO流(通过缓冲区复制文本文件)04 IO流(readLine的原理)05 IO流(MyBuffer ...

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

    提要 16 读取转换流17 写入转换流18 流操作规律-119 流操作规律-220 改变标准输入输出设备21 异常的日志信息22 系统信息 16 读取转换流 字符流体系中的InputStreamRea ...

  4. 网络编程2--毕向东java基础教程视频学习笔记

    Day 23 08 Udp接收端09 Udp键盘录入数据方式10 Udp聊天11 TCP传输12 TCP传输213 TCP练习14 TCP复制文件 08 Udp接收端 需求:定义一个应用程序,用于接收 ...

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

    Day20 01 File概述02 File对象功能-创建和删除03 File对象功能-判断04 File对象功能-获取05 File对象功能-文件列表106 File对象功能-文件列表207 列出目 ...

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

    Day20 10 创建java文件列表11 Properties简述12 Properties存取13 Properties存取配置文件14 Properties练习15 PrintWriter16 ...

  7. 网络编程3--毕向东java基础教程视频学习笔记

    Day24 01 TCP上传图片02 客户端并发上传图片03 客户端并发登录04 浏览器客户端-自定义服务端05 浏览器客户端-Tomcat服务端 01 TCP上传图片 import java.net ...

  8. 网络编程1--毕向东java基础教程视频学习笔记

    目录: 01 网络编程概述1 02 网络编程概述2 03网络编程 网络模型 04网络编程 IP地址 05网络编程 TCP和UDP 06网络编程 Socket 07网络编程 UDP发送端 01 网络编程 ...

  9. 集合3--毕向东java基础教程视频学习笔记

    Day 15 集合框架01 TreeSet02 TreeSet存储自定义对象03 二叉树04 实现Comparator方式排序05 TreeSet练习06 泛型概述07 泛型使用08 泛型类09 泛型 ...

随机推荐

  1. LeetCode——Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  2. LitePal + Gson + Volley的ORM框架尝试方案

    为了紧跟技术潮流,目前的项目开始采用ORM的思想进行重新设计. 数据库采用轻量级ORM框架LitePal,Json解析采用Gson,网络框架采用Volley. 如果只是单纯的将这些第三方框架引进来,事 ...

  3. SQL Server里ORDER BY的歧义性

    在今天的文章里,我想谈下SQL Server里非常有争议和复杂的话题:ORDER BY子句的歧义性. 视图与ORDER BY 我们用一个非常简单的SELECT语句开始. -- A very simpl ...

  4. Sql Server来龙去脉系列之一 目录篇

    从工作一直到现在都没怎么花功夫深入学习下Sql Server数据库,在使用Sql Server时90%的时间基本上都是在接触T-SQL,所以数据库这块基本上属于菜鸟级别.至于数据库的底层框架以及运行机 ...

  5. Oracle Fusion Applications (11.1.8) Media Pack and Oracle Application Development Framework 11g (11.1.1.7.2) for Microsoft Windows x64 (64-bit)

    Oracle Fusion Applications (11.1.8) Media Pack for Microsoft Windows x64 (64-bit) 重新搜索   常见问题    提示  ...

  6. zTree的使用

    一.节点模糊搜索功能:搜索成功后,自动高亮显示并定位.展开搜索到的节点. 二.节点异步加载:1.点击展开时加载数据:2.选中节点时加载数据. 前台代码如下: <script type=" ...

  7. (一)Protobuf的Java使用

    学习使用Protobuf,创建java文件 windows : 步骤一:两个文件:proto.exe,  protobuf-Java-2.4.1.jar 步骤二:建立一个工程CreateProtoBu ...

  8. WPF关闭应用程序方法

    很多人认为关闭应用程序应该很简单,例如WindowsForm里一个Application.Exit();方法就可以解决问题,但在WPF里面可别滥用,因为WPF里Application类没有该方法,倒是 ...

  9. .net接口学习笔记

    1.接口的声明 接口的声明不能包含:数据成员,静态变量:只能包含如下类型的静态成员函数的声明:方法,属性,事件,索引器.声明中不能包含任何实现的代码,而在每个成员成名的主体后,必须使用分号. 接口声明 ...

  10. Visual Studio 2013下JSON可视化工具

          Visual Studio 2013现在我们有个小工具可以实现JSON可视化,这样给我们调试JSON提供了便利. JSON这种数据格式已经比较流行,在WEB前端随处可见. 在你需要安装VS ...