@Override public void run() { try { RandomAccessFile raf; String dateTimeString = Utilities.GetIsoDateTime(new Date(loc.getTime())); String placemarkHead = "<Placemark>\n<gx:Track>\n"; String placemarkTail = "</gx:Track>\n</Placemark></Document></kml>\n"; synchronized (Kml22FileLogger.lock) { if (!kmlFile.exists()) { kmlFile.createNewFile(); FileOutputStream initialWriter = new FileOutputStream(kmlFile, true); BufferedOutputStream initialOutput = new BufferedOutputStream(initialWriter); StringBuilder initialXml = new StringBuilder(); initialXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); initialXml.append("<kml xmlns=\"http://www.opengis.net/kml/2.2\" "); initialXml.append("xmlns:gx=\"http://www.google.com/kml/ext/2.2\" "); initialXml.append("xmlns:kml=\"http://www.opengis.net/kml/2.2\" "); initialXml.append("xmlns:atom=\"http://www.w3.org/2005/Atom\">"); initialXml.append("<Document>"); initialXml.append("<name>").append(dateTimeString).append("</name>\n"); initialXml.append("</Document></kml>\n"); initialOutput.write(initialXml.toString().getBytes()); initialOutput.flush(); initialOutput.close(); // New file, so new track segment addNewTrackSegment = true; } if (addNewTrackSegment) { raf = new RandomAccessFile(kmlFile, "rw"); raf.seek(kmlFile.length() - 18); raf.write((placemarkHead + placemarkTail).getBytes()); raf.close(); } StringBuilder coords = new StringBuilder(); coords.append("\n<when>"); coords.append(dateTimeString); coords.append("</when>\n<gx:coord>"); coords.append(String.valueOf(loc.getLongitude())); coords.append(" "); coords.append(String.valueOf(loc.getLatitude())); coords.append(" "); coords.append(String.valueOf(loc.getAltitude())); coords.append("</gx:coord>\n"); coords.append(placemarkTail); raf = new RandomAccessFile(kmlFile, "rw"); raf.seek(kmlFile.length() - 42); raf.write(coords.toString().getBytes()); raf.close(); Utilities.LogDebug("Finished writing to KML22 File"); } } catch (Exception e) { Utilities.LogError("Kml22FileLogger.Write", e); } }