0

I am using a FileOutputStream to write decimal values to file and since it can only write bytes or byte arrays I find I cannot write 'textual' values like strings.

I have tried various other classes like Writer and PrintWriter as sub-objects but none of them seem to be able to write to the file as I want. I don't want to have to create a new object that will require re-opening the file, it should be possible to do this 'in-line'. Suggestions?

Masked Man
  • 2,176
  • 2
  • 22
  • 41
resetter
  • 5
  • 5
  • What do you mean by inline and that you don't want to create a new object? – Masked Man Aug 06 '16 at 16:18
  • 1
    Can you share the code you tried and highlight why it isn't working as expected? – Mureinik Aug 06 '16 at 16:18
  • I want to be able to output a string to the file at the current position as defined by the FileOutputStream object. Creating another object would require skipping to the desired position in the file and that seems just to clunky. – resetter Aug 06 '16 at 16:20
  • 1
    Use `BufferedWriter` which you can use to write **Strings** – Jude Niroshan Aug 06 '16 at 16:20
  • "java.io.FileOutputStream cannot be converted to java.io.Writer" – resetter Aug 06 '16 at 16:22
  • You can also use PrintWriter. – Masked Man Aug 06 '16 at 16:22
  • I need to be able to convert a FileOutputStream to a class that will allow me to write strings. – resetter Aug 06 '16 at 16:25
  • Creating a "new object" doesn't necessarily require reopening the file and moving the current position. Rather, you can wrap a new `OutputStream` object around your existing `FileOutputStream`. This is how the Java I/O API is designed. – Code-Apprentice Aug 06 '16 at 16:25
  • What other data are you writing to the `FileOutputStream`? It's probably not a good idea to mix binary and text data in the same file. You should either write all the other data as text using a `Writer` instead of an `OutputStream` or write your floating point values as binary. – Code-Apprentice Aug 06 '16 at 16:28
  • Yes, I shouldn't be using FileOuputStream for textual data, trying other classes now, will let you know if it works out. – resetter Aug 06 '16 at 16:45
  • And by the way, you can actually transform the Strings that you want to write into byte arrays (be careful to use the proper encoding tho) – Marcel Aug 06 '16 at 17:23

1 Answers1

0

This should do it i think? It will append it to the End of the File.

public class yourClass
{
    private BufferedWriter bufferedWriter;

    public static void main(String[] args)
    {
        try
        {
            bufferedWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream("file.txt"), StandardCharsets.UTF_8));
        }
        catch (FileNotFoundException e)
        {
            System.out.println("Couldn't initialize the BufferedWriter");
            e.printStackTrace();
            System.exit(0);
        }

        writeStuff("Hey");
        writeStuff("Hey2");
        writeStuff("Hey3");
        writeStuff("Hey4");
        writeStuff("Hey5");
        flushAndClose(false);
        writeStuff("Hey");
        writeStuff("Hey2");
        writeStuff("Hey3");
        flushAndClose(true);
    }

    public void writeStuff(String toWrite)
    {
        bufferedWriter.write(toWrite + System.lineSeparator());
    }

    public void flushAndClose(boolean closeToo)
    {
        bufferedWriter.flush();
        if(closeToo)
        {
            bufferedWriter.close();
        }
    }
}

Everytime you wan't to write something jsut call the writeStuff Method. If this isn't what you wanted, I am sorry i missunderstood you.

Marcel
  • 1,509
  • 1
  • 17
  • 39
  • 1
    This is quite poor code: it never closes the writer, it flushes at each write, making the buffering useless, it doesn't specify a character encoding, it assumes that everything written is a line... – JB Nizet Aug 06 '16 at 17:19
  • Yeah you actually are right. The buffering is useless and it is never closed , but it wasn't suppsoed to ever be closed, isn't that what the op said? the line thing can be changed, that is up to the OP he didn't specify much – Marcel Aug 06 '16 at 17:20
  • It shouldn't be opened and closed every time you write something, but it should definitely be closed when you have finished writing all you wanted to write. – JB Nizet Aug 06 '16 at 17:22