提要

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. RandomUser – 生成随机用户 JSON 数据的 API

    RandomUser 是一个 API,它为您提供了一个或者一批随机生成的用户.这些用户可以在 Web 应用程序原型中用作占位符,将节省您创建自己的占位符信息的时间.您可以使用 AJAX 或其他方法来调 ...

  2. 将excel导入mysql(使用navicat)

    excel: 注: 1.mysql里建立一张跟excel一样的表结构的表(包含id) 2.excel最好没有任何格式,只是纯值,不然会出现导入不了的错误 ----------------------- ...

  3. Erlang进程的Link机制

    这篇文章还不是最终版,有时间时,我会再来补充完善. 什么是link Erlang程序基于进程建模,进程之间的交互机制有收发消息,link和monitor.其中,收发消息通常用于正常的进程间通讯,而li ...

  4. 移动端页面使用单位的问题:关于px、百分比、em、rem开发中逐渐转换的问题记录

    开始写前端页面也有了快两年时间,从一开始的懵逼到现在的淡定,但是不能改变我还是一只小菜鸟的事实,平时遇到的一些问题都会记录在文件夹里,现在都整理一下大家一起分享自己平时也翻翻看看~ 不知道大家平时写的 ...

  5. Direct3D11学习:(零)常见问题及解决方法整理

    转载请注明出处:http://www.cnblogs.com/Ray1024   一.概述 在D3D11学习的这个系列中,单独写一篇文章来记录自己学习过程中遇到的问题及最后的解决方法. 这篇文章的目的 ...

  6. 表上的DELETE操作

    在今天的文章里,我想给你快速展示下当我们从表里删除记录时,在SQL Server里发生了什么.首先我们来创建一个简单的表,在8KB的页上刚好能插入4条记录. -- Create a simple ta ...

  7. QCustomplot使用分享(五) 布局

    一.历史对比 关于QCPLayoutElement这个元素的讲解之前,我想先对1.3.2release版本和2.0.0beta版本的该元素做以简单的对比介绍,首先,1.3.2release版本时,鼠标 ...

  8. 数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!

    Solve the puzzle, Save the world! Problem Description In the popular TV series Heroes, there is a ta ...

  9. [CLR via C#]8. 方法

    一.实例构造器和类(引用类型) 类实例构造器是允许将类型的实例初始化为良好状态的一种特殊的方法. 类实例构造器方法在"方法定义元数据表"中始终叫.ctor(代表constructo ...

  10. [PHP] java读取PHP接口数据

    和安卓是一个道理,读取json数据 PHP文件: <?php class Test{ //日志路径 const LOG_PATH="E:\phpServer\Apache\logs\\ ...