Adrian K.
Adrian K.

Reputation: 33

Java: BufferdWriter prints String into two lines for no reason?

I am currently writing a "text check" program in Java, but somehow I got stuck whilst creating an unique identifier for every file.

Actually I create an new identifier like this:

String identifier = Base64.encode((dateFormat.format(date) + "#" + uuid.toString() + "#" + name+".sc0").getBytes()).replace("=", "");

Also my program creates a new file and opens a BufferedWriter. Actually when I now try to append (I tried using BufferedWriter#write, too, but it didn't work either.)

If I write this String into the file now, it looks like this:

BlMjAxNi8wMy8zMSAyMDo0MjowOSMzMThhYjRkNS0yNjFhLTQwNjItODkyOS03NzlkZDIyOWY4Nj

dGVzdC5zYzA

but it should be in only one line like this:

BlMjAxNi8wMy8zMSAyMDo0MjowOSMzMThhYjRkNS0yNjFhLTQwNjItODkyOS03NzlkZDIyOWY4NjdGVzdC5zYzA

At first I thought that it would probably have a problem with me creating a new line after using BufferedWriter#write, so I tried flushing my BufferedWriter before creating a new line. It didn't work either...

PS: The whole neccessary code:

    String name = file.getName().substring(0, ind);
    File next = new File(folder.getAbsolutePath(), name+".sc0");
    String identifier = Base64.encode((dateFormat.format(date) + "#" + uuid.toString() + "#" + name+".sc0").getBytes()).replace("=", "");

    try {
        next.delete();
        next.createNewFile();

        BufferedWriter writer = new BufferedWriter(new FileWriter(next));
        logger.info("Adding compiler identifier to file ...");
        writer.write("#Script0:"+identifier);

        writer.flush();
        writer.newLine();

        for(String str : lines) {
            writer.newLine();
            writer.append(str);
        }

        writer.flush();
        writer.close();

    } catch (IOException e) {
        logger.error("Strange bug ... Did you delete the file? Please try again!");
        return;
    }

Upvotes: 2

Views: 49

Answers (1)

user207421
user207421

Reputation: 310893

It's the encoder, not the BufferedWriter. Base-64 encoding uses a line length of (I believe) 72 characters.

Upvotes: 0

Related Questions