java reflectioin exam
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
Archive for the ‘ Java ’ Category
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
package com.test.url;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLSaver {
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("사용법 : java URLSaver URL filename");
System.exit(1);
}
URL url = null;
try {
url = new URL(args[0]);
} catch (MalformedURLException e) {
System.out.println("잘못된 URL 형식입니다.");
System.exit(1);
e.printStackTrace();
}
FileOutputStream fos = null;
try {
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
fos = new FileOutputStream(args[1]);
byte[] buffer = new byte[512];
int readcount = 0;
System.out.println("읽기 시작");
while((readcount = in.read(buffer)) != -1) {
fos.write(buffer, 0, readcount);
}
System.out.println("파일 저장 완료");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}