JAVA/Networking

[java/crawling] 크롤링 웹페이지 그대로 복사해오기

java나유 2022. 7. 3. 12:59
public static void main(String[] args) throws Exception{
	
		String page= "가져올 웹주소";
		URL url = new URL(page);
		/* !Stream단어가 들어가면 read만 사용 무조건 int!(Byte 이용), Reader readLine (String)! */
		/* !flush(): BufferedOUtputStream때 사용 ! */
		InputStream is = url.openStream(); //net에서만 사용 
		BufferedInputStream bs = new BufferedInputStream(is); //배열 byte[]
		byte html[]=new byte[is.available()]; //1024써도됨 
		//FileOutputStream fs = new FileOutputStream("game.html"); //방법1) 배열 값 로드 저장 
		BufferedOutputStream fs = new BufferedOutputStream(new FileOutputStream("game.html")); //방법2) 이것도 가능 속도차이.
		
		int s = 0;
		while((s=bs.read(html))!=-1) { 	//Stream은 read에 byte배열명 입력 				//String = "null" int = -1;
			fs.write(html,0,s);
			//System.out.println(s);
		}
		//is.close(); //얘는 안 해도 무방
		fs.flush();
		bs.close(); //
		fs.close(); //얘는 꼭 닫아주기
 //		FileWriter //불러올 때는 Stream이어도 FileWriter로 저장 가능
 //		System.out.println(); //readLine은 못씀
		
		
	}

}
728x90