集册 Java实例教程 网页阅读测验

网页阅读测验

欢马劈雪     最近更新时间:2020-01-02 10:19:05

454
编写一个将网页存储在计算机上的应用程序,以便在未连接到Internet时就可以阅读它们。
/**
时代Java公众号
**/

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;


public class Main {


  public static void main(String[] arguments)throws MalformedURLException  {

  /**
  来 自 时代Java公众号 - nowjava.com
  **/

    try {

      URL   page = new URL("https://nowjava.com");


      // Create file object

      String filename = page.getHost() + "_" + page.getFile() + "_file.html";

      File output = new File(filename);

      try {

        FileWriter fw = new FileWriter(output);

        BufferedWriter out = new BufferedWriter(fw);


        HttpURLConnection conn = (HttpURLConnection) page.openConnection();

        conn.connect();

        InputStreamReader in = new InputStreamReader(

            (InputStream) conn.getContent());

        BufferedReader buff = new BufferedReader(in);

        String line;

        do {

          line = buff.readLine();

          if (line != null) {

            out.write(line, 0, line.length());

          }

        } while (line != null);

        o
展开阅读全文