0

I am receiving data from the serial port and I want to write the data to a file. The problem is that the program is overwriting the data at the same place in the file. For example it writes 0xAA and then 0xBD at the same place and when I open the file to read it in text edit then I find only one number in the file. How can I write the numbers one after another? jess

public class Loading {

    public static void writeFile(String fileName,int bytes) {

        FileOutputStream fo = null;
        File file;


        try {

            file = new File("C:/dar.txt");


            fo = new FileOutputStream(file);

            if (!file.exists())
            { 
                file.createNewFile();
            }

            //StringUtils.getHexString((byte) bytes);
           fo.write((byte) bytes);

            System.out.println(getHexString (bytes));
        }

        catch (Exception e) {
            System.out.println("writeFile exception: " + e.getClass().getName()             + " " + e.getMessage());
        }   
        finally {
            if (fo != null) {try {fo.close();} catch (Exception ex) {}}
        }
    } 
Kick Buttowski
  • 6,371
  • 12
  • 34
  • 54
  • Could you be kind and post your expected result and what you get so we can understand your question better? it seems very nuclear what is troubling you – Kick Buttowski Jan 27 '15 at 03:53
  • Checking `File.exists()` and calling `File.createNewFile()` before `new FileOutputStream(...)` is wasteful. Calling it *afterwards* is merely pointless. The file cannot possibly *not* exist at the point you are checking it. Otherwise an exception would have been thrown and you wouldn't have reached the checking/creating code. `new FileOutputStream(...)` *already does* all this. Don't write pointess code. And why is a parameter that only holds a single byte called `bytes`? – user207421 Jan 27 '15 at 04:03
  • my input data looks like 8d bb a1 8d bb a5 ........... I am receiving this data via serial port. In the file that I am tyring to write c:/dar.txt. It looks like a5 nothing else. – user1919855 Jan 27 '15 at 04:59

1 Answers1

2

As stated previously, FileOutputStream has multiple constructors. Use the constructor that takes both a File and a boolean, and set the boolean to true.

FileOutputStream(File file, boolean append)

This will cause it to append to the file, rather than overwriting...

Community
  • 1
  • 1
camerondm9
  • 688
  • 11
  • 21
  • I tired but got the same result – user1919855 Jan 27 '15 at 04:45
  • public class Loading { public static void writeFile(String fileName,int bytes) throws IOException { File file; FileWriter out = null; try { out = new FileWriter("c:/dar.txt"); out.write((byte) bytes); System.out.println(getHexString (bytes)); } catch(IOException ioe) { ioe.printStackTrace(); } finally { if(out != null) out.close(); } } – user1919855 Jan 27 '15 at 04:47
  • @user1919855 You didn't try it at all. Read this answer again, it is correct. Try again. – user207421 Jan 27 '15 at 22:40