0

Hi Im trying to send data over the network using FileOutputStream. Whenever the FileOutputStream is created using the file linked it deleted all the data found as if it's replacing the old file with a new one.

Can someone please help? Here's the code.

File videoFile = new File (Current_video_file);

log.info(videoFile.toString());

InputStream is = null; 
OutputStream outS = null;

try{
    is = socket.getInputStream();
} catch (IOException e){
    e.printStackTrace();
}

try{
    outS = new FileOutputStream(videoFile);
} catch (IOException e){
    e.printStackTrace();
}

byte [] videoLength = new byte [16*1024];

log.info("About to send");
int count; 
while((count = is.read(videoLength)) > 0){
    log.info("Sending Message");
    outS.write(videoLength, 0, count);
}

log.info("Data Sent");

outS.close();
is.close();
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Liamh101
  • 49
  • 1
  • 10
  • 1
    Hint: you can help yourself; for example by start reading the javadoc for the library classes/methods you intent to use. In other words: you make **assumptions** how this or that should be working. Thing is: reality doesn't care about your assumptions. This is not meant to be rude, but an important lesson for programmers: always **verify** your ideas how some other component really behaves. And especially when it is so well documented, that part is really really easy. – GhostCat Jun 09 '16 at 08:52

2 Answers2

2

The JavaDoc says the following

/**
 * Creates a file output stream to write to the file represented by 
 * the specified <code>File</code> object. If the second argument is
 * <code>true</code>, then bytes will be written to the end of the file
 * rather than the beginning. A new <code>FileDescriptor</code> object is
 * created to represent this file connection.
 * <p>
 * First, if there is a security manager, its <code>checkWrite</code> 
 * method is called with the path represented by the <code>file</code> 
 * argument as its argument.
 * <p>
 * If the file exists but is a directory rather than a regular file, does
 * not exist but cannot be created, or cannot be opened for any other
 * reason then a <code>FileNotFoundException</code> is thrown.
 *
 * @param      file               the file to be opened for writing.
 * @param     append      if <code>true</code>, then bytes will be written
 *                   to the end of the file rather than the beginning
 * @exception  FileNotFoundException  if the file exists but is a directory
 *                   rather than a regular file, does not exist but cannot
 *                   be created, or cannot be opened for any other reason
 * @exception  SecurityException  if a security manager exists and its
 *               <code>checkWrite</code> method denies write access
 *               to the file.
 * @see        java.io.File#getPath()
 * @see        java.lang.SecurityException
 * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
 * @since 1.4
 */
public FileOutputStream(File file, boolean append)

Thus to use it you should add a boolean to the constructor of your FileOutputStream, True to append or False to overwrite

So your code should look like this:

Boolean append = true;
outS = new FileOutputStream(videoFile, append);
Igoranze
  • 1,506
  • 13
  • 35
0

There is a second parameter available for the constructor of FileOutputStream. If true, it will append your data at the end of the file; if false, it will replace the content. By default is false.

So you just need to change the declaration of your FileOutputStream:

outS = new FileOutputStream(videoFile, true); 
Igoranze
  • 1,506
  • 13
  • 35
SCouto
  • 7,808
  • 5
  • 32
  • 49