第一次用自己学的Java解决实际问题,纪念一下!设计i/o,File的知识!


<span style="background-color: rgb(255, 255, 255);">              <span style="font-size:18px;"><strong>程序功能概述:</strong><span style="font-size:14px;">最近在学校档案馆找了一份兼职,主要的工作就是帮他们上传资料,第一天丧尸的馆长给了我三个文件夹每个文件夹下面有一千多个子文件夹,每个子文件夹为一个主题,代表一个主题的子文件下面有几个网页或文档。在传的时候有的需要传............这就不再赘述了。程序功能就是:这里有一个文件路径a,有一个文件路径b,有一个文本文件c.txt。需要把c.txt里边有的主题在文件路径a(肯定有)下面找出来放到空路径b下边。</span></span></span>

                              程序代码介绍:共三个源文件:com.john.file.Copyfile.java

                                                                  com.john.file.Readtxt.java

                                                                  com.john.file.Main.java

 

                                                       Copyfile.java:实现将一个文件夹下面的所有文件复制到另外一个文件下面

                                          Readtxt.java:实现将规范的文本中存放的文件夹名字读出来

                                          Main.java:主函数所在class

            

 

                                代码:

//Copyfile.java
package com.john.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class Copyfile {
	

	public void copyFolder(String oldPath, String newPath) {

		try {
			(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (oldPath.endsWith(File.separator)) {
					temp = new File(oldPath + file[i]);
				} else {
					temp = new File(oldPath + File.separator + file[i]);
				}

				if (temp.isFile()) {
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newPath
							+ "/" + (temp.getName()).toString());
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
				if (temp.isDirectory()) {// 如果是子文件夹
					copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
				}
			}
		} catch (Exception e) {
			System.out.println("复制整个文件夹内容操作出错");
			e.printStackTrace();

		}

	}

	public static void main(String[] args) {
		Copyfile test = new Copyfile();
		test.copyFolder("E:\\from", "F:\\to");
	}
}
//Readtxt.java
package com.john.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Readtxt {
	public ArrayList<String> read(String path){
		FileInputStream fr;
		String name;
		ArrayList<String> names = new ArrayList<String>();
		try {
			fr = new FileInputStream(new File(path));
			BufferedReader br = new BufferedReader(new InputStreamReader(fr,"utf-8"));
			
			if(br.ready()){
				while((name = br.readLine()) != null){
					names.add(name);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return names;
	}
}


 

package com.john.file;
//Main.java

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("please input from path:");
		String frompath = scanner.nextLine();
		System.out.println("please input to path");
		String topath = scanner.nextLine();
		System.out.println("please input the txtpath:");
		String txtpath = scanner.nextLine();
		

		ArrayList<String> names = new Readtxt().read(txtpath);
		for(int i=0;i<names.size();i++){
			new Copyfile().copyFolder(frompath+"\\"+names.get(i),topath+'\\'+names.get(i));
			System.out.println(names.get(i));
		}
	}

}



 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值