字符输出流FileWriter和字符输入流FileReader

来源:互联网 发布:阿里云 腾讯云 aws 编辑:程序博客网 时间:2024/05/17 15:37

流的简述及演示案例: 
输入流和输出流相对于内存设备而言. 
将外设中的数据读取到内存中:输入 
将内存的数写入到外设中:输出。 
 
字符流的由来: 
其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表。获取对应的文字。 在对这个文字进行操作。简单说:字节流+编码表 
 
字节流的两个顶层父类: 
1,InputStream  2,OutputStream. 
 
 
195 
 
 
字符流的两个顶层父类: 
1,Reader  2,Writer 
 
这些体系的子类都以父类名作为后缀。 
而且子类名的前缀就是该对象的功能。 
 
FileWriter演示: 
package cn.itcast.p2.io.filewriter; 
 
import java.io.FileWriter; 
import java.io.IOException; 
//需求:将一些文字存储到硬盘一个文件中。 
public class FileWriterDemo  { 
 
private static final String LINE_SEPARATOR =
System.getProperty("line.separator"); 
 
/** 
*  @param args 
*  @throws IOException 
*/ 
public static void main(String[] args) throws IOException  { 
 
//创建一个可以往文件中写入字符数据的字符输出流对象。 
/* 
* 既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的 * 地)。 

* 如果文件不存在,则会自动创建。 
* 如果文件存在,则会被覆盖。 

* 如果构造函数中加入true,可以实现对文件进行续写! 
*/ 
FileWriter fw  = new FileWriter("demo.txt",true); 
/* 
* 调用Writer对象中的write(string)方法,写入数据。 

* 其实数据写入到临时存储缓冲区中。 

*/ 
fw.write("abcde"+LINE_SEPARATOR+"hahaha"); 
// fw.write("xixi");
/* 
* 进行刷新,将数据直接写到目的地中。 
*/ 
196 
 
 
 
// fw.flush();
/* 
* 关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。 
*/ 
fw.close(); 
 
// fw.write("haha");// java.io.IOException: Stream closed
 
}
 

 
IOException演示: 
package cn.itcast.p2.io.filewriter; 
 
import java.io.FileWriter; 
import java.io.IOException; 
 
public class IOExceptionDemo  { 
 
private static final String LINE_SEPARATOR  = System 
.getProperty("line.separator"); 
 
/** 
*  @param args 
*  @throws IOException 
*/ 
public static void main(String[] args)  { 
 
FileWriter fw  = null; 
try  { 
 
fw  = new FileWriter("k:\\demo.txt"); 
 
fw.write("abcde"  + LINE_SEPARATOR  + "hahaha"); 
 
} catch  (IOException e)  { 
System.out.println(e.toString()); } finally  { 
if  (fw  != null) 
try  { 
fw.close(); 
} catch  (IOException e)  { 
// code... 
throw new RuntimeException("关闭失败");
}

197 
 
 
 
}
}
FileReader演示1: 
package cn.itcast.p3.io.filereader; 
 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
//需求:读取一个文本文件。将读取到的字符打印到控制台. public class FileReaderDemo  { 
 
/** 
*  @param args 
*  @throws IOException 
*/ 
public static void main(String[] args) throws IOException  { 
 
//1,创建读取字符数据的流对象。 
/* 
* 在创建读取流对象时,必须要明确被读取的文件。一定要确定该文件是存在的。 

* 用一个读取流关联一个已存在文件。 
*/ 
FileReader fr  = new FileReader("demo.txt"); 
 
int ch  =  0; 
 
while((ch=fr.read())!=-1){ 
System.out.println((char)ch); 

/* 
//用Reader中的read方法读取字符。 
int ch  = fr.read(); 
System.out.println((char)ch); 
int ch1  = fr.read(); 
System.out.println(ch1); 
int ch2  = fr.read(); 
System.out.println(ch2); 
*/ 
fr.close();
}

 
FileReader演示2: 
package cn.itcast.p3.io.filereader; 
198 
 
 
 
import java.io.FileReader; 
import java.io.IOException; 
//需求:读取一个文本文件。将读取到的字符打印到控制台. public class FileReaderDemo2  { 
 
/** 
*  @param args 
*  @throws IOException 
*/ 
public static void main(String[] args) throws IOException  { 
 
FileReader fr  = new FileReader("demo.txt"); 
/* 
* 使用read(char[])读取文本文件数据。 

* 先创建字符数组。 
*/ 
char[] buf  = new char[1024]; 
 
int len  =  0; 
 
while((len=fr.read(buf))!=-1){ 
System.out.println(new String(buf,0,len));
}
/* 
int num  = fr.read(buf);//将读取到的字符存储到数组中。 
System.out.println(num+":"+new String(buf,0,num)); 
int num1  = fr.read(buf);//将读取到的字符存储到数组中。 
System.out.println(num1+":"+new String(buf,0,num1)); 
int num2  = fr.read(buf);//将读取到的字符存储到数组中。 
System.out.println(num2+":"+new String(buf)); 
*/ 
fr.close();
}
 

140.字符流(Reader、Writer)基本使用: 
 
案例1: 
package cn.itcast.p1.io.charstream.test; 
 
import java.io.FileReader; 
import java.io.FileWriter; 
199 
 
 
 
import java.io.IOException; 
 
/* 
* 需求:将盘中的一个文件进行复制。 

* 思路: 
*  1,需要读取源, 
*  2,将读到的源数据写入到目的地。 
*  3,既然是操作文本数据,使用字符流。 

*/ 
 
public class CopyTextTest  { 
 
/** 
*  @param args 
*  @throws IOException 
*/ 
public static void main(String[] args) throws IOException  { 
 
//1,读取一个已有的文本文件,使用字符读取流和文件相关联。 FileReader fr  = new FileReader("IO流_2.txt"); //2,创建一个目的,用于存储读到数据。 
FileWriter fw  = new FileWriter("copytext_1.txt"); //3,频繁的读写操作。 
int ch  =  0; 
while((ch=fr.read())!=-1){ 
fw.write(ch); 

//4,关闭流资源。 
fw.close(); 
fr.close(); 

 

案例2: 
package cn.itcast.p1.io.charstream.test; 
 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
 
public class CopyTextTest_2  { 
 
private static final int BUFFER_SIZE  =  1024; 
 
 
200 
 
 
 
/** 
*  @param args 
*/ 
public static void main(String[] args)  { 
 
FileReader fr  = null; 
FileWriter fw  = null; 
try  { 
fr  = new FileReader("IO流_2.txt"); 
fw  = new FileWriter("copytest_2.txt"); 
 
//创建一个临时容器,用于缓存读取到的字符。 
char[] buf  = new char[BUFFER_SIZE];//这就是缓冲区。 
 
//定义一个变量记录读取到的字符数,(其实就是往数组里装的字符个数 ) int len  =  0; 
 
while((len=fr.read(buf))!=-1){ 
fw.write(buf,  0, len); 

 
} catch  (Exception e)  { 
// System.out.println("读写失败");
throw new RuntimeException("读写失败"); }finally{ 
if(fw!=null) 
try  { 
fw.close(); 
} catch  (IOException e)  { 
 
e.printStackTrace();
}
if(fr!=null) 
try  { 
fr.close(); 
} catch  (IOException e)  { 
 
e.printStackTrace();
}
}
}
 
}

0 0
原创粉丝点击