メモ - 【JAVA】指定したURLのファイルの内容を表示
ホーム > メモ > 【JAVA】指定したURLのファイルの内容を表示
カウンターです。

2005.5.25

JAVAでHallo World URL編
 http://www.hellohiro.com/url.htm


≪以下、参考に加工した物≫
// 引数でURLを与える事!
// コンパイル:javac HelloWorldURL.java
// 実行 :java HelloWorldURL http://www.google.co.jp/

import java.net.*;
import java.io.*;

public class HelloWorldURL {
	public static void main(String[] args) {
		try {
			// URLクラスのインスタンスを生成
			// args[0] は、URL。

			URL helloURL = 
			new URL("http","imail.profecio.co.jp",8002,args[0]);

			// 入力ストリームを生成

			BufferedReader in = new BufferedReader(
			new InputStreamReader(
			helloURL.openStream(),"JISAutoDetect"));

			// 一行ずつ読み込みます
			String line;
			while ((line = in.readLine()) != null) {

				// 文字コード自動判別後にエンコード

				byte[] byteLine = line.getBytes();
				String strNewLine = new String(byteLine,"SJIS");

				// 表示します

				System.out.println(strNewLine);
			}

			// 入力ストリームを閉じます

			in.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

戻る
http://kank.o.oo7.jp/2016/memo/20050525.html

初版:2005.6.25
更新:2005.6.28(一部改良)
更新:2013.7.2(リニューアル)
更新:2016.8.17(リニューアル)