AMAPstudio

User Tools


The Java training online

Back to the table of contents

Solution: 7. Write the trees in a file

Try to work with the helping elements before looking at the solution…

Helping elements

  • the method might be called write () or writeTrees ()
  • it may be a static method located in Training
  • it takes 2 arguments: (List trees, String fileName)
  • to create the file, use java.io.BufferredWriter
  • you can write: BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
  • a tab can be coded in a char variable: '\t' or in a String: “\t”
  • the header line should start with #
  • in this exercise, z = 0
  • to write a line in the file: out.write (line); out.newLine ();
  • loop on the trees in the given list, cast each element into a SpatializedTree
  • prepare the line by appending parts in a String: String line = “” + t.getId () + tab + …
  • close the file at the end: out.close ();
  • enclose all the method content in a try {} catch () {} clause
  • in case of Exception, write a message with System.out.println (…) then throw the exception back to caller
  • add lines in main() after the previous exercise where you built a list of trees, and write the trees in a file called trees.txt

A possible solution

An example of solution Lines to be added in main() Example of result file in a text editor

Back to the table of contents

solution_of_7._write_the_trees_in_a_file.txt · Last modified: 2021/01/26 15:05 by coligny