文件字符流

package IO;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test2 {
public static void main(String[] args) {
//从文档输入到控制台
Test2.testFileReader(“F:\IO_test\test\tt11.txt”);
//将代码的信息输出文档
// Test2.testFileWritr(“F:\IO_test\test\tt8.txt”);
// Test2.copyFile(“F:\IO_test\test\tt1.txt”,“F:\IO_test\test\tt\tt1.txt”);

}

/**
 * 文件字符输入流
 * @param inPath
 */
public  static  void  testFileReader(String inPath){
    try {
        FileReader fr = new FileReader(inPath); //创建就文件字符输入流的对象
        char [] c = new char[10];
        int len = 0 ; //定义一个输入流的读取长度
        while ((len = fr.read(c)) !=-1){
            System.out.print(new String(c,0,len));
        }
        fr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 文件字符输出流
 * @param outPath
 */
public static void testFileWritr(String outPath){
    String str = "Real love is always worth waiting for";
    try {
        FileWriter fw = new FileWriter(outPath);
        fw.write(str); //写的内存中
        fw.flush(); // 写到硬盘中
        fw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

/**
 * 字符流完成拷贝文件,字符流只适合操作内容是字符文件
 * @param inPath
 * @param outPath
 */
public static void copyFile (String inPath, String outPath) {
    try {
        FileReader fr = new FileReader(inPath);
        FileWriter fw = new FileWriter(outPath);
        char[] c = new char[100];  // 缓冲组

        int len = 0;
        while ((len = fr.read(c)) != -1){
            fw.write(c,0,len);
        }
        fw.flush();
        fw.close();
        fr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值