读取 URL 的前 5 行,然后按相反顺序打印

发布于 2024-10-27 14:24:37 字数 735 浏览 5 评论 0原文

我想编写一个简单的程序,读取 URL 的前五行,然后以相反的顺序打印它们,所以它是第 5 行、第 4 行、第 3 行、第 2 行、第 1 行。

这是我的内容到目前为止:

public static void main(String[] arg) throws Exception {

    BufferedReader keyboard;
    String inputLine;

    keyboard = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Please enter the name of a company (without spaces): ");
    System.out.flush();        /* Make sure the line is printed immediately. */
    inputLine = keyboard.readLine();

    URL u = new URL("http://www." + inputLine + ".com/");
    InputStream ins = u.openStream();

    InputStreamReader isr = new InputStreamReader(ins);
    BufferedReader readURL = new BufferedReader(isr);

做我想做的事情最优雅的方式是什么?

I want to write a simple program that reads the first five lines of a URL and then prints them out in reverse order, so it'd be line 5, line 4, line 3, line 2, line 1.

Here's what I've got so far:

public static void main(String[] arg) throws Exception {

    BufferedReader keyboard;
    String inputLine;

    keyboard = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Please enter the name of a company (without spaces): ");
    System.out.flush();        /* Make sure the line is printed immediately. */
    inputLine = keyboard.readLine();

    URL u = new URL("http://www." + inputLine + ".com/");
    InputStream ins = u.openStream();

    InputStreamReader isr = new InputStreamReader(ins);
    BufferedReader readURL = new BufferedReader(isr);

What would be the most elegant way to do what I'm trying to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-11-03 14:24:37

直接从 URL 读取作为初始示例,因为它已经是一段优雅的代码,可以根据您的需要进行调整。例如...

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

public class DataURL {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                yahoo.openStream()));

        String inputLine;

        int i=5; /* number lines */
        List<String> lines = new ArrayList<String>();
        while (i>0 && (inputLine = in.readLine()) != null) {
            lines.add(inputLine);
            i--;
        }   
        in.close();

        for (i=lines.size()-1; i >= 0; i--) {
            System.out.println("Line " + i + ": " + lines.get(i));
        }   
    }   
}

这段代码只是读取前五行,然后以相反的顺序输出它们。

Take Reading Directly from a URL as a initial example, since it is already an elegant piece of code and accomodate it to your needs. For example ...

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

public class DataURL {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                yahoo.openStream()));

        String inputLine;

        int i=5; /* number lines */
        List<String> lines = new ArrayList<String>();
        while (i>0 && (inputLine = in.readLine()) != null) {
            lines.add(inputLine);
            i--;
        }   
        in.close();

        for (i=lines.size()-1; i >= 0; i--) {
            System.out.println("Line " + i + ": " + lines.get(i));
        }   
    }   
}

This code simply reads the first five lines and then outputs them in reverse order.

半暖夏伤 2024-11-03 14:24:37

读入数组并对其调用反向函数或递减循环。

Read into array and call a reverse function on it or decremented loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文