0

So I'm writing a tool for a game for modding, it's working all fine, but I got a problem with writing Strings to a file. The game needs the file encoded in UTF-16LE BOM, so my code looks something like this:

public void save()
{
    FileOutputStream fos = null;
    byte[] buffer = null;
    try {
        fos = new FileOutputStream(new File("data//backup.txt"));
    } catch (FileNotFoundException ex) {

    }
    try
    {

        fos.write((byte)255);
        fos.write((byte)254);
        fos.write("\"lang\"\n".getBytes("UTF-16LE"));
        String s = "";
        for(int i = 1;i<fileContent.size();i++)
        {
            s = fileContent.get(i);
            fos.write(s.getBytes("UTF-16LE"));
            fos.write(System.getProperty("line.separator").getBytes("UTF-16LE"));
        }
        fos.close();
    }
    catch(Exception e){}    
}

fileContent is a ArrayList of Strings, containing NO line separators (I checked this multiple times). So my problem is, that there are written too many line separators to the file. Instead of e.g.:

a
b

I get something like:

a

b

Any ideas, what I'm doing wrong?

  • Did you run the programm on a diferent OS than you visualize the output, eg. runs on Linux and open file from Windows ? – PeterMmm Mar 20 '14 at 14:24
  • No, it all runs on Windows only. – MuesliBowl Mar 20 '14 at 14:25
  • Is the last line empty too or `b` ? – PeterMmm Mar 20 '14 at 14:34
  • The last line is b. The problem is ,that there that empty line between those too. But I still need the possibilty to write empty lines in that file. – MuesliBowl Mar 20 '14 at 14:43
  • Hmmm, what is the output if you put `fos.write(System.getProperty("line.separator").getBytes("UTF-16LE"));` in comments ? – PeterMmm Mar 20 '14 at 14:48
  • How are you viewing the output? Is it actually interpreting the output as the two byte UTF-16? I've run your code on a Mac, printing numbers from 1 to 10, and the results look correct. You might need to swap the order of the first two bytes written, if you have big-endian/little endian problems. Try looking at the result using a binary viewer to see the actual bytes written (od on linux). – Salix alba Mar 20 '14 at 14:55
  • So If this line, the "\"lang\"\n" is written in one line, the rest of the file is written is the next line. To view the file, I'm using Notepad2, and if I change the order of the first two bytes, I cant open the file anymore( notepad chrashes then ). – MuesliBowl Mar 20 '14 at 15:30

0 Answers0