码迷,mamicode.com
首页 > 编程语言 > 详细

java 字节输入输出流

时间:2016-05-12 12:10:10      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

/*
字节流:
InputStream
OutputStream

*/
import java.io.*;
class  FileStream
{
	public static void main(String[] args) throws IOException
	{
		outputFile();
		//inputFile_1();
		inputFile_2();
	}

	/*不利用数组进行缓冲的方法*/
	public static void outputFile() throws IOException //写
	{
		FileOutputStream fos=new FileOutputStream("D:\\myfile\\mycode\\4.txt");

		fos.write("abndks你好".getBytes());

		fos.close();
	}

	public static void inputFile() throws IOException//读
	{
		FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");
		
		int ch=0;
		while((ch=fis.read())!=-1)
		{
			System.out.println((char)ch);
		}

		fis.close();
	}


	/*利用数组进行缓冲的方法进行读*/

	public static void inputFile_1() throws IOException//读
	{
		FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");
		
		int num=0;
		byte[] b=new byte[1024];
		while((num=fis.read(b))!=-1)
		{
			System.out.println(new String(b,0,num));
		}

		fis.close();
	}

	/*使用字节流对象中特有的available获取字节个数。*/
	public static void inputFile_2() throws IOException//读
	{
		FileInputStream fis=new FileInputStream("D:\\myfile\\mycode\\4.txt");
		
		int num=fis.available();

		byte[] b=new byte[num];
		
		int x=fis.read(b);
		System.out.println("x:"+x);
		System.out.println("num:"+num);
		System.out.println(new String(b));

		fis.close();
	}
}

复制图片:

/*
拷贝图片:
思路:
1,用字节读取流对象和图片关联。
2,用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。
3,通过循环读写,完成数据的存储。
4,关闭资源。

*/
import java.io.*;
class CopyPic 
{
	public static void main(String[] args) 
	{
		FileInputStream fis=null;
		FileOutputStream fos=null;
		try
		{
			System.out.println("1111");
			fis=new FileInputStream("D:\\20.jpg");
			System.out.println("2222");
			fos=new FileOutputStream("C:\\test\\new.jpg");
			System.out.println("3333");

			byte[] b=new byte[1024];
			int len=0;
			while((len=fis.read(b))!=-1)
			{
				fos.write(b,0,len);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException("复制文件失败");
		}
		finally
		{
			try
			{
				if(fis!=null)
					fis.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("读取关闭失败");
			}

			try
			{
				if(fos!=null)
					fos.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("写入关闭失败");
			}
			

		}
	}

}

复制MP3文件,使用字节流缓冲区

/*
通过复制MP3媒体文件来使用缓冲区

*/
import java.io.*;

class CopyMp3 
{
	public static void main(String[] args) 
	{
		long start=System.currentTimeMillis();
		copy_1();
		long end=System.currentTimeMillis();
		System.out.println((end-start)+"毫秒");
		
	}

	public static void copy_1()
	{
		FileInputStream fis=null;
		FileOutputStream fos=null;

		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;

		try
		{
			fis=new FileInputStream("D:\\CloudMusic\\Lonely.mp3");

			fos=new FileOutputStream("D:\\myfile\\mycode\\new.mp3");

			bis=new BufferedInputStream(fis);
			bos=new BufferedOutputStream(fos);

			int by=0;
			while((by=bis.read())!=-1)
			{
				bos.write(by);
			}
		}
		catch (IOException e)
		{
			throw new RuntimeException("复制失败");
		}
		finally
		{
			try
			{
				if(bis!=null)
					bis.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("读取流关闭失败");
			}

			try
			{
				if(bos!=null)
					bos.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("写入流关闭失败");
			}
		}
	}
}


java 字节输入输出流

标签:

原文地址:http://blog.csdn.net/iemdm1110/article/details/51372378

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!