向HDFS输出文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LegendaryHe/article/details/76818932

private static final String BASE_URL = "hdfs://10.15.0.208:8020";

String writePath = "/tmp/test.txt";

String finalUrl = BASE_URL+writePath;

FileSystem  fs = FileSystem.get(URI.create(finalUrl),conf);

        //要追加的文件流,inpath为文件  
        //InputStream in = new BufferedInputStream(new FileInputStream(finalUrl));
        fs.isDirectory(new Path(finalUrl));
        fs.isFile(new Path(finalUrl));
        if( !fs.isDirectory(new Path(finalUrl))&&!fs.isFile(new Path(finalUrl)))
        {
        System.out.println(finalUrl+"不存在于文件系统中");
        }else
        {
        System.out.println(finalUrl+"已存在");
        }
       
        OutputStream out = fs.append(new Path(finalUrl));  
        
        out.write("testWrite".getBytes());
        out.write("test".getBytes());
        out.write("Write".getBytes());
        //IOUtils.copyBytes(in, out, 4096, true);  
OutputStream pw = fs.create(new Path(finalUrl),false);
pw.write("hello testaaaaaaaaaaaaa".getBytes());
pw.write("bbbsss".getBytes());



System.out.println("写入成功");
pw.flush();

pw.close();


//如果文件不存在,create文件后要先把打开的文件流关闭,在进行append操作,否则会出错。

猜你喜欢

转载自blog.csdn.net/LegendaryHe/article/details/76818932