[英]Need help writing to input/output file in java
java新手,需要一些幫助。 我正在處理2個文件。 一個定義了一個類及其所有方法,另一個構造了該類的實例。 我的項目要求我們將類方法的輸出寫入文件。 我的問題是如何讓我的“類定義”文件中的方法寫入與“main方法”中定義的文件相同的文件? 我目前正在使用System.out.print
,我知道這是不正確的。 //文件#1
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();
}
}
將私有PrintWriter
屬性添加到第一個類,並定義一個setPrintWriter()
方法,在實例化第一個類並創建PrintWriter
之后,從第二個類調用該方法。 之后,您可以更改playInstrument()
以使用它而不是System.out
。
注意:所有.java文件(接口除外)都可以歸類為“類定義文件”,因為Java中的類外部不能存在任何代碼。
因為您輸出的大多數內容看起來像調試語句,所以還有第二種方法可以實現它。 您可以在第一個文件中使用System.setOut(PrintStream printStream),並將其傳遞給PrintStream(將PrintWriter更改為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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.