美好的一天。我试图让下面的代码写入一个txt文件。然而,我无法理解。它只是在输出部分的(“”)内打印代码。我的目标是创建文本文件以存储输出数据以便稍后检索。
public class Charlesshaw3Test {
public static void main(String[] args) throws Exception{
**//checking id command line argument provided
if(args.length==0) {
System.out.println("C:\\Users\\bryon\\Desktop\\Java Programming");
System.exit(1);
}
String fileName = args[0];
BufferedWriter out = null;
try {
FileWriter fstream = new FileWriter(fileName);
out = new BufferedWriter(fstream);
}
catch (IOException ex) {
System.out.println("\nCould not create file: "+ fileName+".");
System.exit(1);
}**
//a) 10 Instances of the violin testing.
CharlesShaw3Tst voilin[] = new CharlesShaw3Tst[10];
for (int i = 0; i < 10; i++) {
out.write("\n\n***Creating new violin object [" + (i + 1) + "]***");
voilin[i] = new CharlesShaw3Tst();
out.write("\nCreated");
//b) tune your instruments,
out.write("\nchecking if tuned?");
out.write("\nVoilin tuned? " + voilin[i].isTuned());
out.write("\nTuning...");
voilin[i].setTuned(true);
out.write("\nVoilin tuned? " + voilin[i].isTuned());
//c) Start playing your instrument,
out.write("\nVoilin playing? " + voilin[i].isPlaying());
out.write("\nVoilin tuned? " + voilin[i].isTuned());
out.write("\nCalling playVoilin method");
voilin[i].playViolin();
out.write("\nVoilin playing? " + voilin[i].isPlaying());
// d) Call your unique method, and
int num = voilin[i].getNumString();
out.write("\nNumber of strings: " + num);
out.write("\nString name: ");
String strings[] = voilin[i].getviolinStrings();
for (int s = 0; s < num; s++) {
out.write(strings[s] + " ");
}
out.write("\n");
//e) Stop playing your instruments.
out.write("\nStopping playing..");
voilin[i].stopViolin();
out.write("\nVoilin playing? " + voilin[i].isPlaying());
out.write("\nStopping tuning..");
voilin[i].stopTuneViolin();
out.write("\nVoilin tuned? " + voilin[i].isTuned());
}
//Close the output stream
out.close();
}
}
答案 0 :(得分:0)
尝试使用FileWriter
的附加方法:
try {
FileWriter writer = new FileWriter(file);
writer.append("Your string");
//Append all strings
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
它所指的前一个程序是:
公共课CharlesShaw3Tst {
private final int numString = 5; // number of strings
private final String violinStrings[] = {"E", "B", "G", "D", "A"}; //an array of strings
//fields to determine if the instrument is tuned,
private boolean tuned;
//and if the instrument is currently playing.
私人布尔播放; //将调优和当前播放字段设置为false的构造方法。
public CharlesShaw3Tst() {
this.tuned = false;
this.playing = false;
}
// Other methods
public boolean isPlaying() {
return playing;
}
public void setPlaying(boolean playing) {
this.playing = playing;
}
public boolean isTuned() {
return tuned;
}
public void setTuned(boolean isTuned) {
this.tuned = isTuned;
}
public void playViolin() {
System.out.println("The violin is now playing.");
playing = true;
}
public void stopViolin() {
System.out.println("The violin is now playing.");
playing = true;
}
public void tuneViolin() {
System.out.println("The violin is tuned.");
tuned = true;
}
public void stopTuneViolin() {
System.out.println("The violin is tuned.");
tuned = false;
}
public int getNumString() {
return this.numString ;
}
public String[] getviolinStrings() {
return violinStrings;
}
}