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 ...
随机推荐
- 【WildCard Matching】cpp
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- 好书推荐——《Soft Skill》
这本书不是一本简单的叙述程序员职业规划和如何提高能力的书. 他论述了如何做一个高产,快乐,幸福的程序员,包括职业生涯,理财,学习,健身,信仰等各个方面的内容. 推荐给每一位伟大的拯救宇宙的程序员! 书 ...
- python-面向对象(股票对象举例)
股票对象实例 class Stock(object): def __init__(self,stockCode ,stockName,averagePrice_yesterday,averagePri ...
- 【CentOS】Eclipse插件egit使用
1.简介 2.安装 3.配置 4.使用 5.补充说明 参考资料: http://yufenfei.iteye.com/blog/1750124 1.简介 EGit就是一款Eclips ...
- SQLSERVER中WITH(NOLOCK)详解
在查询语句中使用 NOLOCK 和 READPAST 处理一个数据库死锁的异常时候,其中一个建议就是使用 NOLOCK 或者 READPAST .有关 NOLOCK 和 READPAST的一些技术知识 ...
- p3p之讲解
P3P是一种被称为个人隐私安全平台项目(the Platform for Privacy Preferences)的标准,能够保护在线隐私权,使Internet冲浪者可以选择在浏览网页时,是否被第三方 ...
- 一道PK赛题
Problem Description I think that you might have played the traditional Chinese ring game: The Chines ...
- ASP.NET页面刷新的实现方法总结
先看看ASP.NET页面刷新的实现方法: 第一: private void Button1_Click( object sender, System.EventArgs e ) { Response. ...
- const int *p与int *const p的区别(转:csdn,suer0101)
本文只是一篇学习笔记,是看了<彻底搞定C指针>中的相关篇幅后的一点总结,仅此而已! 一.先搞清const int *p与int const *p的区别 它们的区别就是:没有区别!! 无论谁 ...
- 删除提示 FOREIGN KEY 约束引用”
有时想删除某个表时,提示“无法删除对象 'Orders',因为该对象正由一个 FOREIGN KEY 约束引用”,原因很简单不要急躁,它被其它表的外键引用了,所以无法删除,在此只需先找到哪些表的外键引 ...