2015-09-24 65 views
1

我的test.txt在我的積極directory.I創建文件,需要創建三種方法:Java可以不受3種方法

  1. 首先人們必須創建一個輸出文件和反向行的順序。
  2. 第二個單詞的順序
  3. 第三個順序的單詞和單詞。 test.txt包含輸入。

我開發了每種方法來自己工作,但不知何故,當我在同一時間調用所有三個它似乎不工作。 我在做什麼錯? 這是我的主:

import java.io.*; 
    public class DemoReverser { 
    public static void main (String [] args) 
    throws IOException, FileNotFoundException { 
    Reverser r = new Reverser(new File("test.txt")); 
    r.completeReverse(new File("out3.txt")); 
    r.reverseEachLine(new File("out2.txt")); 
    r.reverseLines(new File("out1.txt")); 
} } 

,這是我的課。

import java.util.*; 
import java.io.*; 

public class Reverser { 

Scanner sc = null ; 
Scanner sc2 = null; 

//constructor takes input file and initialize scanner sc pointing at input 
public Reverser(File file)throws FileNotFoundException, IOException{ 

sc = new Scanner (file); 
} 

//this method reverses the order of the lines in the output 
//prints to output file specified in argument. 
public void reverseLines(File outpr)throws FileNotFoundException, IOException{ 

    List<String> wordsarraylist = new ArrayList<String>(); 

    while(sc.hasNextLine()){ 
     wordsarraylist.add(sc.nextLine()); 
     } 

    Collections.reverse(wordsarraylist); 

    FileWriter writer = new FileWriter(outpr,true); 

    for(String str: wordsarraylist) { 
     writer.write(str+System.lineSeparator()); 
     } 
    writer.flush(); 
    writer.close(); 
} 




//this method reverses the order of the words in each line of the input 
//and prints it to output file specified in argument. 
public void reverseEachLine(File outpr)throws FileNotFoundException, IOException{ 

    while(sc.hasNextLine()){ 
     String sentence = sc.nextLine(); 
     String[] words = sentence.split(" "); 
     ArrayList<String> wordsarraylist = new ArrayList<String>(Arrays.asList(words)); 
     Collections.reverse(wordsarraylist); 
     FileWriter writer = new FileWriter(outpr,true); 

     for(String str: wordsarraylist) { 
      writer.write(str + " "); 
      } 

     writer.write(System.lineSeparator()); 
     writer.flush(); 
     writer.close(); 
    } 
} 

//this methhod reverses the order of the words in each sentence of the input 
//then writes it to output file specified in argument 
//then uses the output file as input and reverses the order of the sentences 
//then overwrites the ouput file with the result 
//the output file will contain the input sentences with their words reversed 
// and the order of sentences reversed. 
public void completeReverse(File outpr) throws FileNotFoundException, IOException{ 

while(sc.hasNextLine()){ 
     String sentence = sc.nextLine(); 
     String[] words = sentence.split(" "); 
     ArrayList<String> wordsarraylist2 = new ArrayList<String>(Arrays.asList(words)); 
     Collections.reverse(wordsarraylist2); 
     FileWriter writer = new FileWriter(outpr,true); 

     for(String str: wordsarraylist2) { 
      writer.write(str + " "); 
     } 
     writer.write(System.lineSeparator()); 
     writer.flush(); 
     writer.close(); 
    } 

    sc2 = new Scanner (outpr); 

    List<String> wordsarraylist = new ArrayList<String>(); 

    while(sc2.hasNextLine()){ 
     wordsarraylist.add(sc2.nextLine()); 
    } 

    Collections.reverse(wordsarraylist); 

    PrintWriter erase = new PrintWriter(outpr); 
    erase.print(""); 
    // erase.flush(); 
    erase.close(); 

    FileWriter writer = new FileWriter(outpr,true); 

    for(String str: wordsarraylist) { 
     writer.write(str+System.lineSeparator()); 
    } 
    writer.flush(); 
    writer.close(); 





} 

}

當我運行該程序,創建OUT1文件gests,這是我的第一種方法的輸出文件,但它是空的。我沒有得到第二種方法創建的out2文件,out3很好。 我在做什麼錯?什麼是錯過

+0

改寫,在下一行移動點以增加可讀性。代碼格式框代碼。 – Panther

回答

1

writer.close();之前添加writer.flush();在所有三種方法

而另一件事 - 掃描儀與文件只一次構造初始化。它必須用其他方法重新初始化。

sc = new Scanner(file); //掃描儀應該是在所有三種方法

,捕獲異常可用,使用

try{ 
    // your code 
}catch(Exception err){ 
    err.printStackTrace(); 
} 

運行代碼後,會生成output3.txt(第一個方法調用)。稍後掃描程序不可用,因爲已達到文件結尾。

修復:掃描儀應該重新初始化爲下兩種方法。

編輯:(更新答案與你聊天的反饋)

1)由於你的限制創建三個掃描器SC1,SC2和SC3。我建議在文件正在處理的每種方法中重新初始化掃描器。

2)以更簡單的方式逆轉的字符串不使用StringBuffer的反向()API(用於學習目的)

int length = str.length(); 
    String reverse = ""; 
    for (int i = length - 1 ; i >= 0 ; i--){ 
    reverse = reverse + str.charAt(i); 
    } 
+0

你有沒有例外?你能捕捉並打印異常嗎? –

+0

沒有例外......你能告訴我如何捕獲和打印異常嗎? – popololvic

+0

我無法初始化掃描器,因爲file只是構造函數的參數,我不允許更改它。 – popololvic

0

如果你還沒有寫,你目前正在試圖允許這是可能的。所以你在測試時會出錯。

但是你在代碼中犯了一些錯誤。 reverseEachLine方法不起作用,您不應該浪費代碼來創建completeReverse方法。請注意以下事項。

  • 構建Scanner當你需要它
  • 記得關閉Scanner
  • 寫處理文件關閉Scanner
  • 刪除後追加如果不Filewriter
  • 必要記住刷新FileWriter
  • 識別方法之間的相似性和關係(完整反向是組合o F線反轉和字反轉)

公共類MyReverser {

private File inputFile; 

public MyReverser(File file) { 
    this.inputFile = file; 
} 

public void reverseLines(File outpr) throws FileNotFoundException, IOException { 
    Scanner sc = new java.util.Scanner(inputFile); 
    List<String> wordsarraylist = new ArrayList<String>(); 
    while (sc.hasNextLine()) { 
     wordsarraylist.add(sc.nextLine()); 
    } 
    sc.close(); 

    Collections.reverse(wordsarraylist); 

    FileWriter writer = new FileWriter(outpr, false); 
    for (String str : wordsarraylist) { 
     writer.write(str + System.lineSeparator()); 
    } 
    writer.flush(); 
    writer.close(); 
} 

public void reverseEachLine(File outpr) throws FileNotFoundException, IOException { 
    Scanner sc = new Scanner(inputFile); 

    ArrayList<List<String>> wordsarraylist = new ArrayList<List<String>>(); 
    while (sc.hasNextLine()) { 
     String sentence = sc.nextLine(); 

     List words = Arrays.asList(sentence.split(" ")); 
     Collections.reverse(words); 
     wordsarraylist.add(words); 
    } 

    FileWriter writer = new FileWriter(outpr, false); 
    for (List<String> list : wordsarraylist) { 
     for (String string : list) { 
      writer.append(string + " "); 
     } 
     writer.append(System.lineSeparator()); 
    } 
    writer.flush(); 
    writer.close(); 
} 

public void completeReverse(File outpr) throws FileNotFoundException, IOException { 
    //reverse lines first 
    reverseLines(outpr); 

    //then reverse words 
    reverseEachLine(outpr); 
} 

}

0

的情況在這裏是掃描儀是實例變量,並在你的操作方法之一讀取一次,它贏得當您從Demo類中調用下一個方法時,沒有更多需要閱讀的內容。已做出更改以讀取句子並將其存儲在實例中,以便它可以在每種方法中重用。

import java.util.*; 
import java.io.*; 

public class Reverser { 

    Scanner sc = null; 
    Scanner sc2 = null; 

    boolean hasReadFile; 
    List<String> fileLinesList; 

    // constructor takes input file and initialize scanner sc pointing at input 
    public Reverser(File file) throws FileNotFoundException, IOException { 

     sc = new Scanner(file); 
     hasReadFile = false; 
     fileLinesList = new ArrayList<>(); 
    } 

    // this method reverses the order of the lines in the output 
    // prints to output file specified in argument. 
    public void reverseLines(File outpr) throws FileNotFoundException, 
      IOException { 

     List<String> wordsarraylist = new ArrayList<String>(); 
     readFile(); 
     for (String sentence : fileLinesList) { 
      wordsarraylist.add(sentence); 
     } 

     Collections.reverse(wordsarraylist); 

     FileWriter writer = new FileWriter(outpr, true); 

     for (String str : wordsarraylist) { 
      writer.write(str + System.lineSeparator()); 
     } 
     writer.flush(); 
     writer.close(); 
    } 

    // this method reverses the order of the words in each line of the input 
    // and prints it to output file specified in argument. 
    public void reverseEachLine(File outpr) throws FileNotFoundException, 
      IOException { 
     readFile(); 
     for (String sentence : fileLinesList) { 
      String[] words = sentence.split(" "); 
      ArrayList<String> wordsarraylist = new ArrayList<String>(
        Arrays.asList(words)); 
      Collections.reverse(wordsarraylist); 
      FileWriter writer = new FileWriter(outpr, true); 

      for (String str : wordsarraylist) { 
       writer.write(str + " "); 
      } 

      writer.write(System.lineSeparator()); 
      writer.flush(); 
      writer.close(); 
     } 
    } 

    private void readFile() { 
     if (!hasReadFile) { 
      while (sc.hasNextLine()) { 
       fileLinesList.add(sc.nextLine()); 
      } 
      fileLinesList = Collections.unmodifiableList(fileLinesList); 
      hasReadFile = true; 
     } 
    } 

    // this methhod reverses the order of the words in each sentence of the 
    // input 
    // then writes it to output file specified in argument 
    // then uses the output file as input and reverses the order of the 
    // sentences 
    // then overwrites the ouput file with the result 
    // the output file will contain the input sentences with their words 
    // reversed 
    // and the order of sentences reversed. 
    public void completeReverse(File outpr) throws FileNotFoundException, 
      IOException { 
     readFile(); 
     for (String sentence : fileLinesList) { 
      String[] words = sentence.split(" "); 
      ArrayList<String> wordsarraylist2 = new ArrayList<String>(
        Arrays.asList(words)); 
      Collections.reverse(wordsarraylist2); 
      FileWriter writer = new FileWriter(outpr, true); 

      for (String str : wordsarraylist2) { 
       writer.write(str + " "); 
      } 
      writer.write(System.lineSeparator()); 
      writer.flush(); 
      writer.close(); 
     } 

     sc2 = new Scanner(outpr); 

     List<String> wordsarraylist = new ArrayList<String>(); 

     while (sc2.hasNextLine()) { 
      wordsarraylist.add(sc2.nextLine()); 
     } 

     Collections.reverse(wordsarraylist); 

     PrintWriter erase = new PrintWriter(outpr); 
     erase.print(""); 
     // erase.flush(); 
     erase.close(); 

     FileWriter writer = new FileWriter(outpr, true); 

     for (String str : wordsarraylist) { 
      writer.write(str + System.lineSeparator()); 
     } 
     writer.flush(); 
     writer.close(); 

    } 
} 
相關問題