黑馬程序員—IO流2

---------------------- JavaEE+Android、Java培訓、期待與您交流! ----------------------


打印流

打印流包括:PrintStream和PrintWriter。該流提供了打印方法,可將各種類型的數據都原樣打印

字節打印流:PrintStream

構造方法中可接收的參數類型:

1、file對象。File

2、字符串路徑:String

3、字符輸出流:OutputStream

字符串打印流:PrintWriter

構造方法中可接受的參數類型

1、file對象:File

2、字符串路徑:String

3、字節輸出流:OutputStream

4、字符輸出流:Writer

import java.io.*;

class PrintDemo
{
	public static void main(String[] args) throws IOException
	{
		//鍵盤錄入,創建讀取流對象
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
		//使用打印流,將文件輸出
		//輸出到屏幕
		PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);
		String line = null;
		while((line=bufr.readLine())!=null)
		{
			if("over".equals(line))
				break;
			out.println(line.toUpperCase());
			//out.flush();
		}
		bufr.close();
		out.close();
	}
}

合併流

SequenceInputStream可以將多個流連接成一個源。

構造函數:SequenceInputStream(Enumeration<? extends FileInputStream> e)

如何合併多個文件:

1、創建集合,並將流對象添加進集合

2、創建Enumeration對象,將集合元素加入。

3、創建SequenceInputStream對象,合併流對象

4、創建寫入流對象,FileOutputStream,將數據寫入流資源

5、定義數組,將讀取流數據存入數組,並將數組中元素寫入文件中。

例如:假設有三個文件,將三者合併到一個新文件中
	//合併流對象
	public static void sequenceFile()throws IOException
	{
		FileOutputStream fos = null;
		SequenceInputStream sis = null;
		try
		{
			//創建集合,存儲多個文件
			ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
			for(int i=1;i<=3;i++)
			{
				al.add(new FileInputStream(i+".part"));
			}
			//匿名內部類訪問局部變量要final
			final Iterator<FileInputStream> it = al.iterator();
			//創建Enumeration匿名對象
			Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
			{
				public boolean hasMoreElements()
				{
					return it.hasNext();
				}
				public FileInputStream nextElement()
				{
					return it.next();
				}
			};
			//合併流對象,將集合元素加入。
			sis = new SequenceInputStream(en);
			//創建寫入流對象,FileOutputStream
			fos = new FileOutputStream("7.bmp");
			byte[] b = new byte[1024*1024];
			int len = 0;
			//循環,將數據寫入流資源
			while((len=sis.read(b))!=-1)
			{
				fos.write(b);
			}

		}
		catch (IOException e)
		{
			throw new RuntimeException("文件操作失敗");
		}
		//關閉流資源
		finally
		{
			try
			{
				if(fos!=null)
					fos.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("關閉流資源操作失敗");
			}
			try
			{
				if(sis!=null)
					sis.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("關閉流資源操作失敗");
			}
		}		
	}

切割流資源

1、先關聯文件FileInputStream

2、定義寫入流變量:FileOutputStream

3、創建數組,並定義切割所需的大小|

4、循環讀寫數據,並每次創建一個新寫入流,創建完後並寫入文件中

5、關閉流資源

	//切割流對象
	public static void splitFile()throws IOException
	{
		//創建全局變量
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try
		{
			//創建文件讀取流
			fis = new FileInputStream("0.bmp");
			//創建數組
			byte[] b = new byte[1024*1024];
			int len = 0;
			//計數器
			int count = 1;
			//循環寫入數據
			while((len=fis.read(b))!=-1)
			{
				//每次創建一個新寫入流,寫入後關閉流資源
				fos = new FileOutputStream((count++)+".part");
				fos.write(b,0,len);
				fos.close();
			}
		}
		catch (IOException e)
		{
				throw new RuntimeException("關閉流資源操作失敗");
		}
		//關閉流資源
		finally
		{
			try
			{
				if(fis!=null)
					fis.close();
			}
			catch (IOException e)
			{
				throw new RuntimeException("關閉流資源操作失敗");
			}
		}
		
	}
}



---------------------- JavaEE+Android、Java培訓、期待與您交流! ----------------------

詳細請查看: http://edu.csdn.net

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章