public static void writeToFile(File target, ID3Tag tag)
    {
        byte[] tagData = new ID3Serializer().serialize(tag);
        File tmp = new File(target.getPath() + "_TEMP");
        if (tmp.exists()) {
            throw new Error("Could not create temp file" + tmp.getPath());
        }
        byte[] header = new byte[10];
        try {
            FileInputStream fis = new FileInputStream(target);
            int read = fis.read(header);
            if (read < header.length) {
                throw new Error(String.format("Target file '%s' is shorter " +
                        "than 10 bytes and probably corrupted",
                        target.getPath()));
            }
            FileOutputStream fos = new FileOutputStream(tmp);
            fos.write(tagData);
            if (header[0] == 'I' && header[1] == 'D' && header[2] == '3') {
                int tagLength = getTagLength(header);
                if (fis.skip(tagLength) < tagLength) {
                    throw new Error("failed to skip all of ID3 tag");
                }
            } else {
                fos.write(header);
            }
            byte[] buf = new byte[8192];
            read = fis.read(buf);
            while (read > 0) {
                fos.write(buf, 0, read);
                read = fis.read(buf);
            }
            fos.close();
            fis.close();
            if (!tmp.renameTo(target)){
                throw new Error(String.format("File rename from '%s' to " +
                        "'%s' failed", tmp, target));
            }
        } catch (IOException e) {
            throw new Error(e);
        }
    }