2016-04-30 25 views
-1

嗨我需要從文件中刪除重複項並按相反順序打印該文件,例如「Ram is Ram is the Teacher」,則輸出應爲「Teacher the是Ram「。從文件中刪除重複文件,並用反向語句打印不使用java的文字

public class BufferedReaderExample { 

     public static void main(String[] args) { 

      BufferedReader br = null; 

      try { 

       String sCurrentLine; 

       br = new BufferedReader(new FileReader("C:\\testing.txt")); 

       while ((sCurrentLine = br.readLine()) != null) { 
        System.out.println(sCurrentLine); 
       } 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        if (br != null)br.close(); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
      } 

     } 
    } 
+0

你的代碼目前不做任何指望打印每一行。另外,輸出不清楚:如果重複,你想要什麼?保持第一個值?保持另一個? – Tunaki

+0

是的我想第一次發生被刪除,最後一次發生打印例如「這是這個使用這個變量」它應該打印像變量這使用 我沒有做任何事情,除了閱讀,因爲我無法想象邏輯 – user4188607

回答

0
public void readAndPrintInReverseOrder() throws IOException { 
    String path = "test.txt"; 

    BufferedReader br = null; 

    try { 
     br = new BufferedReader(new FileReader(path)); 
     Stack<String> lines = new Stack<String>(); 
     String line = br.readLine(); 
     while(line != null) { 
      lines.push(line); 
      line = br.readLine(); 
     } 

     while(! lines.empty()) { 
      System.out.println(lines.pop()); 
     } 

    } finally { 
     if(br != null) { 
      try { 
       br.close(); 
      } catch(IOException e) { 
      } 
     } 
    } 
} 
+0

上面的代碼是打印的方式,如果我的文件中有兩行然後它打印第二行來代替第一和第一代替第二那就是它但我認爲我的問題在最後註釋清楚 – user4188607

+0

請帥哥請幫助我,因爲這是非常關鍵的 – user4188607

+0

任何人都可以回答這個問題,對我來說真的很難! – user4188607

0
package com.himanshu.factorypattern; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 
import java.util.TreeMap; 

public class DuplicateString { 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     String sCurrentLine; 
     BufferedReader br = null; 
     try{ 
      br = new BufferedReader(new FileReader("D:/him.txt")); 
      while((sCurrentLine=br.readLine())!=null) 
      { int c=0; 
      //System.out.println(sCurrentLine); 
       String[] p = sCurrentLine.split(" "); 
       Map<Integer,String> l = new TreeMap<Integer,String>(); 
       for(int i=0;i<p.length;i++){ 
        if(l.containsValue(p[i])){ 

        } 
        else{ 
        l.put(c++,p[i]); 
        //System.out.println(l); 
        } 
       } 

       Set ss = l.keySet(); 
       Iterator it = ss.iterator(); 

       while(it.hasNext()){ 
        //Map.Entry m = (Map.Entry)it.next(); 
        int k= (int)it.next(); 
        System.out.print(l.get(k)+" "); 

       //System.out.println(sCurrentLine); 

      } 
       System.out.println(); 
      } 
     } 
     catch(Exception e){ 

     } 
     finally{ 
      br.close(); 

     } 


     /*String s = "I am the logic the current the current"; 
     String[] p = s.split(" "); 
     Map<Integer,String> l = new TreeMap<Integer,String>(); 
     for(int i=0;i<p.length;i++){ 
      l.put(p[i].hashCode(),p[i]); 
     } 
     Set ss = l.keySet(); 
     Iterator it = ss.iterator(); 

     while(it.hasNext()){ 
      //Map.Entry m = (Map.Entry)it.next(); 
      int k= (int)it.next(); 
      System.out.print(l.get(k)+" "); 

     }*/ 
    } 

} 
+0

嗨我完成了我想要的,但現在的問題是我已經對我的文件進行了一些更改並保存,但我的程序沒有讀取文件我可以知道爲什麼嗎? – user4188607