import java.io.IOException; 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 FileCopyWithProcess { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String localSrc = args[0]; String dst = args[1]; InputStream in = new BufferedInputStream(new FileInputStream(localSrc)); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(dst),conf); try { OutputStream out = fs.create(new Path(dst),new Progressable() { @Override public void progress() { // TODO Auto-generated method stub System.out.println("."); } }); IOUtils.copyBytes(in, out, 4096, true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
上面的程序需要注意的地方是下面这行:
FileSystem fs = FileSystem.get(URI.create(dst),conf);
FileSystem是一个通用的文件系统API,默认有两种取得FileSystem静态工厂实例方式,上面是其中一种,其中通过URI来确定要使用的文件系统类型。。。。。。。。。待续
时间: 2024-11-05 01:02:05