Java—文件相关操作
在学习了File,IO的知识之后,可以做一些小程序来对本地文件进行操作。在这里我列举了几个例子:1. 打印文件夹的结构及内部文件内容 2. 复制文件夹到其他路径 3.文件的合并及分裂
1.树状文件表
只是大体实现类似于操作系统下文件管理器的那种树状表,没有伸缩打开功能,只是显示文件名和所在层次。 从控制台读入文件路径,然后打印出来。之后每深入一个层次,文件或文件夹名前就会加上一个 “-” 每行打印一个文件名。
实现过程: 从控制台读入路径名后,调用printFile()方法,这个方法的两个参数分别是File对象和文件层次,判断当前文件是否是文件夹,如果是,则获取文件夹内所有文件对象到一个File数组中,再进行递归,也就是对他们也调用printFile()方法,这样就能扫描整个文件夹并打印将树状文件表打印出来
public class FileTree {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String f = scan.nextLine();
File file = new File(f);
printFile(file,0);
}
static void printFile(File file,int step){
for(int i = 0; i < step; i++){
System.out.print("-");
}
System.out.println(file.getName());
if(file.isDirectory()){
File[] files= file.listFiles();
for(File temp : files){
printFile(temp,step+1);
}
}
}
}
2. 复制文件夹
实现将指定文件夹复制到指定路径处(不能是这个文件夹或其子文件夹的内部)。
实现过程:在控制台读入相应的文件夹和目标目录后,先在目标创建一个与原文件夹同名的文件夹,然后调用copyDirDetail(File resour,File gol)方法:首先判断 resour 是文件还是文件夹,如果是文件,则直接调用copyFile(File resour,File gol)方法,进行文件复制。如果resour是文件夹,在gol处创建文件夹,并获取其中所有的文件到一个File数组中,遍历数组并全部递归调用copyDirDetail(File resour,File gol)方法,获取resour中每个文件的名字,并在gol目录下完成复制。以此类推,完成整个文件夹的复制。
源代码文件:copyDir.java
/**
* 1.递归 查找子孙文件/文件夹
* 2.文件 复制 copyFile
* 文件夹 创建 mkdirs
*/
public class copyDir {
public static void main(String[] args) {
Scanner scan1 = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);
System.out.println("请输入要复制的文件夹:");
String resource = scan1.nextLine();
System.out.println("请输入要被复制到的路径:");
String goal = scan2.nextLine();
try {
copyDir(new File(resource),new File(goal));
} catch (IOException e) {
e.printStackTrace();
System.out.println("拷贝失败");
}
}
/**
* 拷贝文件夹
* @param resour
* @param gol
*/
public static void copyDir(File resour,File gol) throws IOException {
if(resour.isDirectory()){
gol = new File(gol,resour.getName());
}
copyDirDetail(resour,gol);
}
/**
* 拷贝文件夹细节
* @param resour
* @param gol
*/
public static void copyDirDetail(File resour, File gol) throws IOException {
if(resour.isFile()){
copyFile(resour,gol);
} else if(resour.isDirectory()){
gol.mkdirs();
for(File tmp:resour.listFiles()){
copyDirDetail(tmp,new File(gol,tmp.getName()));
}
}
}
public static void copyFile(File resour,File gol)throws FileNotFoundException,IOException{
InputStream is = null;
OutputStream os = null;
is = new BufferedInputStream(new FileInputStream(resour));
byte[] flush = new byte[1024];
int len = 0;
os = new BufferedOutputStream(new FileOutputStream(gol,false));
while( (len = is.read(flush)) != -1){
// System.out.println("Debug");
os.write(flush,0,len);
}
os.flush();
os.close();
is.close();
}
}
3. 文件合并
- 源代码 :
public class merge {
public static void main(String[] args) {
try {
mergeFile("d:\\split","d:\\merge.txt");
System.out.println("Debug");
} catch (IOException e) {
System.out.println("合并失败");
e.printStackTrace();
}
}
public static void mergeFile(String srcpath,String destPath) throws IOException {
File src = new File(srcpath);
File dest = new File(destPath);
if (dest.isDirectory()){
System.out.println("目标文件为文件夹");
return;
}
File[] lists = src.listFiles();
int len = lists.length;
for (int i = 0; i < len; i++){
mergeDetail(lists[i],dest);
}
}
public static void mergeDetail(File srcfile,File dest) throws IOException {
OutputStream os = new BufferedOutputStream(new FileOutputStream(dest,true));
InputStream is = new BufferedInputStream(new FileInputStream(srcfile));
byte[] flush = new byte[1024];
int len = 0;
while(-1 != (len = is.read(flush))){
os.write(flush,0,len);
}
os.flush();
is.close();
os.close();
}
}
4.分裂文件
- 源代码:
public class splitFile {
private String fileName;
private String path;
private long blockSize;
private int size;
private List<String> blockpath;
private long length;
public splitFile() {
this.blockpath = new ArrayList<String>();
}
public splitFile(String path, long blockSize) {
this();
this.path = path;
this.blockSize = blockSize;
init();
}
public void init(){
File file = null;
//健壮性
if(null == path || !(file = new File(path)).exists()){
System.out.println("文件不存在");
return;
}
if (file.isDirectory()){
System.out.println("无法分割文件夹");
return;
}
this.fileName = file.getName();
//计算块数 实际大小 与每块大小
this.length = file.length();
if (this.blockSize > length){
this.blockSize = file.length();
}
size = (int)Math.ceil(length*1.0/this.blockSize);
}
private void initPathName(String destPath){
for(int i = 0; i < size; i++){
this.blockpath.add(destPath+"\\"+fileName+".part"+(i+1));
}
}
/**
*文件分割
* 1.分割块数
* 2.实际大小
* 3.分割文件存放目录
* @param
*/
public void split(String destpath){
long beginPos = 0;
long actualBlockSize = blockSize;
//计算所有块的大小
initPathName(destpath);
for (int i = 0; i < size; i++){
if(i == size - 1){
actualBlockSize = this.length - beginPos;
splitDetail(i,beginPos,actualBlockSize);
}
splitDetail(i,beginPos,actualBlockSize);
beginPos += actualBlockSize;
}
}
public void splitDetail(int index,long beginpos,long actualBlockSize){
//创建源
File src = new File(this.path);
File dest = new File(this.blockpath.get(index));
try (RandomAccessFile raf = new RandomAccessFile(src,"r");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest))){
//读取文件
raf.seek(beginpos);
//缓冲区
byte[] flush = new byte[1024];
int len = 0;
while(-1 != (len = raf.read(flush))) {
if (actualBlockSize - len >= 0) {
bos.write(flush, 0, len);
actualBlockSize -= len;
}else {
bos.write(flush, 0, (int)actualBlockSize);
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
splitFile file = new splitFile("d:\\test.txt",11);
file.split("d:\\split");
}
}