2017-01-17 47 views
1

我試圖用該文件中的一行文本創建外部文件,然後在Java程序中打印出該行文本。我努力了;但是,我遇到了麻煩。我的代碼不會拋出任何錯誤,它只是不正常工作。從Java中的外部文件中打印文本

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

public class App { 

public static void main(String[] args) { 

    String terminate = "X"; 
    String Question = ""; 
    System.out.println("I am the all-knowing Magic 8 Ball!"); 

    do { 
     Scanner scnr = new Scanner(System.in); 
     System.out.println(""); 
     System.out.println("Ask your question here or enter 'X' to exit:"); 
     Question = scnr.nextLine(); 
     continueGame(Question); 
    } while (terminate != Question); 


     String testData[] = {"test1", "test2", "test3"}; 
     writeFile(testData, "data.txt"); 
     ArrayList<String> fileContents = readFile("data.txt"); 

     if (terminate.equalsIgnoreCase(Question)) { 
     for (String contents : fileContents) { 
      System.out.println(contents); 
     }   
    } 
    } 

public static void continueGame(String Question) { 

    char terminate = 'X'; 
    char condition = Question.charAt(0); 

    if (condition == terminate) 
    { 
     System.out.println(""); 
     System.out.println("Thanks for playing!"); 
     System.exit(0); 
    } 
    try 
    { 
     Random rand = new Random(); 
     int choice; 
     choice = 1 + rand.nextInt(20); 
     responseOptions(choice, Question); 
    } 
    catch (Exception e) 
    { 
     System.out.println("Error: Invalid"); 
    } 
} 

public static void responseOptions(int choice, String answer) 
{ 
    switch (choice) 
    { 
     case 1: answer = "Response: It is certain"; break; 
     case 2: answer = "Response: It is decidely so"; break; 
     case 3: answer = "Response: Without a doubt"; break; 
     case 4: answer = "Response: Yes, definitely"; break; 
     case 5: answer = "Response: You may rely on it"; break; 
     case 6: answer = "Response: As I see it, yes"; break; 
     case 7: answer = "Response: Most likely"; break; 
     case 8: answer = "Response: Outlook good"; break; 
     case 9: answer = "Response: Yes"; break; 
     case 10: answer = "Response: Signs point to yes"; break; 
     case 11: answer = "Response: Reply hazy, try again"; break; 
     case 12: answer = "Response: Ask again later"; break; 
     case 13: answer = "Response: Better not tell you now"; break; 
     case 14: answer = "Response: Cannot predict now"; break; 
     case 15: answer = "Response: Concentrate and ask again"; break; 
     case 16: answer = "Response: Don't count on it"; break; 
     case 17: answer = "Response: My reply is no"; break; 
     case 18: answer = "Response: My sources say no"; break; 
     case 19: answer = "Response: Outlook not so good"; break; 
     case 20: answer = "Response: Very doubtful"; break; 
    } 
    System.out.println(""); 
    System.out.println(answer); 
} 

public static void writeFile(String arrayToWrite[], String filename) { 

    try { 
     PrintWriter wordWriter = new PrintWriter("filename.txt"); 
     for (String words : arrayToWrite) { 
      wordWriter.println(words + "\n"); 
     } 
    } catch (Exception e) { 
     String msg = e.getMessage(); 
     System.out.print(msg); 
    } 
} 

public static ArrayList<String> readFile(String filename) { 
    ArrayList<String> fileContents = new ArrayList(); 
    File myFile = new File(filename); 

    try { 
     Scanner scnr = new Scanner(myFile); 
     String tempStr = ""; 
     while (scnr.hasNextLine()) { 
      tempStr = scnr.nextLine(); 
      fileContents.add(tempStr); 
     } 
    } catch (Exception e) { 
     String msg = e.getMessage(); 
     System.out.println(msg); 
    } 
    return fileContents; 
} 
} 
+0

您是否嘗試在寫完後關閉'PrintWriter'? – BackSlash

+0

你的意思是不正常工作?文件沒有被創建,或者'writeFile()'沒有被調用?請告訴我們更多。 –

+2

它以何種方式無法正常工作?我看到的一件事是,即使您已經使用'println',您也會爲每個單詞添加額外的\ n。 – 4castle

回答

1

與您的代碼的問題是,雖然你得到的writeFile()filename,但你不能讓一個具有名稱文件filename,而不是你是一個文件,filename.txt。雖然在您的readFile()中您正在讀取的文件filename尚未創建,但new file(filename)將創建一個空文件,即文件不存在。現在你正在閱讀一個空文件。把下面的代碼在你writeFile()

try{ 
    FileWriter fw=new FileWriter(filename); 
    for (String words : arrayToWrite) { 
     fw.write(words + "\n"); 
    } 
    fw.close(); 

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

對於額外的注意事項儘量不要使用PrintWriterPrintStreamWriter,他們通過錯誤,將導致你的問題沒有。

+0

由於某種原因,該文件仍然沒有顯示在我的程序文件夾 –

+0

相同的代碼在這裏工作@coding_insanity,希望你已經導入所需的。你能告訴我你的'filename'變量是什麼嗎? –

+0

它現在工作,我只是基本上刷新它。感謝您的幫助,我真的很感激它,是我的代碼來顯示文件包含錯誤,因爲這似乎並不工作 –