4. JAVA
[java] URL 파일 내용 읽기
자르르
2022. 9. 13. 18:09
은근 자주 쓰는데, 할때 마다 헷갈리고, 찾아본다..
public void readUrlFile(String url) {
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
String inputLine;
while((inputLine = in.readLine()) != null) {
sb.append(inputLine+"\n");
}
in.close();
} catch (Exception e) {
System.out.println("error:"+e.getMessage());
} finally {
if( in != null) {
try {
in.close();
}catch (IOException ie){}
}
}
System.out.println(sb.toString());
}