Java:IO流之字符流缓冲区详解
//例子1:
import java.io.*;
class BufferedReaderDemo
{
public static void main(String[] args) throws IOException
{
//创建一个字符读入流对象,和目的地文件相关联.
FileReader fr = new FileReader("F:\\myfile\\buf.txt"); //为了提高字符读入流效率,加入了缓冲技术,只需要将被提高效率的流对象作为参数
//传递给缓冲区的构造函数即可。此时,缓冲区和流相关联.
BufferedReader buf = new BufferedReader(fr);
int num = 0;
String line = null; /*
while((line = buf.readLine())!=null) //此过程中,读入缓冲区一行一行的读取数据,效率高
{
System.out.println(line);
}
*/ while((num = buf.read())!=-1)
{
System.out.print((char)num);
} //其实关闭缓冲区,就是关闭缓冲区中的流对象
buf.close();
}
}
import java.io.*;
class MyBufferedReader extends Reader
{
private Reader r;
MyBufferedReader(Reader r)
{
this.r = r;
}
//可以一次读一行的方法
public String MyreadLine() throws IOException
{
//定义一个临时容器。原BufferedReader封装的是字符数组。
//为了演示方便,定义一个StringBuilder容器,因为最终还是要将数据变为字符串
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch=r.read())!=-1)
{
if(ch=='\r')
continue;
if(ch=='\n')
return sb.toString();
else
sb.append((char)ch);
}
if(sb.length()!=0) //出现读取一行数据到了缓冲区,但是该字符串后面没有'\n',可是仍要把它读取出来。
return sb.toString();
return null;
}
//覆盖Reader中的抽象方法
public int read(char[] cbuf, int off, int len) throws IOException
{
return r.(cbuf,off,len);
}
public void close()throws IOException
{
r.close();
} public void Myclose()throws IOException
{
r.close();
}
}
//测试类
class MyBufferedReaderTest
{
public static void main(String[] args)
{
FileReader fr = null;
MyBufferedReader mybuf = null;
String line = null;
try
{
mybuf = new MyBufferedReader(new FileReader("F:\\myfile\\buf.txt"));
while((line = mybuf.MyreadLine())!=null)
{
System.out.println(line);
}
}
catch(IOException e)
{
throw new RuntimeException("读入流异常");
}
finally
{
try
{
mybuf.Myclose();
}
catch(IOException e)
{
throw new RuntimeException("关闭流异常");
}
}
}
}
自定义一个统计行号的类:MyLineNumberReader,可以设置复制得到的文件内容的行号
import java.io.*;
/*
class MyLineNumberReader extends MyBufferedReader
{
private int linenumber;
MyLineNumberReader(Reader r)
{
super(r);
}
public void setLineNumber(int linenumber)
{
this.linenumber = linenumber;
}
public int getLineNumber()
{
return linenumber;
}
public String MyreadLine() throws IOException
{
linenumber++;//每读一行,行数就自加一次
return super.MyreadLine();
}
}
*/
class MyLineNumberReader
{
private Reader r;
private int linenumber;
MyLineNumberReader(Reader r)
{
this.r = r;
}
public void setLineNumber(int linenumber)
{
this.linenumber = linenumber;
}
public int getLineNumber()
{
return linenumber;
}
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')
return sb.toString();
else
sb.append((char)ch);
}
if(sb.length()!=0)
return sb.toString();
return null;
}
public void Myclose() throws IOException
{
r.close();
}
}
class MyLineNumberReaderDemo
{
public static void main(String[] args)
{
FileReader fr = null;
MyLineNumberReader myl = null;
String str = null;
try
{
fr = new FileReader("MyBufferedReaderTest.java");
myl = new MyLineNumberReader(fr);
//myl.setLineNumber(100); //设置第一行从101行开始
while((str = myl.MyreadLine())!=null)
{
System.out.println(myl.getLineNumber()+":"+str);
}
}
catch(IOException e)
{
System.out.println("流读入异常!");
}
finally
{
try
{
myl.Myclose();
}
catch(IOException e)
{
System.out.println("流关闭异常!");
} }
}
}
构造方法摘要
BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流。
BufferedWriter(Writer out, int sz) 创建一个使用给定大小输出缓冲区的新缓冲字符输出流。
方法摘要
void close() 关闭此流,但要先刷新它。
void flush() 刷新该流的缓冲。
void newLine() 写入一个行分隔符。具有跨平台性,相当于windows系统的"\r\n"和linux系统的"\n";
void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
void write(int c) 写入单个字符。
void write(String s, int off, int len) 写入字符串的某一部分。
import java.io.*;
class BufferedWriterDemo
{
public static void main(String[] args) throws IOException
{
//创建一个字符写入流对象,和目的地文件相关联.
FileWriter fw = new FileWriter("F:\\myfile\\buf.txt"); //为了提高字符写入流效率,加入了缓冲技术,只需要将被提高效率的流对象作为参数
//传递给缓冲区的构造函数即可。此时,缓冲区和流相关联.
BufferedWriter buf = new BufferedWriter(fw); buf.write("abcdefg");
for(int i=0;i<3;i++)
{
buf.newLine();//换行
buf.write("changjiang"+i);
//记住,只要用到缓冲区,必须要刷新
buf.flush();
}
//其实关闭缓冲区,就是关闭缓冲区中的流对象
buf.close();
}
}
import java.io.*;
class BufferedCopyTest
{
public static void main(String[] args)throws IOException
{
//创建一个读入流对象,与要被复制的文件相关联
FileReader fr = new FileReader("BufferedReaderDemo.java");
//创建一个写入流对象,与目的文件相关联
FileWriter fw = new FileWriter("BufferedReaderDemo_Copy.txt"); //创建一个读入缓冲区,与流对象相关联
BufferedReader bufr = new BufferedReader(fr);
//创建一个写入缓冲区,与流对象相关联
BufferedWriter bufw = new BufferedWriter(fw); //读取数据存入缓冲区中
String line;
while((line = bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
bufw.flush();
}
bufw.close();
bufr.close();
}
}
import java.io.*;
class BufferedCopyTest2
{
public static void main(String[] args)
{
BufferedReader bufr = null;
BufferedWriter bufw = null;
try
{
bufr = new BufferedReader(new FileReader("BufferedReaderDemo.java"));
bufw = new BufferedWriter(new FileWriter("BufferedReader_Copy.txt"));
String line;
while((line = bufr.readLine())!=null)
{
System.out.println(line);
bufw.write(line);
bufw.newLine();
bufw.flush();
}
}
catch(IOException e)
{
throw new RuntimeException("读写入异常");
}
finally
{
try
{
if(bufr!=null)
bufr.close();
}
catch(IOException e)
{
throw new RuntimeException("关闭流异常");
}
try
{
if(bufw!=null)
bufw.close();
}
catch(IOException e)
{
throw new RuntimeException("关闭流异常");
}
}
}
}
Java:IO流之字符流缓冲区详解的更多相关文章
- JAVA IO 字节流与字符流
文章出自:听云博客 题主将以三个章节的篇幅来讲解JAVA IO的内容 . 第一节JAVA IO包的框架体系和源码分析,第二节,序列化反序列化和IO的设计模块,第三节异步IO. 本文是第一节. ...
- Java IO 字节流与字符流 (五)
Java的IO流分为字符流(Reader,Writer)和字节流(InputStream,OutputStream),字节流顾名思义字节流就是将文件的内容读取到字节数组,然后再输出到另一个文件中.而字 ...
- java IO(三):字符流
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- Java IO 字节流与字符流 (三)
概述 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作的数据分为:字节流和字符流 流按流向不同分为:输入流和输出流 IO流常用基类 ...
- Java IO 字节流与字符流 (二)
1. 什么是流 Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列.和水流一样,Java中的流也具有一个“流动的方向”,通常可以从中读入一个字节序 ...
- Java——IO类,字符流写数据
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- Java——IO类,字符流读数据
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- 缓冲字符流 java.io.BufferedWriter ,java.io.BufferedReader,缓冲字符输出流:PrintWriter
package seday07; import java.io.IOException;import java.io.PrintWriter; /*** @author xingsir * 缓冲字符流 ...
- java IO流 之 字符流
字符是我们能读懂的一些文字和符号,但在计算机中存储的却是我们看不懂的byte 字节,那这就存在关于字符编码解码的问题.所以在学习Io流的字符流前我们先了解些关于编码问题. 一.字符集与字符编码 1.什 ...
- JAVA之IO流(字符流)
字符流InputStreamReader和OutputStreamWriter是Writer和Read的子类:是字节流通向字符流的桥梁,也就是可以把字节流转化为字符流. InputStreamRead ...
随机推荐
- Quartz.NET Windows
Quartz.NET Windows 服务示例 想必大家在项目中处理简单的后台持续任务或者定时触发任务的时候均使用 Thread 或者 Task 来完成,但是项目中的这种需求一旦多了的话就得将任务调度 ...
- java 参数化类型
package com.gxf.collection; import java.util.LinkedList; public class TestForT<T> { private Li ...
- BICEP单元测试计划-四则运算-测试
一.6个值得测试的具体部位,他们能够提高你的测试技巧 Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你是否可以强制 ...
- boostrap中lg,md,sm,xs
boostrap中lg,md,sm,xs分别表示多少px? .col-xs- 超小屏幕 手机 (<768px).col-sm- 小屏幕 平板 (≥768px).col-md- 中等屏幕 桌面显示 ...
- canvas圆环进度
CSS: <div class="circle"> <p><span id="loadedNum">0</span&g ...
- javascript中继承(一)-----原型链继承的个人理解
[寒暄]好久没有更新博客了,说来话长,因为我下定决心要从一个后台程序员转为Front End,其间走过了一段漫长而艰辛的时光,今天跟大家分享下自己对javascript中原型链继承的理解. 总的说来, ...
- shiro中unauthorizedUrl不起作用
解决方法: 在shiro配置文件中添加(异常全路径做key,错误页面做value) <bean class="org.springframework.web.servlet.handl ...
- POI中设置Excel单元格格式
引用:http://apps.hi.baidu.com/share/detail/17249059 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWork ...
- 【ASP.Net MVC】在AspNet Mvc使用Ajax
目录 一.使用System.Web.Mvc.Ajax 1.1 System.Web.Mvc.Ajax.BeginForm 1.2 System.Web.Mvc.Ajax.ActionLink 二.手工 ...
- HDOJ 3183 A Magic Lamp
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...