2015-06-20 82 views
0

我試圖圍繞我在編程集中遇到的問題進行打包。反向java中的打印行

我們應該編寫從文件讀取並打印出來的代碼。我明白了,我可以做到。

他希望我們做的是將其打印出來。

讀取文件:

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 *。和掃描儀。

這樣做最簡單的方法是什麼?

編輯編輯編輯

這裏的改進的代碼,在那裏我試圖把它變成一個數組。

數組中的數據未打印出來。

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(); 
} 
+4

你爲什麼不將其存儲在一個列表或數組,並打印反向 – Madhan

+1

使用堆棧。 (遞歸的一種方式) – Manikandan

+0

我同意@Madhan,請看這裏:http://stackoverflow.com/questions/10766492/reverse-the-arraylist-in-simplest-way – smoggers

回答

0

嘗試使用org.apache.commons.io.input.ReversedLinesFileReader,它應該做你想要什麼。

0

最簡單的方法是構建一個列表,並從文件中讀取每行添加到列表。完成後,反向打印清單項目。

這是我的問題代碼版本。

public static void main(String[] args) throws FileNotFoundException { 

    FileReader n = new FileReader("/Users/sharish/Data/abc.xml"); 
    Scanner in = new Scanner(n); 

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

    while (in.hasNext()) { 
     lines.add(in.nextLine()); 
    } 

    in.close(); 
    for (int i = lines.size() - 1; i >= 0; i--) { 
     System.out.println(lines.get(i)); 
    } 

} 
1

如果你被允許使用第三方的API,Apache的百科全書IO包含一個類,ReversedLinesFileReader,即(第一除最後一行)讀取類似於一個BufferedReader文件。以下是API文檔:http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/ReversedLinesFileReader.html

另一種(效率較低)解決方案在評論中暗示。您可以將整個文件讀入一個ArrayList中,將該列表逆轉(例如將其內容推入堆棧然後彈出),然後遍歷您的反轉列表進行打印。

編輯: 這裏是一個粗例如:

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

你的榜樣輸入123abc等,包含字符和整型,所以你要hasNextIntnextInt通話將最終拋出異常。要讀取行,請改用hasNextLinenextLine。這些方法返回String,所以我們的ArrayList也需要存儲String。一旦文件在列表中(如果文件很大 - 如果文件很大 - 我們已經將整個文件讀入內存中,這個解決方案不是很好的解決方案),我們可以反轉列表(如果保持反轉形式是有意義的),或者只是向後迭代。

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

你可以讀出你已經在做,但而不是將它們打印(因爲這會爲了打印出來,你需要它周圍的其他方式)的線,將它們添加到一些內存結構像一個列表或一個堆棧,然後在第二個循環中迭代此結構以按需要的順序打印行。

0

使用您的代碼和註釋的答案,這裏是你將如何字符串存儲到ArrayList中,然後打印出來反向(建築離自己的代碼)

{ 
    FileReader n=new FileReader("F:\\Java\\Set 8\\output1.txt"); 
    Scanner in=new Scanner(n); 
    int l; 
    ArrayList<String> reversed = new ArrayList<String>(); // creating a new String arraylist 
    while (in.hasNext()) 
    { 
     l=in.nextInt(); 
     System.out.println(l);  
     reversed.add(l); // storing the strings into the reversed arraylist 
    } 
    in.close(); 
    // for loop to print out the arraylist in reversed order 
    for (int i = reversed.size() - 1; i >= 0; i--) { 
     System.out.println(reversed.get(i)); 
    } 
} 
} 
1

用一個例子堆棧。

public static void displayReverse() throws FileNotFoundException { 
     FileReader n=new FileReader("C:\\Users\\User\\Documents\\file.txt"); 
     Scanner in=new Scanner(n); 
     Stack<String> st = new Stack<String>(); 
     while (in.hasNext()) { 
      st.push(in.nextLine());  
     } 
     in.close(); 

     while(!st.isEmpty()) { 
      System.out.println(st.pop()); 
     } 
    } 
0

使用Java 8:

try(PrintWriter out = 
     new PrintWriter(Files.newBufferedWriter(Paths.get("out.txt")))) { 
    Files.lines(Paths.get("in.txt")) 
     .collect(Collectors.toCollection(LinkedList::new)) 
     .descendingIterator() 
     .forEachRemaining(out::println); 
}