JAVA NIO 文件保存

tag: java nio 保存文件

/**
	 * 读取文件
	 * */
	public  ByteBuffer readFile(String filename){  
		FileChannel fiChannel;
		MappedByteBuffer mBuf=null; 
		try {
			fiChannel = new FileInputStream(filename).getChannel();
			mBuf = fiChannel.map(FileChannel.MapMode.READ_ONLY, 0, fiChannel.size());  
			fiChannel.close();  
			fiChannel = null;  
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return mBuf;        

	}  
	/**
	 * 保存文件
	 * */
	public void saveFile(ByteBuffer src, String filename){  
		FileChannel foChannel;
		try {
			foChannel = new FileOutputStream(filename).getChannel();
			foChannel.write(src);  
			foChannel.close();    
			foChannel = null;   
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}  


你可能感兴趣的:(java,String,null)