关于IO的字节流写文件的一点问题,谢谢大家了.
BufferedOutputStream   bs   =   null; 
 	FileOutputStream   fos   =   null; 
 	try{ 
 		fos   =   new   FileOutputStream(fileName); 
 		bs   =   new   BufferedOutputStream(fos); 
 		//fos.write(outputContent.getBytes()); 
 		bs.write(outputContent.getBytes(),0,outputContent.getBytes().length); 
 		}catch(
FileNotFoundException   fnfe){ 
 			fnfe.printStackTrace(); 
 		}catch(
IOException   ioe){ 
 			ioe.printStackTrace(); 
 		}   
 我用被注释掉的fos.write写文件时,可以写到文件中,可我用bs.write写文件时,就写不进去,写的内容是空的.请问一下,这是怎么回事?谢谢大家了.   
 以下是源文件:   
 public   class   TestByteStream   {  	 
 	public   static   void   main(String[]   args)   {  		 
 		//the   content   that   will   be   write   in   a   file 
 		String   outputContent   =    "these   word   outputed   by   byte   stream "; 
 		//set   file   path 
 		String   filePath   =    "TestByteStream.txt "; 
 		File   fileName   =   new   File(filePath); 
 		try{ 
 			if(!fileName.exists()){ 
 				fileName.createNewFile(); 
 			} 
 		}catch(IOException   ioe){ 
 			ioe.printStackTrace(); 
 		}  		 
 		//output   some   charactors   by   byte   stream 
 		BufferedOutputStream   bs   =   null; 
 		FileOutputStream   fos   =   null; 
 		try{ 
 			fos   =   new   FileOutputStream(fileName); 
 			bs   =   new   BufferedOutputStream(fos); 
 			//fos.write(outputContent.getBytes()); 
 			bs.write(outputContent.getBytes(),0,outputContent.getBytes().length); 
 		}catch(File
NotFoundException   fnfe){ 
 			fnfe.printStackTrace(); 
 		}catch(IOException   ioe){ 
 			ioe.printStackTrace(); 
 		} 
 	}   
 }///:~ 
------解决方案--------------------bs.write(outputContent.getBytes(),0,outputContent.getBytes().length);     
 在其后要加 
 bs.flush();   
 才能将缓冲区的内容真正写到磁盘上。
------解决方案--------------------因为BufferedOutputStream自己在内存中开辟了一个缓冲区,每次写到它的字符它都存到缓冲区中,当缓冲区满了,它才一次性的写入到磁盘上,这样可以减少磁盘的IO操作,提高写入操作的效率。BufferedOutputStream开辟的这个缓冲区有个默认的大小,而你these word outputed by byte stream字符串长度没达到缓冲区的大小,所以需要强制的flush,以便把缓冲区中的内容写入到磁盘。   
 你可以试试改一下代码看看: 
 fos = new FileOutputStream(fileName); 
 bs = new BufferedOutputStream(fos,20); 
 //fos.write(outputContent.getBytes()); 
 bs.write(outputContent.getBytes(),0,outputContent.getBytes().length); 
 //bs.flush();   
 这个时候不用flush它也会向磁盘写入数据的,因为达到了它的缓冲区大小,它就会自动的flush了,   
 你把20改为200试试,如果不手动的flush,它就不会写磁盘。