hdfs-javaAPI
1.delete
1.1 如果是文件,args1设置是否recursive。
1.2 如果是目录,只能删除最后一级目录和里面的文件
,如果设为true,删除目录失败时,会抛异常。
1.3 delete(Path f)已经deprecated,delete(Path f, boolean recursive)
/** Delete a file.
*
* @param f the path to delete.
* @param recursive if path is a directory and set to
* true, the directory is deleted else throws an exception.In
* case of a file the recursive can be set to either true or false.
* @return true if delete is successful else false.
* @throws IOException
*/
public abstract boolean delete(Path f, boolean recursive) throws IOException;
2.deleteOnExit(Path f)
给一个文件打个标记,等fs关闭,会自动删除,这个文件必须存在,如果删除失败,fs关闭时会返回错误
3.rename
须知:
- 类似于mv,根据目录是否改变改名或更换位置。执行后,源文件就没了
- 目标文件不能存在,目标目录必须存在。一般先用exists和mkdir创建一下。
- 源路径是目录时,目标路径必须为目录,且必须存在。
- 跨文件系统的移动(local到hdfs或者反过来)都是不允许的
- rename时如果dstPath已经有,不会执行rename,不会覆盖,而是返回false
/**
* Renames Path src to Path dst. Can take place on local fs
* or remote DFS.
* @param src path to be renamed
* @param dst new path after rename
* @throws IOException on failure
* @return true if rename is successful
*/
public abstract boolean rename(Path src, Path dst) throws IOException;
/**
* Renames Path src to Path dst
* <ul>
* <li
* <li>Fails if src is a file and dst is a directory.
* <li>Fails if src is a directory and dst is a file.
* <li>Fails if the parent of dst does not exist or is a file.
* </ul>
* <p>
* If OVERWRITE option is not passed as an argument, rename fails
* if the dst already exists.
* <p>
* If OVERWRITE option is passed as an argument, rename overwrites
* the dst if it is a file or an empty directory. Rename fails if dst is
* a non-empty directory.
* <p>
* Note that atomicity of rename is dependent on the file system
* implementation. Please refer to the file system documentation for
* details. This default implementation is non atomic.
* <p>
* This method is deprecated since it is a temporary method added to
* support the transition from FileSystem to FileContext for user
* applications.
*
* @param src path to be renamed
* @param dst new path after rename
* @throws IOException on failure
*/
@Deprecated
protected void rename(final Path src, final Path dst,
final Rename... options)
4.mkdirs
须知:
- mkdirs(Path f) == mkdirs(f, FsPermission.getDirDefault())
- FsPermission是要创建的文件的权限
- mkdirs相当于mkdir -p,会创建多级目录
合并文件
一行一行的操作
BufferedReader
BufferedWriter
PrintWriter