Need help writing to input/output file in java
New to java and need some help. I am working with 2 files. One defines a class and all of it's methods and another constructs an instance of this class. My project requires that we write the output from the class methods to a file. My question is how do I get the methods in my "class definition" file to write to the same file as that defined in my "main method"? I am currently using System.out.print
which I know is not correct.
//File #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;
}
}
File #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 new开发者_运维百科File = 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();
}
}
Add a private PrintWriter
property to the first class and define a setPrintWriter()
method which you call from the second class after you have both instantiated the first class and created the PrintWriter
. After that you can change playInstrument()
to use it instead of System.out
.
NB: All .java files (with the exception of interfaces) can be categorized as "class definition files", because no code can exist outside a class in Java.
Because most of the things you are outputting look like debugging statements, there is a second way to accomplish it. You can use System.setOut(PrintStream printStream) in the first file, and pass it a PrintStream (change your PrintWriter to a PrintStream, its the same thing for what you are using it for). Then, in your other file, System.out.println() will print to the file instead of standard output.
Using this approach you won't have to change the first file. Essentially this allows you to redirect all output to a file rather than the command line, which is useful for GUI applications.
So in the second file, change:
java.io.PrintWriter output = new java.io.PrintWriter(newFile);
to
System.setOut(new PrintStream(newFile));
and change:
output.println("Your current string number is " + guitar.strings);
...
output.print("Tuning " + guitar.getInstrumentName());
to
System.out.println("Your current string number is " + guitar.strings);
...
System.out.print("Tuning " + guitar.getInstrumentName());
精彩评论