2014-03-06 75 views
1

我正在編寫代碼來讀取文本文件,將其轉換爲LinkedList,並以相反的順序寫入另一個文本文件。反向鏈表和寫入文件:Java

我面臨的問題是,轉換文本文件後,我無法扭轉我的列表。在寫作的同時我也遇到了問題。這裏是我的代碼,其次是它的輸出...

package Collections; 
import java.io.*; 
import java.util.*; 

public class Store implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @SuppressWarnings("rawtypes") 
    public static void main(String[] args) throws IOException { 

     BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.print("Enter the File Name:"); 
     String fname1 = br1.readLine(); 
     FileInputStream fr1 = null; 

     // Reading the file 

     try { 
      fr1 = new FileInputStream(fname1); 
     } catch (FileNotFoundException fe) { 
      System.out.println("File Not Found."); 
      return; 
     } 
     BufferedInputStream bin1 = new BufferedInputStream(fr1); 
     BufferedReader br = new BufferedReader(new InputStreamReader(bin1)); 
     LinkedList<String> words = new LinkedList<String>(); 
     FileOutputStream fout1 = new FileOutputStream("ReverseOrder.txt"); 
     ObjectOutputStream oos = new ObjectOutputStream(fout1); 

     // Converting the file into Linked List 

     String theWord; 
     while (true) { 
      theWord = br.readLine(); 
      if (theWord != null && theWord.length() > 1) 
       words.add(theWord); 
      if (theWord == null) 
       break; 
     } 
     String[] data = new String[words.size()]; 
     for (int i = 0; i < words.size(); i++) { 

      data[i] = words.get(i); 

     } 
     System.out.println("Words:" + words); 

     // Reversing the LInkedList... Here I don't know why 
     // Collections.reverse(words) isn't working. I coudn't reverse my list 

      Collections.reverse(words); 

     System.out.println("Words:" + words); 

     // Writing into a file....but here it is not printing in reverse order. 
     // In fact if I tried to print in Regular order but it's printing with 
     // extra characters 

     oos.writeObject(words); 
     oos.close(); 
     bin1.close(); 
    } 
} 

輸出是:

Enter the File Name:replace.txt 
Words:[Suresh is a good boy and He completed his graduation from IIT Guwahati in 2013 But he is a great man ] 
Words:[Suresh is a good boy and He completed his graduation from IIT Guwahati in 2013 But he is a great man ] 

文件中打印輸出是:

¬íSR java.util.LinkedList中)S] J`「xpw t eSuresh是一個好孩子,他在2013年完成了他從IIT Guwahati畢業但他是一個偉大的人xq〜

+0

你能提供你的輸入文件的內容嗎? – peter

+0

Suresh是一個好孩子,他在2013年完成了他從IIT Guwahati畢業但他是一個偉大的人.......這是我的輸入文件的內容 – user3389983

+0

這是全部寫在一行嗎? – peter

回答

0

要顛倒一個LinkedList迭代通過LinkedList將元素推到一個堆棧直到LinkedList的頭部爲空。然後從堆棧中彈出,將每個元素添加回鏈接列表,直到堆棧爲空。

1

您正在循環中運行Collections.reverse(words);。您只需運行一次,然後再次添加自己的單詞即可跳過循環。取而代之的是:

for (int i = words.size() - 1; i > 0; i--) { 

     words.add(words.get(i)); 
     Collections.reverse(words); 
    } 

運行僅此:

Collections.reverse(words); 
+0

我也這樣做了,但輸出中仍然沒有變化...:(....幫助我 – user3389983

+0

你能分享你現在擁有的新代碼嗎?(更新問題) – peter

+0

是的,我已經更新了代碼,但輸出是一樣的......在新代碼中我已經使用了oos .writeObject(單詞)在Collections.reverse(單詞)之前,但打印到文件中的對象是ASCII碼或somthing ....在輸出中查看 – user3389983

0

這看起來錯:

for (int i = words.size() - 1; i > 0; i--) { 
    words.add(words.get(i)); 
    Collections.reverse(words); 
} 

你爲什麼要在一個循環做什麼呢?如果Collections.reverse()反轉列表,所有你需要做的是

Collections.reverse(words); 

而且你應該怎麼辦?

讓我知道如果我在這裏丟失東西