用Java编写文件
我目前正在编写一个Web服务器编程,我正在研究HTTP PUT方法。当客户端连接时,他键入如下内容:用Java编写文件,java,file-io,Java,File Io,我目前正在编写一个Web服务器编程,我正在研究HTTP PUT方法。当客户端连接时,他键入如下内容: PUT /newfile.txt HTTP/1.1 Host: myhost BODY This text is what has to be written to the new created file 我希望能够将客户机请求的主体写入创建的文件中 这就是我要做的,它可以工作,但当我按下回车键后,它就停留在那里了 InputStream is = conn.getInputStream()
PUT /newfile.txt HTTP/1.1
Host: myhost
BODY This text is what has to be written to the new created file
我希望能够将客户机请求的主体写入创建的文件中
这就是我要做的,它可以工作,但当我按下回车键后,它就停留在那里了
InputStream is = conn.getInputStream();
OutputStream fos = Files.newOutputStream(file);
int count = 0;
int n = 10;
while (count < n) {
int b = is.read();
if (b == -1) break;
fos.write(b);
++count;
}
fos.close();
conn.close();
InputStream is=conn.getInputStream();
OutputStream fos=Files.newOutputStream(文件);
整数计数=0;
int n=10;
while(计数
您可以试试这个
Scanner sc = new Scanner(is);
扫描仪
将使您能够轻松地将文件读取到字符串
。你可以用它来分开身体。您只需将其作为Scanner
的next
方法的参数提供即可
分离后,需要将该
字符串写入文件。只用
FileWriter writer = new FileWriter(file);
writer.write(bodyContentString);
writer.flush();
writer.close();
祝你好运。它停在哪里?在连接输入流关闭之前,is不会返回-1