码迷,mamicode.com
首页 > 移动开发 > 详细

android开发字符流

时间:2016-03-17 11:00:09      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

字符流:读写文件时,以字符为基础

 

字节输入流 :reader抽象类 常用子类:FileReader

用法:int read(char 【】c,int off,int len)

 

字节输出流:writer抽象类 常用子类:FileWriter

用法:void writer(char 【】 c ,int off,int len)

 

demo:

package test;
import java.io.*;
public class TestChar {
    public static void main(String args []) {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        char c [] = new char[100];
        try {
            fileReader = new FileReader("/Users/ningyu/Desktop/from.txt");
            fileWriter = new FileWriter("/Users/ningyu/Desktop/to.txt");
            
            while(true){
                int temp = fileReader.read(c, 0, c.length);
                
                if (temp == -1) {
                    break;
                }
                
                fileWriter.write(c, 0, c.length);
            }
            
//            System.out.println(temp);
            
            for(int i = 0;i < c.length; i++){
                System.out.println(c[i]);
            }
            
            
            
        } catch (Exception e) {
            System.out.println(e);
        }finally {
            try {
                fileReader.close();
                fileWriter.close();
            } catch (Exception e2) {
                System.out.println(e2);
            }
        }
    }
}

android开发字符流

标签:

原文地址:http://www.cnblogs.com/ningxiaoge/p/5286388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!