create(新建)
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
public class FileCopyShowProgress {
public static void main(String[] args) throws Exception{
String inPath="D:\\file1.txt";//本地路径
String outPath="hdfs://master:9000/input/file1.txt";//hdfs的路径
InputStream in =new BufferedInputStream(new FileInputStream(inPath));
Configuration conf=new Configuration();
//create在写入文件时会自动产生所有的父目录
FileSystem fs=FileSystem.get(URI.create(outPath),conf);
OutputStream out =fs.create(new Path(outPath),new Progressable() {
@Override
public void progress() {
System.out.print(".");
}
});
IOUtils.copyBytes(in, out, 4096,true);
}
}
FSDataOutputStream不允许定位,因为HDFS只允许对一个打开的文件顺序写入,或向一个已有文件添加(换句话说,不支持除尾部的其他位置的写入)。
OutputStream out =fs.create(new Path(outPath),new Progressable() {
//替换为
FSDataOutputStream out =fs.create(new Path(outPath),new Progressable() {