无法使用PrintWriter将java写入文本文件

时间:2013-12-11 19:30:53

标签: java output printwriter println

让我先说一下我对java非常新。这是我上课的第八周,我被困在一个项目上。以下是我到目前为止的情况:

import java.io.*;


public class Guitar {
// Initialize variables 
boolean isPlaying;
boolean inTune;
char[] guitStrings = {'D', 'G', 'C', 'A'}; // Guitar strings
int numOfStrings = 4; // Number of strings the guitar has.

public void Guitar(){
isPlaying = false; // Guitar is not playing by default.
inTune = false; // Guitar is not tuned by default.
System.out.println("The guitar is not tuned and is not playing.");
}

public void isPlaying(){
    System.out.println("Your guitar is now playing!");
    isPlaying = true; // Set guitar to playing
}
public void inTune(){
    System.out.println("Your guitar is now tuned!");
    inTune = true; // Set guitar to tuned.
}

public void stopPlaying(){
    isPlaying = false; // Set isPlaying to false.
    System.out.println("Your guitar has finished playing!");
}
public void notes(){
    System.out.println("The guitar has played a total of " + numOfStrings + 
            " strings and they are: " + guitStrings[0] + "," + guitStrings[1] + "," 
            + guitStrings[2] + "," + guitStrings[3]);
    }

public static void main(String[] args) throws IOException{
    java.io.File file = new java.io.File("guitartest.txt");
    if(file.exists()){
        System.out.println("File already exists!");
        System.exit(1);
    }

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

    Guitar[] guit = new Guitar[10];   
    for (int i = 0; i < guit.length; i++){
        guit[i] = new Guitar();
        guit[i].Guitar();
        guit[i].inTune();
        guit[i].isPlaying();
        guit[i].notes();
        guit[i].stopPlaying();
    }
}    
}

这个程序可以完成我需要做的所有事情,但我们在项目上有最后一步。我必须从命令行将其输出到文本文件。我已将最后一段代码更改为:

public static void main(String[] args) throws IOException{
    java.io.File file = new java.io.File("guitartest.txt");
    if (file.exists()){
        System.out.println("This file already exists!");
        System.exit(1);
    }

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

    Guitar[] guit = new Guitar[10];   // Create 10 instruments
    for (int i = 0; i < guit.length; i++){
        output.println(guit[i] = new Guitar());
        output.println(guit[i].Guitar());
        output.println(guit[i].inTune());
        output.println(guit[i].isPlaying());
        output.println(guit[i].notes());
        output.println(guit[i].stopPlaying());
    }
}

这会编译代码,并在控制台中显示我想要的结果,但文本文件guitartest.txt是完全空白的。我不是在寻找有人为我完成这项任务,我只是在寻找你能指出的任何建议或资源。非常感谢你!

4 个答案:

答案 0 :(得分:3)

PrintWriter是缓冲的,这意味着您写入的文本在实际写入文件之前存储在其内部缓冲区中。因此,在完成编写时需要调用close()方法,以便PrintWriter对象将数据写入文件并关闭它。

您也可以调用flush(),如果您希望现在编写数据但又想继续使用PrintWriter对象,这可能很有用。

答案 1 :(得分:1)

完成写作后

output.flush();
output.close();

答案 2 :(得分:1)

接受PrintWriter的{​​{1}}构造函数实现为

File

换句话说,您的输出正在缓冲,需要不时刷新或一次性刷新。

致电

public PrintWriter(File file) throws FileNotFoundException {
    this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
         false);
}

output.flush();

使用完毕后。

答案 3 :(得分:0)

您可能需要在结尾处刷新输出流,即在for方法中的main()循环后

output.flush();
output.close();

为了安全起见,您可以在try/catch块中包含这两个调用:

try {
    output.flush();
    output.close();
} catch (Exception e) {
    e.printStackTrace();
}