黑马程序员-关于IO字符流基础

------------------------------Android培训                    Java培训  期待与您的交流!---------------------------------

/*字符流的两个基类

Writer  Reader

既然IO流用于操作数据的
那么数据的最常见体现形式是:文件

那么演示操作文件FileWriter,后缀是父类名,前缀是该类的操作方法

*/
import java.io.*;
class IO_701 {
    public static void main(String args []) throws IOException{
        //创建File Writer对象。该对象一被初始化就必须明确被操作的文件。
        //而且该文件会被创建到指定目录。如果该文件已存在,将被覆盖。
        //其实该步是将数据存储到目的地址;
        FileWriter fw=new FileWriter("demo.txt");
        //此时数据并没有直接写入文件内,而存在于缓存区
        fw.write("hello");
        //清空fw的缓存数据,写入到文件中。
        fw.flush();
        fw.close();//关闭此流,关闭前会再一次清空缓存,将不能在写数据
    }
}


/*
io异常的处理方式
*/
import java.io.*;

class  IO_801
{
    public static void main(String[] args) 
    {
        FileWriter fw=null;                      //一般在前面创建引用
        try{
            fw=new FileWriter("f://demo.txt");//与文件相关联
            fw.write("hello");                  //向文件写入hello
    
            fw.flush();                          //清存
        }catch (IOException e){
            System.out.println(e.toString());
        }finally{
            try{
                if(fw!=null)
                fw.close();                      //关闭
            }catch(IOException e){
                System.out.println("close"+e.toString());
            }
        }
    }
}


/*
    对已有文件的续写。
*/
import java.io.*;
class  IO_901
{
    public static void main(String[] args) 
    {
        FileWriter fw=null;
        try{
            fw=new FileWriter("demo.txt",true);    //为true则会在后面写入
            //换行在续写,没有\r就不会换行        非跨平台的换行
            fw.write("\r\nworld");

            fw.flush();

        }catch(IOException e){
            System.out.println(e.toString());
        }finally{
            try{
            if(fw!=null)//当不等于空时,初始化成功,关闭操作需要close();
                fw.close();
            }catch(IOException e){
                System.out.println(e.toString());
            }
        }
    }
}

/*
*文本文件的读取方式
*
*第一种方式
*单个字符的读取
*
*
*第二种方式
*/
import java.io.*;


class IO_1001 
{
    public static void main(String[] args) throws Exception
    {
        //创建一个文件读取流对象,和指定的文件相关联
        //保证文件是存在的,否则会报FileNotFoundException
        FileReader fr=new FileReader("demo.txt");
    
        
/*        char a=(char)fr.read();
        char b=(char)fr.read();//会往下读取
        
        System.out.println(a);
        System.out.println(b);
*/        
        //通过循环读取文件的所有字符
        int i=0;
        while((i=fr.read())!=-1){
            System.out.println("ch="+(char)i);
        }
        fr.close();
            
    }
}

/*
*第二种方式
*
*通过字符数组缓存进行读取
*
*
*/
import java.io.*;



class IO_1002 
{
    public static void main(String[] args) throws Exception
    {
        
        FileReader fr=new FileReader("demo.txt");
        
        //定义一个字符数组,用于存储读到的字符
        //将read(char [])返回时读到字符个数
        
        //
    /*    int [] buf=new int[3];

        int num=fr.read(buf);//该read(char [])返回的是读到字符的个数

        System.out.println("num"=num+new String(buf,0,num));
        fr.close();
*/
        //字符数组循环读取文件的全部字符
        char [] buf =new char [1024];
        int s=0;
        while((s=fr.read(buf))!=-1){
            System.out.println("s"+s+" "+new String(buf,0,s));
        }
        fr.close();
    }
}



/*
*将D盘的文件复制到F盘
*
*
*/
import java.io.*;
class IO_1301 
{
    public static void main(String[] args) 
    {
        copy();
    }

    public static void copy(){
        FileReader fr=null;
        FileWriter fw=null;
        try{
            fr=new FileReader("F:\\Demo15.java");        //与原来的文件读关联
            fw=new FileWriter("G:\\Demo15_copy.txt");    //与复制文本进行写关联

            char [] buf=new char[1024];                    //创建缓存区字长1024

            int len=0;
            while((len=fr.read(buf))!=-1)            //将存在缓存区的长度返回
            {            
            //    System.out.print(new String(buf,0,changdu));
                fw.write(buf,0,len);                //写入到新文件中去
            }

        }catch(IOException e){
            System.out.println(e.toString());
            throw new RuntimeException("复制失败");        //抛出复制失败的异常
        }finally{

            if(fr!=null){
                try{
                    fr.close();                            //关闭两个流的try-catch
                }catch(IOException e){
                    System.out.println(e.toString());
                }
            }
            if(fw!=null){
                try{
                    fw.close();        
                }catch(IOException e){
                    System.out.println(e.toString());
                }
            }
        }
    }
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值