簡體   English   中英

為什么 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 指定數據應傳輸到位置零的文件 位置是絕對的,你打開文件進行追加並不重要,因為你忽略了當前的通道位置。

請注意說明的文檔

position -文件中傳輸開始的位置; 必須是非負數

您的代碼是針對常見任務的非常規方法。 因此,讀者很難理解,遇到錯誤時也很難得到幫助。 由於URL僅提供InputStream支持,因此請堅持使用流並避免使用通道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM