我现在想把一个文件从hdfs复制到本地计算机上。我已经通过fileinputstream和fileoutputstream完成了大部分工作。但后来我遇到了以下问题。
java i/o异常。mkdirs无法创建文件
我做了一些研究,发现在使用filesystem.create()时(hadoop函数)
https://hadoop.apache.org/docs/current/api/org/apache/hadoop/fs/filesystem.html#create(org.apache.hadoop.fs.path,%20org.apache.hadoop.util.progressible)
原因如下:
如果我将路径设置为不存在的文件夹,则会创建一个文件夹,并且我下载的文件就在其中。
如果我将路径设置为现有文件夹(例如当前目录),则会出现上述i/o异常。
假设我已经获得了正确的路径和fileinputstream,我应该使用什么(在文件系统库中更好)来解决这个问题
我的代码
//src and dst are the path input and output
Configuration conf = new Configuration();
FileSystem inFS = FileSystem.get(URI.create(src), conf);
FileSystem outFS = FileSystem.get(URI.create(dst), conf);
FSDataInputStream in = null;
FSDataOutputStream out = null;
in = inFS.open(new Path(src));
out = outFS.create(new Path(dst),
new Progressable() {
/*
* Print a dot whenever 64 KB of data has been written to
* the datanode pipeline.
*/
public void progress() {
System.out.print(".");
}
});
1条答案
按热度按时间lvmkulzt1#
在“file”类中,有一个名为createnewfile()的方法,它只在不存在新文件时创建新文件。