为什么 FileChannel 不附加到文件末尾?

Why doesn't FileChannel append to end of file?

我正在尝试使用 Java 从 REST API 下载几个不同的文件。

到目前为止,我正在获取文件,但内容不会附加到输出文件的末尾。

我将 FileOutputStream 构造函数从 new FileOutputStream(path) 更改为 new FileOutputStream(path, true) 但不知何故它不起作用。

有人可以指点我所缺少的吗?

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class GetXML {

 
    // This Method Is Used To Download A Sample File From The Url
    private static void downloadFileFromUrlUsingNio() {

        String filePath ="config/sample.txt";
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the NO which you want to parse: ");       
        
        
        while(in.hasNextLine()){
        
        String sampleUrl = "e.g.comSearch?NO=" + in.nextLine();
    
 
        URL urlObj = null;
        ReadableByteChannel rbcObj = null;
        FileOutputStream fOutStream  = null;
 
        // Checking If The File Exists At The Specified Location Or Not
        Path filePathObj = Paths.get(filePath);
        boolean fileExists = Files.exists(filePathObj);
        if(fileExists) {
            try {
                urlObj = new URL(sampleUrl);
                rbcObj = Channels.newChannel(urlObj.openStream());
                
                fOutStream = new FileOutputStream(filePath, true);
 
                fOutStream.getChannel().transferFrom(rbcObj, 0, Long.MAX_VALUE);
                
                System.out.println("! File Successfully Downloaded From The Url !");
            } catch (IOException ioExObj) {
                System.out.println("Problem Occured While Downloading The File= " + ioExObj.getMessage());              
            } finally {
                try {
                    if(fOutStream != null){
                        fOutStream.close();
                        System.out.println("fOutStream closed");
                    }
                    if(rbcObj != null) {
                        rbcObj.close();
                        System.out.println("rbcObj closed");
                    }
                } catch (IOException ioExObj) {
                    System.out.println("Problem Occured While Closing The Object= " + ioExObj.getMessage());
                }               
            }
        } else {
            System.out.println("File Not Present! Please Check!");
        }
        }
        in.close();
        System.out.println("Scanner Closed");
    }
 
    public static void main(String[] args) {
        downloadFileFromUrlUsingNio();
    }
}

您写了:

fOutStream.getChannel().transferFrom(rbcObj, 0, Long.MAX_VALUE);

第二个参数,0,指定数据应该传输到文件位置零。位置是绝对的,与你打开文件无关对于追加,因为您忽略了当前频道位置。

the documentation指出,

position - The position within the file at which the transfer is to begin; must be non-negative

您的代码是完成一项常见任务的非常规方法。这样,读者很难理解,遇到错误也很难得到帮助。由于 URL 仅提供 InputStream 支持,请坚持使用流媒体并避免使用频道。