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();
}
}
}
上一篇: 史上最小机械键盘开卖:可爱得不能自已
推荐阅读
-
记录无法安装mysql-Invalid GPG Key from file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql的解决办法
-
记录无法安装mysql-Invalid GPG Key from file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql的解决办法
-
C# copy files from source directory to destination file and rename repeated files and does not override
-
webpack file-loader和url-loader的区别
-
C# extract img url from web content then download the img
-
解决启动MongoDB错误:error while loading shared libraries: libstdc++.so.6:cannot open shared object file:
-
关于mysql启动问题---mysqld_safe mysqld from pid file * ended
-
webpack file-loader和url-loader的区别
-
C# copy files from source directory to destination file and rename repeated files and does not override
-
解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file