写入文件Java的问题

时间:2019-02-19 16:35:03

标签: java

我在写入文件时遇到问题。只要我没有使用Math.round(),我只有在插入50 ~~行后停止写入文件的问题,现在我遇到了printWriter完全不写入文件的问题...

代码: 这是一个创建每行(行)的函数

public void addRow (Run run, int index)
    {
        line[this.index+index]=index+" ";
        line[this.index+index]+=run.percentOfCStates+" ";
        line[this.index+index]+=run.avarageIncome+" ";
        line[this.index+index]+=run.percentOfCACell+" ";
        line[this.index+index]+=run.percentOfLACell+" ";
        line[this.index+index]+=run.percentOfCStatesInCACell+" ";
        line[this.index+index]+=run.percentOfCStatesInLACell+" ";
        line[this.index+index]+=run.percentOfSharingCell+" ";
        line[this.index+index]+=run.percentOfAllCStrategy+" ";
        line[this.index+index]+=run.percentOfPcStrategy+" ";
        line[this.index+index]+=run.percentOfallDStrategy+" ";
        line[this.index+index]+=run.percentOfkDStrategy+" ";
        line[this.index+index]+=run.avarageHParameter+" ";
        line[this.index+index]+="0.00";
        if(((int)run.avarageEpsParameter)<100)
            line[this.index+index]+="0";
        line[this.index+index]+=Integer.toString((int)run.avarageEpsParameter)+" ";
        line[this.index+index]+=run.avaragePcParameter+" ";
        for (int i=0; i <run.percentOfkD.length-1;i++)
            line[this.index+index]+=run.percentOfkD[i]+" ";
        line[this.index+index]+=run.percentOfkD[7];
        System.out.println(line[this.index+index]);
    }

这是我写入文件的功能

public void writeToFile(String pathToFile) throws FileNotFoundException
        {
            out= new PrintWriter(pathToFile);
            for (int i=0; i<line.length; i++)
                out.println(line[i]);
        }

这是我使用Math.round的函数,当我没有使用它时,它会插入50行之类的数据,并且在某处剪切了数据,并跳过了其余的行:

    public double   percentOfCStates=0;

    public double   avarageIncome=0;

    public double           CounterOfLACell=0;
    public double       CounterOfCACell=0;
    public double       counterOfPcCell=0;

    public double       percentOfCACell=0;
    public double       percentOfLACell=0;

    public double       percentOfCStatesInCACell=0;
    public double       percentOfCStatesInLACell=0;

    public double       percentOfSharingCell=0;
    public double       percentOfAllCStrategy=0;
    public double       percentOfPcStrategy=0;
    public double       percentOfallDStrategy=0;
    public double       percentOfkDStrategy=0;


    public double       avarageHParameter=0;
    public double       avarageEpsParameter=0;
    public double       avaragePcParameter=0;

    public double       []percentOfkD=new double[8];
public void countStatistics()
        {
            int allCells=this.xRange*this.yRange;

            this.percentOfCStates=Math.round((this.percentOfCStates/allCells)*100);
            this.percentOfCStates/=100;
            this.avarageIncome=(Math.round((this.avarageIncome/(this.CounterOfCACell+this.CounterOfLACell))*100))/10;
            this.avarageIncome/=100;
            this.percentOfCACell=(Math.round((this.percentOfCACell/allCells)*100));
            this.percentOfCACell/=100;
            this.percentOfLACell=(Math.round((this.percentOfLACell/allCells)*100));
            this.percentOfLACell/=100;
            this.percentOfCStatesInCACell=(Math.round((this.percentOfCStatesInCACell/allCells)*100));
            this.percentOfCStatesInCACell/=100;
            this.percentOfCStatesInLACell=(Math.round((this.percentOfCStatesInLACell/allCells)*100));
            this.percentOfCStatesInLACell/=100;
            this.percentOfSharingCell=(Math.round((this.percentOfSharingCell/allCells)*100));
            this.percentOfSharingCell/=100;
            this.percentOfAllCStrategy=(Math.round((this.percentOfAllCStrategy/allCells)*100));
            this.percentOfAllCStrategy/=100;
            this.percentOfPcStrategy=(Math.round((this.percentOfPcStrategy/allCells)*100));
            this.percentOfPcStrategy/=100;
            this.percentOfallDStrategy=(Math.round((this.percentOfallDStrategy/allCells)*100));
            this.percentOfallDStrategy/=100;
            this.percentOfkDStrategy=(Math.round((this.percentOfkDStrategy/allCells)*100));
            this.percentOfkDStrategy/=100;
            this.avarageHParameter=(Math.round((this.avarageHParameter/this.CounterOfLACell)*100));
            this.avarageHParameter/=100;
            this.avarageEpsParameter=(Math.round((this.avarageEpsParameter/this.CounterOfLACell)));
            //this.avarageEpsParameter/=100;
            this.avaragePcParameter=(Math.round(this.avaragePcParameter/this.counterOfPcCell)*100);
            this.avaragePcParameter/=100;
            for(int i=0; i<this.percentOfkD.length; i++)
            {
                this.percentOfkD[i]=(Math.round((this.percentOfkD[i]/allCells)*100));
                this.percentOfkD[i]/=100;
            }
        }

您有什么建议吗?

编辑

对象统计信息中包含该writeToFile函数。

try {
    statistics.writeToFile("text.txt");
} catch (FileNotFoundException e) {
    System.out.println("Error");
}

edit2

0 0.42 0.38 0.35 0.35 0.21 0.21 0.35 0.09 0.09 0.09 0.08 8.0 0.00100 1.0 0.04 0.04 0.04 0.04 0.0 0.0 0.0 0.0
1 0.41 0.34 0.21 0.49 0.13 0.29 0.28 0.01 0.03 0.1 0.06 8.0 0.00100 0.0 0.04 0.05 0.05 0.05 0.0 0.0 0.0 0.0
2 0.42 0.36 0.14 0.56 0.09 0.33 0.26 0.01 0.02 0.07 0.05 8.0 0.00100 0.0 0.05 0.05 0.05 0.06 0.0 0.0 0.0 0.0
3 0.42 0.37 0.1 0.59 0.07 0.35 0.26 0.0 0.01 0.04 0.04 8.0 0.00100 0.0 0.05 0.05 0.05 0.06 0.0 0.0 0.0 0.0

1 个答案:

答案 0 :(得分:4)

您忘记关闭印刷机。

由于写入磁盘的速度很慢,但是写入内存的速度很快,因此printwriter(以及管道的其他部分)会保留包含待处理数据的缓冲区。

仅当您调用closeflush时才会刷新这些缓冲区,让引用过期不会刷新它们

现在最简单的解决方案是使用“ try-with-resources”语句,该语句可以在所有情况下为您处理。

try(PrintWriter p = new PrintWriter(pathToFile)) {
    // Use p to write things
}

如果有任何未处理的异常,这还将处理关闭编写器