samedi 4 mars 2017

How can I add line number in a txt file in Java?

I want to create a random x,y coordinate generator in java. I find some code and improve it. It gives me 100 random x,y point. That's OK but I want to I can add line number for each x,y coordinate.

My currently output is :

16.75 34.56 45.89 32.1 36.02 10.23

I want this output :

line1 16.75 34.56 line2 45.89 32.1 line3 36.02 10.23

I add my writer code to text file.

public void outputRandomPoints(int n){
ArrayList<Point> points = getRandomPoints(n);
try {
FileWriter write = new FileWriter(OUTPUTFILEPATH);
BufferedWriter writer = new BufferedWriter(write);
for (Point point : points){
point.x = Math.floor(point.x * 100) / 100;
point.y = Math.floor(point.y * 100) / 100;

writer.write(point.x  + " " + point.y);
writer.newLine();

}

writer.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "There's some IO exception");
System.exit(0);
}
System.out.println("Success.");
}

This code write to point.x and point.y succesfull and I need add line number. I couldn't do that. Can you help me please ?




Aucun commentaire:

Enregistrer un commentaire