hadoop创建文件,如文件存在则追加内容

public static void createNewFile(String path, String content, FileSystem fs) throws Exception{
    	Configuration conf = new Configuration();
    	conf.set("dfs.support.append", "true");
        Path d_path = new Path(path);
        FSDataOutputStream os = null;
        if(fs.exists(d_path)){
        	try {
        		os = fs.append(d_path);
            	os.write(content.getBytes("UTF-8"));
			} catch (Exception e) {
				// TODO: handle exception
				appendFileContent(path, content, fs);
			}
        	
        }else{
        	os = fs.create(d_path);
            
            os.write(content.getBytes("UTF-8"));
            
        }
        if(os != null){
        	os.close();
        }
    }
    
    public static void appendFileContent(String path, String content, FileSystem fs) throws Exception{
    	Configuration conf = new Configuration();
    	Path d_path = new Path(path);
    	if(fs.exists(d_path)){
    		byte[] bytes = readHDFSFile(path);
    		FSDataOutputStream os = fs.create(d_path);
    		os.write(bytes);
    		os.write("\n".getBytes("UTF-8"));
    		os.write(content.getBytes("UTF-8"));
    		os.close();
    	}else{
    		createNewFile(path, content, fs);
    	}
    }

猜你喜欢

转载自qq346359669.iteye.com/blog/2173493