Java中使用字节流类读取文本内容

package cn.jbit.inputstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            // 创建一个FileInputStream对象

            File file = new File("D:\\c.txt");

            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bs = new byte[100];
            
            // 将文件的内容读入 数组中
            fileInputStream.read(bs);
            
            // 将byte数组转成String
            String str = new String(bs);
            System.out.println(str);

            // 写入文件
            FileOutputStream fileOutputStream = new FileOutputStream(
                    "D:\\c1.txt", true);
            fileOutputStream.write(bs, 0, bs.length);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

 

posted @ 2017-09-30 00:37  xiaobudong  阅读(3059)  评论(0编辑  收藏  举报