黑马程序员——第18天——IO输入与输出(概述,字符流)



/*
字符流和字节流:

字节流两个基类:
InputStream OutputStream

字符流两个基类:
Reader Writer

先学习一下字符流的特点。

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

那么先以操作文件为主来演示。

需求:在硬盘上,创建一个文件并写入一些文字数据。

找到一个专门用于操作文件的Writer子类对象。FileWriter。 后缀名是父类名。前缀名是该流对象的功能。

import java.io.*;
class  FileWriterDemo
{
	public static void main(String[] args) throws IOException
	{
		//创建一个FileWriter对象,该对象一被初始化就必须要明确被操作的文件。
		//而且该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。
		//其实该步就是在明确数据要存放的目的地。
		FileWriter fw = new FileWriter("demo.txt");
		
		//调用write方法,将字符串写入到流中。
		fw.Write("abcde");

		//刷新流对象中的缓冲中的数据。
		//将数据刷到目的地中。
		//fe.flush();

		//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据。
		//将数据刷到目的地中。
		//和flush区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。
		fw.close();	
	}
}

<strong>IO异常的处理方式。(必须写)</strong>
import java.io.*;
class  FileWriterDemo2
{
	public static void main(String[] args) 
	{
		FileWriter fw = null;//在代码块外面建立引用,在try内进行初始化
		try
		{
			fw = new FileWriter("demo.txt");

			fw.write("abcdefg");
		}
		catch (IOException e)
		{
			System.out.println("catch:"+e.toString());
		}
		finally
		{
			try
			{
				if(fw!=null)//函数健壮性判断,必须有!
					fw.close();//流要分别一个一个的关。			
			}
			catch (IOException e)
			{
				System.out.println(e.toString());
			}
		}
	}
}

<strong>演示对已有文件的数据续写</strong>
import java.io.*;

class  FileWriterDemo3
{
	public static void main(String[] args) 
	{
		FileWriter fw = null;
		try
		{
			//传递一个true参数,代表不覆盖已有的文件。并在已有文件的末尾处进行数据的续写。
			fw = new FileWriter("demo.txt",true);

			fw.write("haha\r\nxiexie");


		}
		catch (IOException e)
		{
			System.out.println("catch:"+e.toString());
		}
		finally
		{
			try
			{
				if(fw!=null)
					fw.close();				
			}
			catch (IOException e)
			{
				System.out.println(e.toString());
			}
		}
	}
}

读文件
import java.io.*;
class  FileReaderDemo
{
	public static void main(String[] args) 
	{
		//创建一个文件读取流对象,和指定名称的文件相关联。
		//要保证该文件是已经存在的,如果不存在,就好发生异常FileNotFoundException。
		FileReader fr = new FileReader("demo.txt");

		//调用读取流对象的read方法。
		//read():一次读一个字符。而且会自动往下读。
		
		int ch = 0;
		
		while((ch=fr.read())!=-1)
		{
		`	System.out.println((char)ch);
		}
				
		/*
		while(true)
		{
			int ch = fr.read();
			if(ch==-1)
				break;
			System.out.println("ch="+(char)ch);
		}
		*/
		fr.close();
	}
}

/*
第二种方式:通过字符数组进行读取。
*/

import java.io.*;
class FileReaderDemo2 
{
	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("demo.text");

		//定义一个字符数组,用于存储读到字符。
		//该read(char[])返回的是读到字符个数。
		char[] buf = new char[1024];
		
		int num = 0;
		while((num=fr.read())!=-1)
		{
			System.out.println(new String(buf,0,num));
		}

/*
		int num = fr.read(buf);
			
		System.out.println("num="+num+"..."+new String(buf));

		int num1 = fr.read(buf);

		System.out.println("num="+num1+"..."+new String(buf));

		int num2 = fr.read(buf);

		System.out.println("num="+num2+"..."+new String(buf));
*/
		fr.close();
	}
}

练习
<strong>//读取一个.java文件,并打印在控制台上。</strong>
class  FileReaderTest
{
	public static void main(String[] args) throws IOException
	{
		FileReader fr = new FileReader("DateDemo.java");
		
		char[] buf = new char[1024];

		int num = 0;
		
		while((num=fr.read(buf))!=-1)
		{
			System.out.print(new String(buf,0,num));
		}
		
		fr.close();
	}
}
<strong>
将C盘一个文本文件复制到D盘。
重点掌握!</strong>
/*
复制的原理:
其实就是将C盘下的文件数据存储到D盘的一个文件中。

步骤:
1,在D盘创建一个文件。用于存储C盘文件中的数据。
2,定义读取流和C盘文件关联。
3,通过不断的读写完成数据存储。
4,关闭资源。
*/

import java.io.*;

class CopyText 
{
	public static void main(String[] args) throws IOException
	{
		//copy_1();
		copy_2();
	}

	public static void copy_2()
	{
		FileWriter fw = null;
		FileReader fr = null;
		try
		{
			fw = new FileWriter("SystemDemo_copy.txt");
			fr = new FileReader("SystemDemo.java");

			char[] buf = new char[1024];

			int len = 0;

			while((len=fr.read(buf))!=-1)
			{
				fw.write(buf,0,len);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException("读写失败");
		}
		finally
		{
			if(fr!=null)
				try
				{
					fr.close();
				}
				catch (IOException e)
				{
				}
			if(fw!=null)
				try
				{
					fw.close();
				}
				catch (IOException e)
				{
				}
		}
	}


	//从C盘读一个字符,就往D盘写一个字符。
	public static void coy_1()throws IOException
	{
		//创建目的地。
		FileWriter fw = new FileWriter("RuntimeDemo_copy.txt");

		//与已有文件关联。
		FileReader fr = new FileReader("RuntimeDemo.java");

		int ch = 0;

		while((ch=fr.read()!=-1))
		{
			fw.write(ch);
		}
		fw.close();
		fr.close();
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值