A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 啊全 中级黑马   /  2015-5-20 00:06  /  232 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.io.bytes;

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

public class ByteTest {
public static void main(String[] args) {
        //写入文件
                        //创建文件
                        FileWriter fw =null;
                               
                                try
                                {
                                        //在里面定义的东西。外面是访问不到的
                                        fw = new FileWriter("IoTest.java");
                                        //将字符写入字节流中
                                        fw.write("io流1s5af4ef5sa4221 31s2fe3a51f2asd31f5ea1f23ads1f321212 af123s1fwe23f123啊身份213符123符231符123 f");
                                }
                                catch (IOException e){
                                        System.out.println("to"+e.toString());
                                }
                                       
                                        finally{
                                                try
                                                {
                                                        if(fw!=null)
                                                        //colse 刷新后关闭流
                                                        fw.close();
                                                }
                                                catch (IOException e)
                                                {
                                                        System.out.println("to"+e.toString());
                                                }
                                       
                        }
                       
                        //读取文件
                        FileReader fl =null;
                        try
                        {
                                fl = new FileReader("IoTest.java");
                                //读取字符
                                char[] chs = new char[1024];
                                int num = 0;

                                while((num = fl.read(chs))!=-1){
                                        System.out.println(new String(chs,0,num));
                                        if(num%5==0){
                                                System.out.println();
                                               
                                        }
                                }
                        }
                        catch (IOException e)
                        {
                                System.out.println(e);
                        }
                        finally{
                        try
                        {
                                if(fl != null)
                                fl.close();
                        }
                        catch (IOException e)
                        {
                                System.out.println(e);
                                                }
                                        }
}
}

1 个回复

倒序浏览
顶个,我也快学到IO流了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马