黑马程序员——Java IO字符流、字节流(小练习)

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

                                     
                   一、字符流 
1、写(FileWriter) 
Writer基类的几种常用方法: 
a) public void write(String str) : 写入字符串 

b) public void flush():刷新该流的缓冲 

c) public  void close():关闭该流,但先刷新

2、读(FileReader) 
Reader有哪些常用的方法: 
a) public int read():读取单个字符 
b) public int read(char[] buf) :将字符读入数组。 
c) public void close():关闭该流并释放与之关联的所有资源,此处没有刷新。

下面是复制文件小练习:

import java.io.*;
class  CopyText
{
	public static void copyText()
	{
		FileWriter fw=null;
		FileReader fr=null;
		try
		{
			fr=new FileReader("d:\\1.txt");
			fw=new FileWriter("d:\\2.txt");
			int len=0;
			char[] buf = new char[1024];
			while ((len=fr.read(buf))!=-1)
			{
				fw.write(buf,0,len);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException("读写失败");
		}
		finally 
		{
			try
			{
				if (fr!=null)
				{
					fr.close();
				}
			}
			catch (IOException e)
		    {
			throw new RuntimeException("读失败");
		    }
			try
			{
				if (fw!=null)
				{
					fw.close();
				}
			}
			catch (IOException e)
		    {
			throw new RuntimeException("写失败");
		    }
		}
	}
	public static void main(String[] args) 
	{
		copyText();
	}
}
                         

                      二、字符流

字节流主要操作byte类型数据,以byte数组为准,主要操作类有InputStream(字节输入流)、OutputSteam(字节输出流)由于IputStream和OutputStream都是抽象类,所要要用这两个类的话,则首先要通过子类实例化对象。

 1、字节输出流:OutputStream

a) public void write(int b): 将一个字节数据写入数据流

b) public void write(byte[] b):将一个byte数组写入数据流

b) public void write(byte[] b,int off,int len):将一个指定范围的byte数组写入数据流

d) public void flush():刷新该流的缓冲 

c) public  void close():关闭输出流

2、 字节输入流:InputStream

a) public void available(): 可以取得输入文件的大小

b) public int read():读取内容,以数字的方式读取

c) public int read(byte b):将内容读到byte数组,同时返回读入的个数

d) public  void close():关闭输出流

下面是复制图片小练习:

import java.io.*;
class  CopyPic
{
	public static void copyPic()
	{
	FileInputStream fis=null;
	FileOutputStream fos=null;
	try
	{
		fos=new FileOutputStream("d:\\2.jpg");
		fis=new FileInputStream("d:\\1.jpg");
		int len=0;
		byte[] buf= new byte[1024];
		while ((len=fis.read(buf))!=-1)
		{
			fos.write(buf,0,len);
		}
	}
	catch (IOException e)
	{
		throw new RuntimeException("copy fail");
	}
	finally 
	{
		try
		{
			if (fis!=null)
			{
				fis.close();
			}
		}
		catch (IOException e)
	{
		throw new RuntimeException("read fail");
	}
	try
		{
			if (fos!=null)
			{
				fos.close();
			}
		}
		catch (IOException e)
	{
		throw new RuntimeException("write fail");
	}
	}
	}
	public static void main(String[] args) 
	{
		copyPic();
	}
}
下面是复制MP3小练习:

import java.io.*;
class CopyMp3 
{
	public static void copyMp3()throws IOException
	{
	BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("d:\\2.mp3"));
	BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("d:\\1.mp3"));
	int len=0;
	while ((len=bufis.read())!=-1)
	{
		bufos.write(len);
	}
	bufos.close();
	bufis.close();
	}
	public static void main(String[] args) throws IOException
	{
	long startTime=System.currentTimeMillis();
	copyMp3();
	long endTime=System.currentTimeMillis();
	System.out.println((endTime-startTime)+"毫秒");
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值