d13
java.io.FileReader
- 1 核心类 sun.nio.cs.StreamDecoder
- 2 read() 读取一个字符串
- 3 read(char[ ]) 读取多个字符串
java.io.FileWriter
- 核心类 sun.nio.cs.StreamEncoder
缓冲区字符流
- 1 BufferedReader
- 2 BufferedWriter
装饰模式
-----------------------------
1.实现方法
Buffered类继承Writer类,在类中添加Writer类型的成员变量,
对相应的方法进行重写,调用成员变量的方法进行完成
class BufferedWriter extends Writer{
Writer out;
char[ ] = new char[8192] ;
public void writer(String str){
//先将数据写入缓冲区
cb.xxx
//如果cb已经满了,在写入到out中
}
public void close(){
//1.清理cb
//2.关闭out
}
}
设计思路
对原来方法进行一个包裹
public abstract class Writer implements Appendable, Closeable, Flushable {
private char[] writeBuffer;
private static final int WRITE_BUFFER_SIZE = 1024;
protected Object lock;
protected Writer() {
this.lock = this;
}
protected Writer(Object var1) {
if (var1 == null) {
throw new NullPointerException();
} else {
this.lock = var1;
}
}
==========================================================
BufferedWriter
函数的回调
BufferedWriter 和 BufferedReader为带有默认缓冲的字符输出输入流,比没有缓冲区的效率高。
BufferedWriter
//构造方法
bufferedWriter bf = new bufferedWriter(Writer out);
//主要方法
void writer(char ch );//写入单个字符
void writer(char [ ] cbuf, int off , int len)//写入字符数据的某一个部分
void writer(String s , int off , int len)//写入字符串的某一部分
void newLine()//写入一个行分隔符
void flush()//
void close()
================================================
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
public static void main(String[] args)throws IOException {
FileWriter fw = new FileWriter("/media/ifeng/_dde_data/iotest/hello2.txt");
/*创建一个字符写入流的缓冲区对象,并和指定的要被缓冲的流对象想关联*/
BufferedWriter bufw = new BufferedWriter(fw);
//使用缓冲区的方法将数据写入到缓冲区中
bufw.write("hello world!");
bufw.newLine();
bufw.newLine();
bufw.write("hello world!2");
bufw.write("hello world!3");
bufw.close();
}
}
BufferedReader
构造方法
BufferedReader br = new BufferReader(Reader in);
主要方法
int read( );//读取单个字符
int read(char[ ] ,int off,int len);//将字符读入到数组的某一个部分,返回读取的字符数,达到尾部,返回-1
String readLine( ); //读取一个文本行
void close( ); //
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedReaderDemo {
private FileReader fr;
private char []buf = new char[1024];
private int count = 0;
private int pos = 0;
public BufferedReaderDemo(FileReader f){
this.fr = f;
}
public int myRead() throws IOException{
if(count == 0){
count = fr.read(buf);
pos = 0;
}
if(count < 0 ){
return -1;
}
int ch = buf[pos++];
count--;
return ch;
}
public String myReadLine() throws IOException{
StringBuilder sb = new StringBuilder();
int ch = 0;
while ((ch = myRead())!= -1){
if(ch == '\r')
continue;
if(ch == '\n')
return sb.toString();
sb.append((char) ch);
if(count == 0)
return sb.toString();
}
return null;
}
}
复制文本的小程序
BufferedReaderDemo.java
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedReaderDemo {
private FileReader fr;
private char []buf = new char[1024];
private int count = 0;
private int pos = 0;
public BufferedReaderDemo(FileReader f){
this.fr = f;
}
public int myRead() throws IOException{
if(count == 0){
count = fr.read(buf);
pos = 0;
}
if(count < 0 ){
return -1;
}
int ch = buf[pos++];
count--;
return ch;
}
public String myReadLine() throws IOException{
StringBuilder sb = new StringBuilder();
int ch = 0;
while ((ch = myRead())!= -1){
if(ch == '\r')
continue;
if(ch == '\n')
return sb.toString();
sb.append((char) ch);
if(count == 0)
return sb.toString();
}
return null;
}
}
BufferedWriterDemo.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
public static void main(String[] args)throws IOException {
FileWriter fw = new FileWriter("/media/ifeng/_dde_data/iotest/hello2.txt");
/*创建一个字符写入流的缓冲区对象,并和指定的要被缓冲的流对象想关联*/
BufferedWriter bufw = new BufferedWriter(fw);
//使用缓冲区的方法将数据写入到缓冲区中
bufw.write("hello world!");
bufw.newLine();
bufw.newLine();
bufw.write("hello world!2");
bufw.write("hello world!3");
bufw.close();
}
}
TextCopyByBuf.java
import java.io.*;
public class TextCopyByBuf {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/media/ifeng/_dde_data/iotest/hello2.txt");
FileWriter fw = new FileWriter("/media/ifeng/_dde_data/iotest/hello3.txt");
BufferedReader bufr = new BufferedReader(fr);
BufferedWriter bufw = new BufferedWriter(fw);
//一行一行的写入
String line = null;
while ((line = bufr.readLine())!=null){
bufw.write(line);
bufw.newLine();
bufw.flush();
}
/* //一个字节一个字节的写入
int ch = 0;
while ((ch = bufr.read())! = -1){
bufw.write(ch);
}*/
bufr.close();
bufw.close();
}
}
字节流
- 基本操作和字符流相同
- 不仅可以操作字符,还可以操作其他媒体文件
===========================================
Junit 单元测试在 intelliJ IDEA 中的应用
ps:单元测试可以没有main运行
===========================================
Copy一个jpg文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
public class ByteStreamDemo {
/*
*
* 使用字节流复制图片
*/
public void main() throws Exception{
//文件输入流(字节流)
FileInputStream fin = new FileInputStream("/media/ifeng/_dde_data/iotest/1.jpg");
//文件输出流
FileOutputStream fout = new FileOutputStream("/media/ifeng/_dde_data/iotest/qinfang.jpg");
byte [ ] buffer = new byte[1024];
int len = -1 ;
System.out.println("1111111");
while (((len = fin.read(buffer)) != -1)){
fout.write(buffer,0,len);
}
fin.close();
fout.close();
System.out.println("over");
}
}
================================
使用文件输入流读取文本文件
import java.io.FileOutputStream;
public class ByteStreamDemo2 {
public static void main(String[] args) throws Exception {
System.out.println(charset.defaultCharset());
String str = "你好中国人";
FileOutputStream fos = new FileOutputStream("/media/ifeng/_dde_data/iotest/text222.txt");
fos.write(str.getBytes());
fos.close();
System.out.println("over!");
}
}
==========================================
字节流
1.FileInputStream
支持skip()方法,skip向后跳的时候不能文件头地址,可以超过文件末尾地址
2.FileOutputStream
不支持skip
RandomAccessFile
随机访问文件,定位到文件的任意位置