java Io reader writer 笔记3

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


import org.junit.Test;


/**
 * Io流:
 *  java中   一个 数字或者字母 占用     1个byte =1个字节       ;一个中文 占用      两个字节  = 2byte !!
 * 节点流 :  除了以下四个 为节点流  其余的流 均为  处理流 
 *    1、字节流 InputStream   OutputStream  抽象类 
 *  2、字符流 Reader        Writer        抽象类
 * 
 *    
 *   字节流InputStream /OutputStream  用来处理  非文本文件  : 视频 、音频、图片 等   以字节形式存储的文件。
 *   字符流Reader  /Writer         用来处理  纯文本文件 :txt 。
 * 
 *   字符流的联系  读取   写出  和复制   联系   
 *   
 *    char[] c = new char[] 数组来每次读取指定个数的 字符。
 * 
 * @author Administrator
 *
 */
public class TestFileReaderFileWriter {

/**
* 文本文件的复制 。
*/
@Test
public  void  testReaderWriterCopy(){
// 当前java工程下 的文件 : 源文件<需要读取的文件 >必须存在
// File srcfile  = new  File("1234.doc");
// File destfile  = new  File("1234copy.doc");
// 当前java工程下 的文件 : 源文件<需要读取的文件 >必须存在
File srcfile  = new  File("1234.txt");
// 目标文件可以不存在   ,在执行过程中创建  如果 目标文件存在,  则进行覆盖。
File destfile  = new  File("1234copy.txt");
// 字符流 :输入流 
FileReader fr =null;
// 字符流  :输出流 
FileWriter fw =null;
try {
fr = new FileReader(srcfile);
fw = new FileWriter(destfile);

// 字符流的 数组单位  用 char[]
char[] c = new char[100];
int len;
while((len = fr.read(c))!=-1){
// 读取的文件内容进行输出。
// String s = new String(c, 0, len);
// System.out.print(s);

// 写入到新文件中
fw.write(c, 0, len);

}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



}




@Test
public  void  testReaderWriter(){
// 当前java工程下 的文件 : 源文件<需要读取的文件 >必须存在
// File srcfile  = new  File("1234.doc");
// File destfile  = new  File("1234copy.doc");
// 当前java工程下 的文件 : 源文件<需要读取的文件 >必须存在
File srcfile  = new  File("1234.txt");
// 目标文件可以不存在   ,在执行过程中创建  如果 目标文件存在,  则进行覆盖。
File destfile  = new  File("1234copy.txt");
// 字符流 :输入流 
FileReader fr =null;
// 字符流  :输出流 
FileWriter fw =null;
try {
fr = new FileReader(srcfile);
fw = new FileWriter(destfile);
 
// 字符流的 数组单位  用 char[]
char[] c = new char[100];
int len;
while((len = fr.read(c))!=-1){
// 读取的文件内容进行输出。
String s = new String(c, 0, len);
System.out.print(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

prefectjava

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值