Java:Printwriter的工作方式几乎完美,但插入一些奇怪的字符......为什么会这样?

时间:2016-04-30 07:16:28

标签: java

我只是在编写一个家庭作业程序,我正在使用FileOutputStream将一些字符串写入文件。虽然它在大多数情况下工作,但由于某种原因,程序会在文件的开头插入一些奇怪的字符,文件的其余部分看起来应该是这样。我希望有人可以帮我弄清楚这里发生了什么。

所以这是我在主类中的代码:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ChetSpalsky
 */
public class Writer {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    CarPlate carPlate1 = new CarPlate();
    carPlate1.setNumber("1098463");
    carPlate1.setState("CA");
    carPlate1.setColor("Blue");

    CarPlate carPlate2 = new CarPlate();
    carPlate2.setNumber("6371849");
    carPlate2.setState("MA");
    carPlate2.setColor("Red");

    CarPlate carPlate3 = new CarPlate();
    carPlate3.setNumber("5738402");
    carPlate3.setState("AZ");
    carPlate3.setColor("Green");

    String writeCarPlate1 = (carPlate1.getNumber() + " " + carPlate1.getState() + " " + carPlate1.getColor());
    String writeCarPlate2 = (carPlate2.getNumber() + " " + carPlate2.getState() + " " + carPlate2.getColor());
    String writeCarPlate3 = (carPlate3.getNumber() + " " + carPlate3.getState() + " " + carPlate3.getColor());

    try {
        ObjectOutputStream text = new ObjectOutputStream(new FileOutputStream("dataOutput.txt", false));

        PrintWriter pw = new PrintWriter(text);

        pw.println(writeCarPlate1);
        pw.println(writeCarPlate2);
        pw.println(writeCarPlate3);
        pw.flush();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Writer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Writer.class.getName()).log(Level.SEVERE, null, ex);
    }

}

}

请注意,我知道InputStream导入现在尚未使用,我计划稍后在程序中使用它们,我已经尝试将它们评论出来并再次运行程序,尽管它没有任何差异。

如果它是相关的,这里是定义CarPlate对象的CarPlate类的代码:

public class CarPlate {
    private String number;
    private String state;
    private String color;

    public CarPlate() {

    }

    public CarPlate(String number, String state, String color) {
        this.number = number;
        this.state = state;
        this.color = color;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

最后,这是文本文件的输出:

  

¨Ì w01098463CABlue

     

6371849 MA Red

     

5738402 AZ Green

我只想要文本文件:

  

01098463 CA Blue

     

6371849 MA Red

     

5738402 AZ Green

非常感谢任何帮助,谢谢你们!

2 个答案:

答案 0 :(得分:3)

您正在使用ObjectOutputStream,它将某些信息添加到文件中:

https://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html

只需使用PrintWriter或FileOutputStream写入文件,除非您尝试编写对象...

PrintWriter pw = new PrintWriter(new File("dataoutput.txt"));

答案 1 :(得分:-2)

请勿使用FileOutputStream写入文件而不是尝试使用

BufferedWriter writer = new BufferedWriter( new FileWriter( yourfilename));
writer.write( yourstring);