Reading in N lines of the input stream and printing in reverse without using an array structure or type list?

Using the method readLine() BufferedReader, can you print the first N lines of a stream in reverse without using a list or array?

+5
source share
6 answers

I think you can do it through recursion with something like:

void printReversed(int n)
{
   String line = reader.readLine();

   if (n > 0)
     printReversed(n-1);

   System.out.println(line);
}
+2
source

How about recursion to reverse the order?

Pseudocode:

reverse(int linesLeft)
   if (linesLeft == 0)
      return;
   String line = readLine();
   reverse(linesLeft - 1);
   System.out.println(line);
+1
source

. . (1 / ), . .

public class ReversedBufferPrinter {

    class Worker implements Runnable {
           private final CountDownLatch trigger;
           private final CountDownLatch release;
           private final String  line;

           Worker(String line, CountDownLatch release) {
              this.trigger = new CountDownLatch(1);
              this.release = release;
              this.line = line;
           }

           public CountDownLatch getTriggerLatch() {
               return trigger;
           }

           public void run() {
              try {
                  trigger.await();
              } catch (InterruptedException ex) { } // handle 
              work();
              release.countDown();  
           }

           void work() { 
               System.out.println(line);
           }
    } 

    public void reversePrint(BufferedReader reader, int lines) throws IOException {
        CountDownLatch initialLatch = new CountDownLatch(1);
        CountDownLatch triggerLatch = initialLatch; 
        int count=0;
        String line;
        while (count++<lines && (line = reader.readLine())!=null) {
            Worker worker = new Worker(line, triggerLatch);
            triggerLatch = worker.getTriggerLatch();
            new Thread(worker).start();
        }
        triggerLatch.countDown();
        try {
            initialLatch.await();
        } catch (InterruptedException iex) {
            // handle
        }
    }

    public static void main(String [] params) throws Exception {
        if (params.length<2) { 
            System.out.println("usage: ReversedBufferPrinter <file to reverse> <#lines>");
        }
        String filename = params[0];
        int lines = Integer.parseInt(params[1]);
        File file = new File(filename);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        ReversedBufferPrinter printer = new ReversedBufferPrinter();
        printer.reversePrint(reader, lines);
    }
}
+1

, BufferedReader StringBuilder. .

public void reversePrint(BufferedReader bufReader, int lines) throws IOException {
    BufferedReader resultBufferReader = null;
    {
        String line;
        StringBuilder sb = new StringBuilder();
        int count = 0;
        while (count++<lines && (line = bufReader.readLine())!=null) {
            sb.append('\n'); // restore new line marker for BufferedReader to consume.
            sb.append(new StringBuilder(line).reverse());
        }
        resultBufferReader = new BufferedReader(new StringReader(sb.reverse().toString()));
    }
    {           
        String line;
        while ((line = resultBufferReader.readLine())!=null) {
            System.out.println(line);
        }
    }
}
+1

, , inorder . : , , . ( , " " )

, , , / .

0

Prepare each line you read for a line and print the line. If you run out of lines for reading, you simply print what you have.

Alternatively, if you are sure about the number of lines you have and you do not want to use the line:

void printReversed(int n, BufferedReader reader)
{
   LineNumberReader lineReader = new LineNumberReader(reader);

   while (--i >= 0)
   {
      lineReader.setLineNumber(i); 
      System.out.println(lineReader.readLine());    
   }
}
0
source

All Articles