2014-03-06 30 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); 

而且你应该怎么办?

让我知道如果我在这里丢失东西