欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Loading file from URL

程序员文章站 2022-04-03 08:37:06
...

 

import java.io.BufferedReader;
import java.io.IOException;   
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;   
import java.net.URLConnection;   
import java.util.Map;
import java.util.Properties; 

/**
 * 从某个URL加载文件,可以上网络上的,也可以是本地的
 * @author zqc
 *
 */
public class LoadPropertyFileFromURL
{
    /**
     * 从URL指定位置加载资源
     * 
     * @param url 资源位置
     */
	public static void loadCommonFile(URL url)
	{
		InputStream in = null;   
        URLConnection urlConnection = null;
        
        try  
        {   
        	urlConnection = url.openConnection();   
        	urlConnection.setUseCaches(false);   
        	in = urlConnection.getInputStream();   
            
        	InputStreamReader is = new InputStreamReader(in);
        	BufferedReader br = new BufferedReader(is);
        	
        	String line = br.readLine();
        	while (null != line)
        	{
        		//line = new String(line.getBytes(),"GBK");
        		System.out.println(line);
        		line = br.readLine();
        	}
        }   
        catch (Exception e)   
        {   
            //处理  
        }   
        finally  
        {   
            if (null != in)   
            {   
                try  
                {   
                	in.close();   
                }
                catch (IOException ignore)   
                {
                	//处理
                }   
                catch (RuntimeException ignore)   
                {
                	//处理
                }   
            }   
        }   
	}
	
	/**
	 * 从指定URL位置加载properties文件
	 * 
	 * @param url 资源位置
	 */
	public static void loadPropertyFile(URL url)   
    {   
        Properties properties = new Properties();   
        InputStream in = null;   
        URLConnection urlConnection = null;
        
        try  
        {   
        	urlConnection = url.openConnection();   
        	urlConnection.setUseCaches(false);   
        	in = urlConnection.getInputStream();   
            properties.load(in);   
               
            for (Map.Entry<Object, Object> entry : properties.entrySet())   
            {   
                String key = (String)entry.getKey();   
                String value = (String)entry.getValue();
                System.out.println("key:" + key);
                System.out.println("value:" + value);
            }   
        }   
        catch (Exception e)   
        {   
            //处理  
        }   
        finally  
        {   
            if (null != in)   
            {   
                try  
                {   
                	in.close();   
                }
                catch (IOException ignore)   
                {
                	//处理
                }   
                catch (RuntimeException ignore)   
                {
                	//处理
                }   
            }   
        }   
    }
	
	public static void main(String[] args)
	{
		try
		{
			//LoadPropertyFileFromURL.loadCommonFile(new URL("file:D:/pig.txt"));
			LoadPropertyFileFromURL.loadCommonFile(new URL("file:D:/pig.properties"));
			LoadPropertyFileFromURL.loadPropertyFile(new URL("file:D:/pig.properties"));
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
	}

}

 

 

 

相关标签: URL