现在从下位机传送数据到上位机,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,想法是每次读取数据时就覆盖内容但是变成追加了