(同じ利用者による、間の5版が非表示) | |||
1行目: | 1行目: | ||
[[java.net のクラスたち|java.net]].URL [https://docs.oracle.com/javase/jp/8/docs/api/java/net/URL.html] | [[java.net のクラスたち|java.net]].URL [https://docs.oracle.com/javase/jp/8/docs/api/java/net/URL.html] | ||
私はネット上にあるリソースを取得します。 | |||
==コンストラクタ== | ==コンストラクタ== | ||
11行目: | 11行目: | ||
==例== | ==例== | ||
URL url = new URL("<nowiki>https://hoge.com</nowiki>"); | URL url = new URL("<nowiki>https://hoge.com</nowiki>"); | ||
InputStreamReader isr = new InputStreamReader(url.openStream()); | [[InputStreamReader]] isr = new InputStreamReader(url.openStream()); | ||
int i = isr.read(); | int i = isr.read(); | ||
while (i != -1) { | while (i != -1) { | ||
19行目: | 19行目: | ||
isr.close(); | isr.close(); | ||
URL url = new URL("http://dokojava.jp/favicon.ico"); | URL url = new URL("<nowiki>http://dokojava.jp/favicon.ico</nowiki>"); | ||
InputStream is = url.openStream(); | InputStream is = url.openStream(); | ||
OutputStream os = new FileOutputStream("dj.ico"); | OutputStream os = new [[FileOutputStream]]("dj.ico"); | ||
int i = is.read(); | int i = is.read(); | ||
while (i != -1) { | while (i != -1) { |
2019年7月3日 (水) 08:50時点における最新版
私はネット上にあるリソースを取得します。
コンストラクタ
- public URL(String spec) - URLオブジェクトを生成
メソッド
- openStream - 指定のURLに接続して、その接続から読み込むためにのInputStreamを返す
例
URL url = new URL("https://hoge.com"); InputStreamReader isr = new InputStreamReader(url.openStream()); int i = isr.read(); while (i != -1) { System.out.print((char) i); i = isr.read(); } isr.close();
URL url = new URL("http://dokojava.jp/favicon.ico"); InputStream is = url.openStream(); OutputStream os = new FileOutputStream("dj.ico"); int i = is.read(); while (i != -1) { os.write((byte) i); i = is.read(); } is.close(); os.close();