从Java生成的CSV文件添加了不需要的问号(?)

时间:2014-01-28 04:51:54

标签: java csv java-io bufferedwriter stringbuffer

我正在用Java代码创建一个CSV文件。 (我还生成了HTMLtxt个相同的文件。问题仍然存在。)

代码如下。

private String writeToFile(StringBuffer csvData, long randomDigits, String type) {
    String fileName = "";
    String filePath = "";
    if ("CSV".equals(type)) {
        filePath = WebConstants.PATH + randomDigits + "CSV.csv";
        fileName = randomDigits + "CSV.csv";
    }
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
        bw.write(csvData.toString());
        bw.flush();
        bw.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return fileName;
}

现在,生成文件时,所有数据看起来都是正确的。 但是在文件末尾,有一个不需要的问号(?)。 我调试了代码,当调用bw.write(csvData.toString())时,csvData的值最后不包含问号。

这就是我创建StringBuffer csvData

的方式
private StringBuffer saveAsCVSFile(Vector<XYZVO> list) {

    StringBuffer csvData = new StringBuffer();
    XYZVO VO = null;
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            VO = list.get(i);
            csvData.append(VO.getABC()+",");
            csvData.append(VO.getDEF()+",");
            csvData.append(VO.getGHI()+",");
            csvData.append(VO.getJKL()+",");
            csvData.append(VO.getMNO()+"\n");
        }
    }
    return csvData;
}

此处,XYZVO包含所有Stringint个变量。

我还添加了一个代码来读取生成的文件,如下所示。

private void checkFileData(String filePath) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String line = null;
            while((line = br.readLine()) != null) {
                System.out.println("Line X: "+line);
            }
        } catch(FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

当我在控制台中检查此读取代码的输出时,它最终没有显示问号。

如果有人有任何想法,请告知。

2 个答案:

答案 0 :(得分:0)

当您关闭作者时,它将首先被刷新。因此,收盘前的冲洗是没有必要的。尝试删除它。

答案 1 :(得分:0)

我使用FileWriter来实现相同的目标:

try
    {
    FileWriter writer=new FileWriter(csv);
    writer.append("Mobile Number");
    writer.append(',');
    writer.append("Message");
    writer.append(',');
    writer.append("Service");
    writer.append(',');
    writer.append("Time");
    writer.append('\n');


    writer.append(mobileNumber);
    writer.append(',');
    writer.append(message);
    writer.append(',');
    writer.append(service);
    writer.append(',');
    writer.append(time);
    writer.append('\n');

    writer.flush();
    writer.close();
}
catch(Exception e){
    //catch Exception here
}

使用您的代码

我使用了你的代码,但它在我的工作结束.......

public static void main(String[] args) throws URISyntaxException, UnknownHostException, ClassNotFoundException{
        Test a = new Test();
        Vector<MyObject> testObj =  new Vector<MyObject>();
        testObj.add(new MyObject());
        testObj.add(new MyObject());
        testObj.add(new MyObject());
        a.checkFileData(a.writeToFile(a.saveAsCVSFile(testObj),123456789 , "CSV"));

    }


    private StringBuffer saveAsCVSFile(Vector<MyObject> list) {

        StringBuffer csvData = new StringBuffer();
        MyObject VO = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                VO = list.get(i);
                csvData.append(VO.getABC()+",");
                csvData.append(VO.getDEF()+",");
                csvData.append(VO.getGHI()+",");
                csvData.append(VO.getJKL()+",");
                csvData.append(VO.getMNO()+"\n");
            }
        }
        return csvData;
    }

    private void checkFileData(String filePath) {
        try {

            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String line = null;
            while((line = br.readLine()) != null) {
                System.out.println("Line X: "+line);
            }
            br.close();
        } catch(FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



    private String writeToFile(StringBuffer csvData, long randomDigits, String type) {
        String fileName = "";
        String filePath = "";
        if ("CSV".equals(type)) {
            filePath = randomDigits + "CSV.csv";
            fileName = randomDigits + "CSV.csv";
        }
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
            bw.write(csvData.toString());
            bw.flush();
            bw.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return fileName;
    }


class MyObject{
    static int count =1;

    public String getABC() {
        return "ABC"+(++count);
    }

    public String getMNO() {
        return "MNO"+count;
    }

    public String getJKL() {
        return "JKL"+count;
    }

    public String getGHI() {
        return "GHI"+count;
    }

    public String getDEF() {
        return "DEF"+count;
    }
}

输出

Line X: ABC2,DEF2,GHI2,JKL2,MNO2
Line X: ABC3,DEF3,GHI3,JKL3,MNO3
Line X: ABC4,DEF4,GHI4,JKL4,MNO4