首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FileOutputStream删除文件内容

FileOutputStream删除文件内容
EN

Stack Overflow用户
提问于 2016-06-09 08:44:16
回答 2查看 1.5K关注 0票数 0

嗨,我想用FileOutputStream通过网络发送数据。每当使用链接的文件创建FileOutputStream时,它就删除了发现的所有数据,就好像用新的文件替换旧文件一样。

有人能帮忙吗?这是密码。

代码语言:javascript
运行
复制
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();
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-09 08:53:36

JavaDoc说:

代码语言:javascript
运行
复制
/**
 * 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添加到覆盖

所以您的代码应该如下所示:

代码语言:javascript
运行
复制
Boolean append = true;
outS = new FileOutputStream(videoFile, append);
票数 2
EN

Stack Overflow用户

发布于 2016-06-09 08:52:37

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

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

代码语言:javascript
运行
复制
outS = new FileOutputStream(videoFile, true); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37720979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档