简体   繁体   English

新创建的文件无法在Java中打开

[英]New Created File Can't Open in Java

My else if block will verify the result from remote service. 我的else if块将验证来自远程服务的结果。

If the result matches, it will trigger another API call to contact the remote service again. 如果结果匹配,它将触发另一个API调用以再次联系远程服务。 The remote service will send back a file to my client program. 远程服务会将文件发送回我的客户端程序。

I tested all my code and it is working but I am not able to open back the new file and it's showing the file was corrupted. 我测试了所有代码,并且可以正常工作,但是无法打开新文件,这表明文件已损坏。 My client program will read the file from the remote service and write it into another file name in a different directory. 我的客户端程序将从远程服务读取文件,并将其写入其他目录中的另一个文件名。

This is my source code: 这是我的源代码:

else if (result == 1 && value.equals("problem"))
{
    String Url = "http://server_name:port/anything/anything/";
    String DURL = Url.concat(iD);
    System.out.println("URL is : " + DURL);  // the remote API URL 
    URL theUrl = new URL (DURL);
    HttpURLConnection con1 = (HttpURLConnection) theUrl.openConnection();  //API call
    con1.setRequestMethod("GET");
    con1.connect();
    int responseCode = con1.getResponseCode();
    if(responseCode == 200)
    {
        try
        {
            InputStream is1 = con1.getInputStream();
            BufferedReader read1 = new BufferedReader (new InputStreamReader(is1));
            String data1 = "" ; 
            while ((data1 = read1.readLine()) != null)
            {
                PrintStream ps = new PrintStream(new FileOutputStream(filePath));
                ps.print(data1);
                ps.close();

            }
            System.out.println("The new sanitized file is ready");
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

This is my filePath mentioned in the code D:/file/red_new.docx . 这是我在代码D:/file/red_new.docx中提到的filePath This is how I get my file path: String filePath = "D:/file/"+fn+"_new."+fileType; 这就是我获取文件路径的方式: String filePath = "D:/file/"+fn+"_new."+fileType; . The fn variable is the filename from the JSON string from the first API call while the fileType is the file type from the JSON string from the second API call. fn变量是来自第一个API调用的JSON字符串的文件名,而fileType是来自第二个API调用的JSON字符串的文件类型。 I add in the _new to indicate it's a new file and using java concatenate with the fn and fileType to obtain the full path. 我在_new添加以指示它是一个新文件,并使用Java与fnfileType以获取完整路径。

You're creating a new output file per line of input, so you'll only ever get the last line. 您将在每行输入中创建一个新的输出文件,因此您只会得到最后一行。 And you're also losing the line terminators. 而且您还会丢失行终止符。 Try this: 尝试这个:

PrintStream ps = new PrintStream(new FileOutputStream(filePath));
while ((data1 = read1.readLine()) != null)
{
    ps.println(data1);
}
ps.close();

You're also not closing the input stream. 您也没有关闭输入流。

If these files aren't all known to be text files you should be using InputStream and OutputStream . 如果不是所有这些文件都是文本文件,则应使用InputStreamOutputStream

Please use finally block to close the open streams. 请使用finally块关闭打开的流。 If stream is not closed, you can't open it until the process holding the stream is closed or released. 如果未关闭流,则在关闭或释放保存该流的进程之前,无法打开它。

eg: 例如:

try( InputStream is1 = con1.getInputStream()){
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM