Reputation: 172
I have problems with writing to file. As long as i have not using Math.round(), i have only problem with stopping writing to file after 50~~ rows inserted into file, now i have problem that printWriter write totally nothing to file...
Code: this is a function that create each line (row)
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]);
}
this is my function that write to file
public void writeToFile(String pathToFile) throws FileNotFoundException
{
out= new PrintWriter(pathToFile);
for (int i=0; i<line.length; i++)
out.println(line[i]);
}
this is my function that using Math.round, when i havent used it, it insert something like 50 rows and data was cutted in some point and rest of rows was skipped:
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;
}
}
have you got any suggestions?
object statistics have that writeToFile function inside.
try {
statistics.writeToFile("text.txt");
} catch (FileNotFoundException e) {
System.out.println("Error");
}
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
Upvotes: 1
Views: 121
Reputation: 18834
You forgot to close your printwriter.
Since writing to disk is slow, but writing to memory is fast, printwriter (and other parts of the pipeline) keep buffers containing pending data.
These buffers are only flushed when you call either close
or flush
, letting the the reference expire does not flush them
The easiest solution now is using a "try-with-resources" statement, that handles this for you in all cases.
try(PrintWriter p = new PrintWriter(pathToFile)) {
// Use p to write things
}
This will also handle closing the writer in the case of any unhandled exceptions
Upvotes: 4