黑马程序员:FileReader介绍及应用

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

创建一个文件读取流对象,和指定名称的文件相关联

要保证该文件已经存在,如果不存在,会发生异常FileNotFoundException
FileReader fr = new FileReader("demo.txt");
//调用读取流对象的read方法  
int ch1 = fr.read();    //第一种方式:取后直接打印
int ch2 = fr.read(); //若已达到流的末尾,则返回-1
System.out.println("ch1"+(char)ch1);
System.out.println("ch2"+ch2);

通过循环来遍历所有字符
int ch = 0;
while((ch=fr.read())!=-1)
{
System.out.println((char)ch);
}

第二种方式读取流对象:通过字符数组进行读取  取后先存再打印
FileReader fr = new FileReader("demo.txt");
//定义一个字符数组,用于存储读取到的字符
//该read(char[])返回的是都到字符个数
char[] buf = new char[3];  //一般会定义成 char[1024]
int num = fr.read(buf);
int num1 = fr.read(buf);
int num2 = fr.read(buf);
若demo.txt文件中为 adbcdefg
这返回 num=3,abc
num1=3,def
num2=1,gef //为什么会有ef,因为在内存中读的时候,根据数组长度,长度完了就从头回来覆盖,所以把d覆盖成g,而ef未变  所以在输出的时候new String(buf,0,num) 有几个元素就把几个元素转换成字符串 (copyValueOf(buf,0,num))

循环遍历
int num = 0;
while((num=fr.read(buf)!=-1)
{
//System.out.print(new String(buf,0,num));
System.out.print(String.copyValueOf(buf,0,num));
}

应用:

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


/*
 * 练习题一:两种方法将DateTest.java复制到dt.txt;
 * read()
 * read(char[]); //推荐使用这种
 * 练习题二:将DateTest.java文件字符打印在Console上
 */
public class IOTest {


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//copyFile_1("DateTest.java","2.txt");
		//copyFile_2("2.txt","1.txt");
		printFile("DateTest.java");
	}
	//方法一,逐个读取复制
	public static void copyFile_1(String originalfile, String newfile)
	{
		FileReader fr = null;
		FileWriter fw = null;
		try
		{
			fr = new FileReader(originalfile);
			fw = new FileWriter(newfile);
			int num = 0; //fr.read()如果不定义变量,再用fr.read()的时候返回的就是下一个值
			while((num=fr.read())!=-1)
			{
				fw.write((char)num);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException("文件复制失败"); 
		}
		finally
		{
			try
			{
				if(fw!=null)
					fw.close();
			}
			catch(IOException e)
			{
				throw new RuntimeException("2.txt文件关闭失败");
			}
			try
			{
				if(fr!=null)
					fr.close();
			}
			catch(IOException e)
			{
				throw new RuntimeException("DateTest.java文件关闭失败");
			}
		}
	}
	//第二种方式复制文件
	public static void copyFile_2(String originalfile, String newfile)
	{
		FileReader fr = null;
		FileWriter fw = null;
		try
		{
			fr = new FileReader(originalfile);
			fw = new FileWriter(newfile);
			char[] chs = new char[1024];
			int len = 0;
			while((len=fr.read(chs))!=-1)
			{
				fw.write(chs,0,len);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException("文件复制失败");
		}
		finally
		{
			try
			{
				if(fw!=null)
					fw.close();
			}
			catch(IOException e)
			{
				throw new RuntimeException(newfile+"文件关闭失败");
			}
			finally
			{
				try
				{
					if(fr!=null)
						fr.close();
				}
				catch(IOException e)
				{
					throw new RuntimeException(originalfile+"文件关闭失败");
				}
			}
		}
	}
	//将DateTest.java打印在console上
	public static void printFile(String filename)
	{
		FileReader fr = null;
		try
		{
			fr = new FileReader(filename);
			int len = 0;
			char[] chs = new char[1024];
			while((len=fr.read(chs))!=-1)
			{
				//sop(new String(chs,0,len));
				sop(String.copyValueOf(chs,0,len));
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException(filename+"文件读取失败");
		}
		finally
		{
			try
			{
				if(fr!=null)
					fr.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException(filename+"文件关闭失败");
			}
		}
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}


}


---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值