Back/Java

[JAVA] I/O - URL클래스, PrintWriter

RAHM 2016. 2. 3. 14:11
public static void main(String[] args) throws IOException {
  Scanner sc = new Scanner(System.in);
  System.out.println("URL 입력 : ");
  String spec = sc.nextLine();
  System.out.println("저장 할 경로 입력 : ");
  String savePath = sc.nextLine();
  URL url = new URL(spec);
  InputStream is = url.openStream();
  BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
//  BufferedWriter bw = new BufferedWriter(new FileWriter(savePath, true));
  
  //PrintWriter
  PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(savePath)),true);
  
  String str = null;
  while((str = br.readLine()) != null){
   //println()은 한줄단위로 쓴다.
   pw.println(str);
  }
  is.close();
  br.close();
//  bw.flush();
//  bw.close();
  pw.flush();
  pw.close();
 }