Buffered Writer:将ArrayList写入文件

时间:2014-05-15 14:35:38

标签: java arraylist writer

我们要将ArrayList的内容(经度和纬度的图像)写入文件(.csv或txt,无所谓)。
我们知道bw.write()有问题,但我们没有得到如何管理它。有人有想法吗?

这是我们的代码:

/**
 * Test if stadium contains image 
 */
public void RemStaImSB(){
    Mbb mbb = new Mbb(1,new Point (1,-0.192423,51.480788),new Point(2,-0.189537,51.482646));
    int i = 0;
    imgOut = new ArrayList<Image>();
    for(Image im: this.img){

        if(mbb.piMbb(im)==false){
            imgOut.add(im);
        }
        i++;
    }
    System.out.println("Reporting the images "+i); }


/**
 * Test if stadium contains image 
 */    
public void main() throws Exception{
//Declarations
File outputFile;
BufferedWriter bw;
//Create outputFile object
outputFile = new File ("Chelsea.txt");
//Create stream - file is created
bw= new BufferedWriter(new FileWriter(outputFile));

//Send data thorugh file writer
bw.write(imgOut.lat & imgOut.lon);
//Close the writer
bw.close();

}

2 个答案:

答案 0 :(得分:2)

在这一行:

bw.write(imgOut.lat & imgOut.lon);

您正在编写纬度和经度的按位AND运算结果。您应该单独编写值:

bw.write(imgOut.lat);
bw.write(imgOut.lon);

但是这会将数据写为单个字符。相反,如果您打印数据的String表示会更好。由于您正在写入csv,因此可以用逗号分隔数据:

bw.write(imgOut.lat + "," + imgOut.lon);

答案 1 :(得分:0)

如果您要写入.csv文件,则可以执行以下操作:

bw.write(imgOut.lat + "," + imgOut.lon);