Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java编写文件_Java_File Io - Fatal编程技术网

用Java编写文件

用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()

我目前正在编写一个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();
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