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

最佳答案

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 的构造函数添加一个 boolean 值,True 表示追加,False 表示覆盖

所以你的代码应该是这样的:

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

关于java - FileOutputStream 删除文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37720979/

相关文章:

Java:双重与 float

c# - 在 C# 中发送 SYN 数据包?

c# - C#中的函数Socket.Select()在linux操作系统中使用epoll吗?

python - 我需要发送不止一个 socket.send() 到一个 http 请求

android将日志语句写入sdcard

java - Spring Boot Embedded Kafka无法连接

java - 在 4x6"纸上使用 java 打印 gif

java - 如何在java中压缩动画webp而不将其转换为静态?

java - 当我尝试解压缩时,ZipOutPutStream、ZipEntry 返回 null

java - 无效_授权 : AADSTS50126: Error validating credentials due to invalid username or password