remotepath != null 与 !TextUtils.isEmpty(remotepath) 的区别

remotepath != null   与 !TextUtils.isEmpty(remotepath) 的区别


 !TextUtils.isEmpty(remotepath)    与   remotepath != null &&remotepath.length > 0   一样


或者初始化 remotepath = null,这时只判断 remotepath != null 也可以,如果初始化 remotepath = “” ,这时必须 用!TextUtils.isEmpty(remotepath) 判断


可以单独写个java main 方法检验


public class Test {

private static String str = "";
/**
* @param args
*/
public static void main(String[] args) {
setText();
}
public static void setText(){  
       if(str != null)  
           System.err.println("daying");
   } 
}

import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; /*************** Begin ***************/ public class HDFSApi { /** * 判断路径是否存在 */ public static boolean test(Configuration conf, String path) throws IOException { FileSystem fs = FileSystem.get( ); return fs.exists( ); } /** * 复制文件到指定路径 * 若路径已存在,则进行覆盖 */ public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException { FileSystem fs = FileSystem.get( ); Path localPath = new Path( ); Path remotePath = new Path( ); /* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆盖 */ fs.copyFromLocalFile( ); fs.close(); } /** * 追加文件内容 */ public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException { FileSystem fs = FileSystem.get( ); Path remotePath = new Path( ); /* 创建一个文件读入流 */ FileInputStream in = new FileInputStream( ); /* 创建一个文件输出流,输出的内容将追加到文件末尾 */ FSDataOutputStream out = fs.append( ); /* 读写文件内容 */ } /** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set( ); String localFilePath = " "; // 本地路径 String remoteFilePath = ""; // HDFS路径 String choice = ""; // 若文件存在则追加到文件末尾 try { /* 判断文件是否存在 */ Boolean fileExists = false; if (HDFSApi.test(conf, remoteFilePath)) { fileExists = true; System.out.println(remoteFilePath + " 已存在."); } else { System.out.println(remoteFilePath + " 不存在."); } /* 进行处理 */ if ( !fileExists) { // 文件不存在,则上传 HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); System.out.println(localFilePath + " 已上传至 " + remoteFilePath); } else if ( choice.equals("overwrite") ) { // 选择覆盖 HDFSApi.copyFromLocalFile(conf, localFil
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值