Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FileOutputStream删除文件的内容_Java_Sockets_Fileoutputstream - Fatal编程技术网

Java FileOutputStream删除文件的内容

Java FileOutputStream删除文件的内容,java,sockets,fileoutputstream,Java,Sockets,Fileoutputstream,嗨,我正在尝试使用FileOutputStream通过网络发送数据。无论何时使用链接的文件创建FileOutputStream,它都会删除找到的所有数据,就好像用新文件替换旧文件一样 有人能帮忙吗?这是密码 File videoFile = new File (Current_video_file); log.info(videoFile.toString()); InputStream is = null; OutputStream outS = null; try{ is =

嗨,我正在尝试使用FileOutputStream通过网络发送数据。无论何时使用链接的文件创建FileOutputStream,它都会删除找到的所有数据,就好像用新文件替换旧文件一样

有人能帮忙吗?这是密码

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();

FileOutputStream的构造函数还有第二个参数可用。如果为true,它将在文件末尾附加您的数据;如果为false,它将替换内容。默认情况下为false

因此,您只需更改FileOutputStream的声明:

outS = new FileOutputStream(videoFile, true); 

FileOutputStream的构造函数还有第二个参数可用。如果为true,它将在文件末尾附加您的数据;如果为false,它将替换内容。默认情况下为false

因此,您只需更改FileOutputStream的声明:

outS = new FileOutputStream(videoFile, true); 

JavaDoc说:

/**
 * 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)
因此,要使用它,您应该向FileOutputStream的构造函数添加一个布尔值,True表示追加,False表示覆盖

因此,您的代码应该如下所示:

Boolean append = true;
outS = new FileOutputStream(videoFile, append);

JavaDoc说:

/**
 * 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)
因此,要使用它,您应该向FileOutputStream的构造函数添加一个布尔值,True表示追加,False表示覆盖

因此,您的代码应该如下所示:

Boolean append = true;
outS = new FileOutputStream(videoFile, append);

提示:你可以自己动手;例如,开始阅读javadoc以获取您想要使用的库类/方法。换句话说:你假设这个或那个应该如何运作。事实是:现实并不在乎你的假设。这并不意味着粗鲁,但对程序员来说是一个重要的教训:始终验证您的想法以及其他组件的实际行为。特别是当它有很好的文档记录时,这一部分真的很容易;例如,开始阅读javadoc以获取您想要使用的库类/方法。换句话说:你假设这个或那个应该如何运作。事实是:现实并不在乎你的假设。这并不意味着粗鲁,但对程序员来说是一个重要的教训:始终验证您的想法以及其他组件的实际行为。尤其是当它有很好的文档记录时,这一部分真的很容易。