Java-Io流简单应用

1.获取文件夹下面所有的.txt文件

 //根据一个案例可以发散思维写
 public class simpleDemo{
 		public static void main(String[] arges){
			//创建文件对象
			File file = new File("C:\\Test\\FileDoc");
			scanFolders(file,".txt");
 		}
	   public static void scanFolders(File file, String extensionName) {
    		// 1. 获取文件目录下的所有子目录文件数组
    		File[] files = file.listFiles();
    		// 2. 循环遍历,获取每一个子文件目录
    		for (File f : files) {
    		// 2.1 判断是否是文件夹
    		if (f.isDirectory()) {
    			// 2.1.1 条件成立,说明是文件夹,进行递归调用
    			scanFolders(f, extensionName);
    		} else {
    			// 2.1.2 条件不成立,说明是文件,进行判断
    			if (f.getName().endsWith(extensionName)) {
    				System.out.println(f.getAbsolutePath());
    		    }
    	    }
   		}
    }
 }

2.关于java web文件下载前台用ajax请求发现浏览器无反应,改为表单提交,至于为什么不能用ajax进行文件下载还不清楚,希望有大佬留言解决下:

		//前台
	    $(document).on('click','#editable-sample button.download', function () {
	    	//需要带的参数,没有就空表单提交
            var reportName = ($(this).data("report_name"));
            var reportPath = ($(this).data("report_path"));
            //动态生成form表单
            var url = "${ctx}/report/downloadReport";
            var form = $("<form></form>").attr("action", url).attr("method", "get");
            form.append($("<input></input>").attr("type", "hidden").attr("name", "rName").attr("value", reportName));
            form.append($("<input></input>").attr("type", "hidden").attr("name", "rPath").attr("value", reportPath));
            form.appendTo('body').submit().remove();
        });
		//后台
	   @RequestMapping("/report/downloadReport")
	   @PostMapping
	   public void downloadReportFile(HttpServletRequest request,HttpServletResponse response) throws Exception{
	       String reportName = request.getParameter("rName");
	       String reportPath = request.getParameter("rPath");
	       String filePath = reportPath +File.separator+ reportName;
	       File file = new File(filePath);
	       if(file.exists()){
	           String filename = file.getName();
	           log.info("#报表开始下载------------>" + filename);
	           BufferedInputStream bis = null;
	           BufferedOutputStream bos = null;
	           try {
	               //设置响应头,控制浏览器下载该文件
	               response.setCharacterEncoding("UTF-8");
	               response.setContentType("application/force-download");
	               response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
	               response.setContentLength( (int) file.length( ));
	               bis = new BufferedInputStream(new FileInputStream(file));
	               bos = new BufferedOutputStream(response.getOutputStream());
	               byte[] buff= new byte[1024];
	               int len = 0;
	               while(-1 != (len = bis.read(buff, 0, buff.length))){
	                   bos.write(buff, 0, len);
	               }
	               bis.close();
	               bos.flush();
	               bos.close();
	           } catch (Exception e) {
	               e.printStackTrace();
	           }finally {
	               if(bos!=null){
	                   bos.close();
	               }
	               if(bis!=null){
	                   bis.close();
	               }
	           }
	           log.info("#报表下载完成------------>" + filename);
	       }
	   }

3IO缓冲流

	public class Demo {
		public static void main(String[] args) throws IOException {
			
			// 1. 创建一个文件字节输入流对象, 关联源文件
			InputStream in = new FileInputStream("C:\\Test\\temp\\loli.avi");
			// 1.2 创建一个缓冲字节输入流, 用于包装 `文件字节输入流` 对象
.			BufferedInputStream bis = new BufferedInputStream(in);
			
.			// 2. 创建一个文件字节输出流对象, 关联目标文件
.			File file = new File("C:\\Test\\temp\\lo.avi");
			if (!file.exists()) {
			// 如果文件不存在, 就需要创建
			File parentFile = file.getParentFile();
			parentFile.mkdirs();
			}
.			OutputStream out = new FileOutputStream(file);
.			// 2.2 创建一个缓冲字节输出流, 用于包装 `文件字节输出流` 对象
	 		BufferedOutputStream bos = new BufferedOutputStream(out);
		
.			// 3. 读取与写入
			byte[] buf = new byte[1024];
.			int len = -1;
			while ((len = bis.read(buf)) != -1) {
				bos.write(buf, 0, len);
.			}
			
			// 4. 关闭资源
			bos.close();
.			bis.close();
		}
.	}

4复制目录及子目录 (多层复制)

public class Demo09 {
	public static void main(String[] args) throws IOException {
			// 复制目录及子目录
		
		// 1. 创建两个文件目录, 一个关联源文件, 另一个关联目标文件
.			File srcFile = new File("C:\\Users\\Temp");	
			File destFile = new File("C:\\Users\\myDoc");	
		
		// 2. 调用功能
		copyDirAndAllFiles(srcFile, destFile);
			
}	
	private static void copyDirAndAllFiles(File srcFile, File destFile) throws IOException {
			// 获取 `源文件夹` 的名称
		     String srcName = srcFile.getName();
			// System.out.println(srcName);
		
			// 在 `目标文件夹` 下创建一个源文件名称的文件夹
			File copyFile = new File(destFile, srcName);
			copyFile.mkdirs();
.			
			// 获取源文件下的所有文件对象
			File[] listFiles = srcFile.listFiles();
		// 循环遍历, 获取每一个文件对象
			// 特别说明 : 遍历中 f 是源文件的名称, copyFile 是目标文件的写入名称.
			for (File f : listFiles) {
				// 判断是否为文件夹或文件对象
				if (f.isDirectory()) {
				// System.out.println(f.getName() + " 是文件夹对象.");
					// 实现方法的递归调用
				copyDirAndAllFiles(f, copyFile);
				} else {
			  	// System.out.println(f.getName() + " 是文件对象.");
					// 实现文件对象复制
				   // 1. 创建缓冲字节输入 / 输出流对象
					BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
					BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(copyFile, f.getName())));
					// 2. 读写操作
					byte[] buf = new byte[1024];
					int len = -1;
				while ((len = bis.read(buf)) != -1) {
						bos.write(buf, 0, len);
						bos.flush();
					}
				// 3. 关闭资源
				bos.close();
		    	bis.close();
				}
			}
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值