读取 URL 的前 5 行,然后按相反顺序打印
我想编写一个简单的程序,读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以直接从 URL 读取作为初始示例,因为它已经是一段优雅的代码,可以根据您的需要进行调整。例如...
这段代码只是读取前五行,然后以相反的顺序输出它们。
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 ...
This code simply reads the first five lines and then outputs them in reverse order.
读入数组并对其调用反向函数或递减循环。
Read into array and call a reverse function on it or decremented loop.