FileReader

package com.rl.io.output;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
 *FileReader的构造器
 */

public class IOReaderDemo {
	public static void main(String[] args) {
	readerFile();
	}
	
	public static void readerFile() {
		FileReader fr = null;
		try {
			
			//创建FileReader对象 
			 fr = new FileReader("helloworld.txt");
			 /**
			 //读取单个字符,返回的是字符的ascii码,强转为字符
			int num = fr.read();
			System.out.println((char)num);
			*/
			//如果整个文本读取完毕,最后没有字符会返回-1
			int num = 0;
			//通过循环来读取字符,判断跳出循环的标志是num=-1
			while((num = fr.read()) != -1){
				//打印字符
				System.out.print((char)num);
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(fr != null){
				try {
					//colse的异常处理,释放资源
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
}

package com.rl.io.output;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
 *读取方式
 */

public class IOReaderDemo1 {
	public static void main(String[] args) {
	readerFile();
	}
	
	public static void readerFile() {
		FileReader fr = null;
		try {
			
			//创建FileReader对象 
			 fr = new FileReader("helloworld.txt");
			//创建一个字符的数组
			 char[] chs =new char[5];
			 //读取5个字符放入数组当中,返回的数值是读取到的字符串
			int num = fr.read(chs);
			System.out.println(num);
			String str = new String(chs);
			System.out.println(str);
			System.out.println("------------");
			//读取1个字符放入数组当中,返回的数值是读取到的字符串
		    num = fr.read(chs);
		    if(num <5){
		    	str = new String(chs,0,num);
		    }
			
			System.out.println(num);
			System.out.println(str);
			//如果num中的数值为-1,表示数组已经读完
			char[] chs1 = new char[1024];
			int num1=0;
			//循环条件的判断边界是fr.read(char[]),如果返回值为-1,说明文件已经读取完毕
			while((num1 = fr.read(chs1) ) != -1){
				System.out.println(new String(chs1,0,num1));
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(fr != null){
				try {
					//colse的异常处理,释放资源
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
}

package com.rl.io.copyfile;

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

/**
 * 例子:把一个java文件拷贝到项目根目录
 * 分析:
 *1、创建字符输入流对象
 *2、创建字符输出流对象
 *3、把输出流输入的数据写入输出流中
 *4、关闭资源
 */
public class CopyFileDemo {
	public static void main(String[] args) {
		copy1();
		System.out.println("done");
	}
	/**
	 * 拷贝文件方式1
	 */
	public static void copy(){
		Reader reader = null;
		Writer writer = null;
		try {
			//创建文件读取对象
			reader = new FileReader("E:/eclipse/eclipse/workspace/io_demo/src/com/rl/io/output/IOReaderDemo.java");
			//创建写文件的对象
			writer = new FileWriter("IOReaderDemo.java");
			int num = -1;
			while((num = reader.read()) != -1){
				writer.write(num);
				
			}
			
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		
	   }finally{
		   
		   try {
			   //先开谁就后关闭谁
			   if(writer != null)
			writer.close();
			   if(reader != null)
			   reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}		  
	   }
	
	}
	
	/**
	 * 拷贝文件方式2
	 */
	public static void copy1(){
		Reader reader = null;
		Writer writer = null;
		try {
			//创建文件读取对象
			reader = new FileReader("E:/eclipse/eclipse/workspace/io_demo/src/com/rl/io/output/IOReaderDemo.java");
			//创建写文件的对象
			writer = new FileWriter("IOReaderDemo.java");
			//定义每次的读取的长度的对象
			int len= -1;
			//定义存储读取内容的数组
			char[] chs = new char[1024];
			//当len !=-1时,一直读取
			while((len = reader.read(chs)) != -1){
				//把读取的文件写入到目标文件中
				writer.write(chs,0,len);
				
			}
			
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		
	   }finally{
		   
		   try {
			   //先开谁就后关闭谁
			   if(writer != null)
			writer.close();
			   if(reader != null)
			   reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}		  
	   }
	
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值