设为首页 加入收藏

TOP

IO (一)(二)
2017-10-16 18:20:36 】 浏览:2595
Tags:
}
catch (IOException e){ e.printStackTrace(); }finally { try { if(writer != null){ writer.close(); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭流失败"+e.getMessage()); } } } }

 

4 FileReader

4.1 FileReader简介

  • 用来读取字符文件的便捷类。

 

4.2 FileReader构造方法

  • 在给定的文件名中创建一个FileReader对象
public FileReader(String fileName)
           throws FileNotFoundException
  • 在给定的文件中创建一个FileReader对象
public FileReader(File file)
           throws FileNotFoundException

 

  • 读取单个字符,返回值是作为整数读取的字符,在0~65535之间,如果到达末尾,则返回-1
public int read()
         throws IOException

 

  • 示例:
package java19;

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

/**
 * 2017/10/12
 * 说明:
 */
public class FileReaderDemo {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("demo.txt");

            int ch = 0;
            while((ch = fr.read()) != -1){
                System.out.print((char)ch);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException("流关闭失败"+e.getMessage());
                }

            }
        }


    }
}

  • 将字符读取数组。返回读取字符的个数,如果已到达末尾,则返回-1
public int read(char[] cbuf)
         throws IOException

 

  • 示例:
package java19;

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

/**
 * 2017/10/12
 * 说明:
 */
public class FileReaderDemo {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("demo.txt");
            char[] chs = new char[1024];

            int length = 0;
            while((length = fr.read(chs)) != -1){
                System.out.print(new String(chs,0,length));
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException("流关闭失败"+e.getMessage());
                }

            }
        }


    }
}

5 练习:文件复制

5.1 文件复制方式一

  • 示例:
package java19;

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

/**
 * 2017/10/12
 * 说明:
 */
public class FileCopy {
    public static void main(String[] args) throws IOException {
        //创建输入流和输出流
        FileReader fr = new FileReader("demo.txt");
        FileWriter fw = new FileWriter("test1.txt");

        //文件复制
        int ch = 0;
        while((ch = fr.read()) != -1){
            fw.write(ch);
        }



        //关闭流
        fr.close();
        fw.close();



    }
}

 

5.2 文件复制方式二

  • 示例:
package java19;

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

/**
 * 2017/10/12
 * 说明:
 */
public class FileCopy {
    public static void main(String[] args) throws IOException {
        //创建输入流和输出流
        FileReader fr = new FileReader("demo.txt");
        FileWriter fw = new FileWriter("test2.txt");

        //文件复制
        char[] buffer = new char[1024];
        int len = 0 ;
        while((len = fr.read(buffer) )!= -1){
            fw.write(buffer,0,len);
        }

        //关闭流
        fr.close();
        fw.close();



    }
}

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇二、Tomcat配置以及IDEA运行第一.. 下一篇Java对象克隆

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目