FileOutputStream文件写出txt无法覆盖

cai_0927 2019-03-18 10:47:27
现在从下位机传送数据到上位机,FileOutputStream第二个参数选择false,但是每次上传的数据依旧无法覆盖,请问如何解决?

public class test {
public static void main(String[] args) throws IOException {
//创建一个ServerSocket,指定监听的端口
ServerSocket serverSocket = new ServerSocket(8086);
while (true) {
//使用ServerSocket监听用户的请求
Socket socket = serverSocket.accept();
File dest = new File("e:/class/eclipse workplace/1.txt");
//2.选择流
FileOutputStream os = null;
DataInputStream in = null;
DataOutputStream out = null;
try {
os = new FileOutputStream(dest, false);
in = new DataInputStream(socket.getInputStream());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = in.read(buffer)) != -1) {
//out.write(buffer,0,count);
os.write(buffer, 0, count);
System.out.println("接收结束");
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭网络资源
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (in != null) {
in.close();
}

} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}
}
}

这里是保存的TXT,想法是每次读取数据时就覆盖内容但是变成追加了

...全文
322 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧