简单实现JAVA序列化、反序列化

public class SerializeMyUtils {
	
	private SerializeMyUtils(){};
	
	
	public static Object unserialize(byte[] bytes){
		
		if(null == bytes || bytes.length == 0){
			return null;
		}
		
		ByteArrayInputStream bais = null;
		ObjectInputStream ois = null;
		Object obj = null;
		try{
			bais = new ByteArrayInputStream(bytes);
			ois = new ObjectInputStream(bais);
			obj = ois.readObject();	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(ois != null){
					ois.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			try{
				if(bais != null){
					bais.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return obj;
	}
	
	
	public static byte[] serialize(Object obj){
		if(null == obj){
			return null;
		}
		ByteArrayOutputStream baos = null;
		ObjectOutputStream oos = null;
		byte[] bs = null;
		try{
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(obj);
 			bs = baos.toByteArray();
 		}catch(Exception e){
			e.printStackTrace();
		}finally{
				try {
					if(null != oos){
 						oos.close();
					}
				} catch (IOException e) {
 					e.printStackTrace();
				}
				try {
					if(null != baos){
						baos.close();
					}
				} catch (IOException e) {
 					e.printStackTrace();
				}
		}
		return bs;
	}
}

 

 

    简单读取的代码:

	public static void main(String[] args){
		URLConnection myConn = null;
		InputStream in = null;
		BufferedReader br = null;
		URL myUrl = null;
		try{
			myUrl = new URL("https://www.aicailang.com/new/invManadmcAgrPc");  
	        myConn = myUrl.openConnection();  //打开连接  
	        in = myConn.getInputStream();       //获取输入流  
			br = new BufferedReader(new InputStreamReader(in));
			String line = null;
			while((line = br.readLine()) != null){
				System.out.println(line);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(null != br){
				try {
					br.close();
				} catch (IOException e) {
 					e.printStackTrace();
				}
			}
			
			if(null != in){
				try {
					in.close();
				} catch (IOException e) {
 					e.printStackTrace();
				}
			}
		}
	}

 

流的转换简单实现

	 /**
	  * 获取字节流文件
	 * @param f
	 * @return
	 */
	public static byte[] getBytesFromFile(File f) {
	        try {
				FileInputStream fileInputStream = null;
				try {
				    fileInputStream = new FileInputStream(f);
				    byte[] b = new byte[fileInputStream.available()];
				    fileInputStream.read(b);
				    return b;
				} catch (Exception e) {
				    throw e;
				} finally {
				    try {
				        if (fileInputStream != null) {
				            fileInputStream.close();
				        }
				    } catch (IOException e) {
				    }
				}
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}
	    	return null;
	    }
	    
	    /**
	     * 写文件
	     * @param savePath
	     * @param data
	     */
	    public static void wirteDataToFile(String savePath,byte[] data){
	        try {
				FileOutputStream fileOutputStream=null;
				try {
				    fileOutputStream = new FileOutputStream(savePath);
				    fileOutputStream.write(data);
				    fileOutputStream.flush();
				} catch (Exception e) {
				    throw e;
				} finally {
				    try {
				        if (fileOutputStream != null) {
				            fileOutputStream.close();
				        }
				    } catch (IOException e) {
				    }
				}
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}
	    }
	
	 /**
	  * 创建临时文件目录
	 * @param prefix
	 * @param suffix
	 * @return
	 */
	public static File createTempDir(){
		try {
			//File  tempPath = new File("./temp"); //创建相对路径的临时文件目录
			File tempPath = new File(System.getProperty("java.io.tmpdir"));
			if(!tempPath.exists() || !tempPath.isDirectory()){
			    tempPath.mkdir();  //如果不存在,则创建该文件夹
			}
			return tempPath;
		} catch (Exception e) {
  			LOGGER.error(e.getMessage(), e);
		}
		return null;
	 }

	private static File createTempFile(String prefix, String suffix, File tempPath)throws IOException {
		File tempFile = File.createTempFile(prefix, suffix, tempPath);
		return tempFile;
	}

 

猜你喜欢

转载自wo-niu.iteye.com/blog/2291326