用反向Java打印行 [英] Printing lines in reverse java

查看:57
本文介绍了用反向Java打印行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决编程集中遇到的问题.

I'm trying to wrap my head around a problem I have in a programming set.

我们应该编写从文件读取并打印出来的代码.我明白了,我能做到.

We're supposed to write code that reads from a file and prints it out. I get that, I can do it.

他要我们做的是将其反向打印.

What he wants us to do is have it print out in reverse.

文件显示为:

abc
123
987

他想要:

987
123
abc

代码如下:

{
    FileReader n=new FileReader("F:\\Java\\Set 8\\output1.txt");
    Scanner in=new Scanner(n);
    int l;
    while (in.hasNext())
    {
        l=in.nextInt();
        System.out.println(l);      
    }
    in.close(); 
}
}

是的,我正在使用java.io. *;和扫描仪.

Yes, I am using java.io.*; and Scanner.

最简单的方法是什么?

编辑编辑编辑

这是经过改进的代码,我尝试将其放入数组中.

Here's the improved code, where I try to put it into an array.

阵列中的数据没有打印出来.

The data in the array isn't printing out.

public static void main(String[] args) throws IOException
{
    int[]Num=new int[20];
    Scanner in=new Scanner(new FileReader("F:\\Java\\Set 8\\output1.txt"));
    int k;
    for (k=0;k<20;k++)
    {
        Num[k]=in.nextInt();
    }
    //in.close();
    for (k=20;k<20;k--)
    {
        System.out.print(+Num[k]);
    }
    //in.close();   
}

推荐答案

如果允许使用第三方API,则Apache Commons IO包含一个 ReversedLinesFileReader 类,该类读取与BufferedReader(最后一行除外).以下是API文档: http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/ReversedLinesFileReader.html

If you are permitted the use of third party APIs, Apache Commons IO contains a class, ReversedLinesFileReader, that reads files similar to a BufferedReader (except last line first). Here is the API documentation: http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/ReversedLinesFileReader.html

注释中提示了另一个(效率较低)的解决方案.您可以将整个文件读取到ArrayList中,然后反转该列表(例如,将其内容压入堆栈,然后将其弹出),然后遍历反转的列表进行打印.

Another (less efficient) solution is hinted at in the comments. You can read your entire file into an ArrayList, reverse that list (e.g. by pushing its contents onto a stack and then popping it off), and then iterate through your reversed list to print.

这是一个粗略的示例:

Here is a crude example:

ArrayList<String> lines = new ArrayList<String>();

Scanner in = new Scanner(new FileReader("input.txt"));
while (in.hasNextLine())
{
    lines.add(in.nextLine());
}

使用 ArrayList 代替静态数组.我们不一定事先知道文件的长度,因此静态数组在这里没有意义. http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Use an ArrayList instead of a static array. We don't necessarily know the length of the file in advance so a static array doesn't make sense here. http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

您的示例输入 123 abc 等包含字符和整数,因此您对 hasNextInt nextInt的调用最终将引发异常.要读取行,请使用 hasNextLine nextLine .这些方法返回 String ,因此我们的ArrayList也需要存储String. http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextLine()

Your example input 123, abc, etc, contains characters as well as ints, so your calls to hasNextInt and nextInt will eventually throw an Exception. To read lines use hasNextLine and nextLine instead. These methods return String and so our ArrayList also needs to store String. http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextLine()

一旦文件位于列表中(如果文件很大,则不是一个很好的解决方案-我们已将整个文件读入内存),我们可以反转列表(如果保持反转的形式有意义),也可以向后迭代.

Once the file is in a list (not a good solution if the file is large - we've read the entire file into memory) we can either reverse the list (if keeping a reversed form around makes sense), or just iterate through it backwards.

for (int i=lines.size()-1; i>=0; i--)  // from n-1 downTo 0
{
    String line = lines.get(i);
    System.out.println( line );
}

这篇关于用反向Java打印行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆