java2--文件io流操作【例】

字節流

//2012.8.9
//IO流操作
import java.io.*;
public class IoDemo{
	public static void main(String args[]){
		OutputStream out = null;	//輸出流
		InputStream in = null;		//輸入流
		//目錄
		String locket = "g:" + File.separator + "java" 
				+ File.separator + "do" + File.separator + "temp.txt";
		//輸出過程
		try{
			File f = new File(locket);
			out = new FileOutputStream(f);
			String wrt = "Hello World!!!Hello World!!!Hello " +
						"World!!!Hello World!!!Hello World!!!Hello World!!!";
			byte b[] = wrt.getBytes();//轉換爲byte數組類型 
			out.write(b);
			System.out.println("寫入完成!");
			out.close();
		}catch(Exception e){
			e.printStackTrace();	
		}
		//輸入過程
		try{
			File f = new File(locket);
			in = new FileInputStream(f);
			byte b[] = new byte[in.available()];//定義biye數組 長度爲文件大小
			in.read(b);
			String wrt = new String(b,0,b.length);
			System.out.println(wrt);
			in.close();	
		}catch(Exception e){
			e.printStackTrace();	
		}	
	}	
}
這樣的話會不會有長度限制??是不是隻能達到1024?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章