黑马程序员---IO--常用基类字符流抽象类中的中的FileWriter FileReader的使用

------- android培训java培训、期待与您交流! ----------

IO常用基类字符流抽象类

中的FileWriter FileReader

主要方法:

write()

flush()

close()

 

练习与演示:

import java.io.*;
public class FileWriterDemo 
{
	public static void main(String[] args) throws IOException
	{
		//创建一个FileWriter对象,一旦被初始化就必须要有被操作的文件,要抛异常
		FileWriter f=new FileWriter("demo.txt");
		
		
		f.write("abcd");//写到流中,并未到指定文件中
		//f.flush();//刷新流中缓冲数据到指定目的文件中
		f.close();//关闭流,因为写的操作会调用系统的资源,所以到最后一定要执行一下close。
		f.write("a");//前边把流关闭就写不进去了,会出现stream close的提示
	}
}


 

IO异常的处理方式

import java.io.*;
public class FileWriterDemo2 
{
	public static void main(String[] args)
	{
		//别的代码块也要用到f,所以在外部定义f。
		FileWriter f=null;
		try
		{
			f=new FileWriter("demo.txt");//再在内部new对象
			f.write("jkjk");
		}
		catch (IOException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if(f!=null)//不等于空的判断
					f.close();			
			}
			catch (IOException e)
			{
				System.out.println(e.toString());
			}
		}
	}
}

在已有文件内续写

public class FileWriterDemo3 
{
	public static void main(String[] args)
	{
		//传递一个true参数,代表不覆盖已有文件,并在文件末尾处续写
		FileWriter f=null;
		try
		{
			f=new FileWriter("demo.txt",true);//在新建对象时传入true参数代表不覆盖已有文件并在文件末尾处续写。
			f.write("1234\r\nover");//windows系统中换行是\r\n
		}
		catch(IOException e)
		{
			System.out.println(e.toString());
		}
		finally
		{
			try
			{
				if(f!=null);
					f.close();
			}
			catch(IOException e)
			{
				System.out.println(e.toString());			
			}
		}	
	}
}

Read 两种方式取出

 

Read 两种方式取出

import java.io.*;
/*第一种方法*/
public class FileReaderDemo 
{
	public static void main(String[] args) throws IOException
	{
		//创建读取流功能对象,明确要读取文件
		//如果指定的文件不存在会发生FileNotFoundException,是IOException的子类
		FileReader r=new FileReader("demo.txt");
		int ch=1;
		while((ch=r.read())!=-1)
		{
			System.out.print((char)ch);//(char)将结果强转成字符类型
		}
		
	}
}

/*
 * 第二种方式:通过字符数组进行读取*/
public class FileReaderDemo2 
{
	public static void main(String[] args) throws IOException
	{
		FileReader r=new FileReader("demo.txt");
		char[] c=new char[1024];
		//循环
		int n=0;
		while((n=r.read(c))!=-1)
		{
			System.out.print(new String(c,0,n));
		}
		r.close();
	}
}

 

 

练习1

/*读取C:\Users\dell\workspace\study\src\pakage17路径下的ArraysDemo.java 文件*/

public class FileReaderTest 
{
	public static void main(String[] args) throws IOException
	{
		FileReader r=new FileReader("C:\\Users\\dell\\workspace\\study\\src\\pakage17\\ArraysDemo.java");
		
		char[] ch=new char[1024];
		
		int num=0;
		
		while((num=r.read(ch))!=-1)
		{
			System.out.print(new String(ch,0,num));
		}
	}
}


 

练习二

/*

 * 将文件复制到别处

 * 1,先在指定路径创建相同名字文件

 * 2,读取源文件内容

 * 3,将源文件内容写入到指定文件*/

import java.io.*;
public class CopyText 
{
	public static void main(String[] args)
	{
		way1();
	}
	
	public static void way1()
	{
		FileWriter w=null;
		FileReader r=null;	
		try
		{
			//创建新文件,待写入
			w=new FileWriter("d:\\demo.txt");
			//定义要复制的文件
			r=new FileReader("demo.txt");
			//读取
			char[] ch=new char[1024];
			int num=0;
			while((num=r.read(ch))!=-1)
			{
				w.write(ch,0,num);
			}
		}
		catch(IOException e)
		{
			throw new RuntimeException("读写失败");
		}
		finally
		{
			if(w!=null);
			try
			{				
				w.close();					
			}
			catch(IOException e)
			{}
			if(r!=null);
			try
			{
				r.close();	
			}	
			catch(IOException e)
			{}
		}
	}
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值