2011-10-12 80 views
0

新手到java和需要一些幫助。我正在處理2個文件。一個定義了一個類及其所有方法,另一個構造了這個類的一個實例。我的項目要求我們將類方法的輸出寫入文件。我的問題是如何讓我的「類定義」文件中的方法寫入與我的「main方法」中定義的文件相同的文件?我目前使用System.out.print,我知道這是不正確的。 //文件#1需要幫助寫入輸入/輸出文件在java

public class JessiahP3 { 
    boolean isPlaying = false; 
    int strings = 1; 
    boolean isTuned = false; 
    public String instrumentName; 


    //is tuned 
    public void tune() { 
     if(isTuned == false) 
      isTuned = true; 
     else 
      isTuned = false; 
    } 

    //instrument name 
    public void setInstrumentName(String insName){ 
     instrumentName = insName; 
    } 

    //get instrument name 
    public String getInstrumentName(){ 
     return instrumentName; 
    } 

    private Boolean getTuned(){ 
     return isTuned; 
    } 

    public void playInstrument(){ 
     isPlaying = true; 
     System.out.println("The instrument is now playing."); 
    } 

    public void stopInstrument(){ 
     isPlaying = false; 
     System.out.print("The instrument has stopped playing."); 
    } 

    public void setString(int newString){ 
     if (newString >= 1 && newString <= 6)  
      strings = newString; 
     else 
      System.out.print("You have exeeded the maximum number of strings."); 
    } 

    public void stringUp(){ 
     if (strings < 10) 
      strings++; 
    } 

    public void stringdown(){ 
     if (strings > 1) 
      strings--; 
    } 

    public String[] getStringNames(String[] stringNames) { 
     for (String i:stringNames){ 
      System.out.println(i); 
     } 
     return stringNames; 
    } 
} 

文件#2

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

public class JessiahP3Test { 

    public static void main(String[] args)throws Exception { 
     //declare new input/ output file 
     java.io.File newFile = new java.io.File("Jessiahp4.txt"); 
     //java.io.File newFile = new java.io.File(args[0]); 

     //check to see if file exist 
     if (newFile.exists()) { 
      //delete existing file 
      newFile.delete(); 
     } 

     //create a file 
     java.io.PrintWriter output = new java.io.PrintWriter(newFile); 

     //instance 1 of Instrument = Guitar  
     JessiahP3 guitar = new JessiahP3(); 
     guitar.setInstrumentName("Guitar"); 
     guitar.setString(3); 
     output.println("Your current string number is " + guitar.strings); 
     String[] stringNames = {"Abe", "Blue", "Dream"}; 
     guitar.getStringNames(stringNames); 
     output.print("Tuning " + guitar.getInstrumentName()); 
     guitar.tune(); 
     guitar.playInstrument(); 
     //output.print("The" + guitar.getInstrumentName() + "is playing"); 
     guitar.stopInstrument(); 


     //close i/o file 
     output.close(); 
    } 
} 

回答

1

私人PrintWriter屬性添加到頭等艙和定義setPrintWriter()方法,你從第二類叫你不僅實例化後頭等艙並創建了PrintWriter。之後,您可以更改playInstrument()以使用它而不是System.out

注意:所有的.java文件(接口除外)都可以歸類爲「類定義文件」,因爲在Java類的類外不存在任何代碼。

+0

你好,你能幫我定義setPrintWriter方法嗎? 'private void setPrintWriter(){java.io.PrintWriter = new java.io.PrintWriter(newFile); }' –

+0

@Jessiah:首先在類的開頭添加屬性:'private java.io.PrintWriter printWriter;'並將方法定義爲'public void setPrintWriter(java.io.PrintWriter pw){this.printWriter = pw ; }'。它不能是私人的,也不能從你的主類中調用。 – Kaivosukeltaja

1

因爲你輸出的東西大部分看起來像調試語句,所以還有第二種方法來完成它。您可以在第一個文件中使用System.setOut(PrintStream printStream),並將其傳遞給PrintStream(將PrintWriter更改爲PrintStream,與您使用PrintStream的方式相同)。然後,在其他文件中,System.out.println()將打印到文件而不是標準輸出。

使用此方法,您不必更改第一個文件。本質上,這允許您將所有輸出重定向到文件而不是命令行,這對GUI應用程序很有用。

在第二個文件

所以,變化:

java.io.PrintWriter output = new java.io.PrintWriter(newFile); 

System.setOut(new PrintStream(newFile)); 

和變化:

output.println("Your current string number is " + guitar.strings); 
... 
output.print("Tuning " + guitar.getInstrumentName()); 

System.out.println("Your current string number is " + guitar.strings); 
... 
System.out.print("Tuning " + guitar.getInstrumentName()); 
+0

這工作完美。謝謝。進入這門課只有8周,所以我們還沒有得到那麼多。 –