Java复习之知识点整理(十三)----标准的输入输出流,File类,浏览文件夹和复制文件夹

37 篇文章 3 订阅
一、标准的输入流 System.in
--------------------------------------------------
1.类型是InputStream

@Test
	public void tsSystemIn() throws Exception
	{
		InputStream is = System.in;
		InputStreamReader isr = new InputStreamReader(is);
		
		BufferedReader br = new BufferedReader(isr);
		String line = null;
		while((line = br.readLine()) != null){
			if(line.equals("quit")){
				System.exit(-1);
			}
			System.out.println("hello:" + line);
		}	
	}



二、标准的输出流System.out
---------------------------------------------------
1.System.out 的类型是PrintStream, 是OutputStream的子类的子类

@Test
	public void tsSystemOut() throws Exception{
		
		System.setOut(new PrintStream(new FileOutputStream("d:\\a.txt")));
		System.out.println("xxxxxxxxxxxxxxxxxx");
	}


三、File类,对目录或者文件的封装
---------------------------------------------------
1.isDirectory();
2.exists();
3.isFile();
4.mkdir() / mkdirs() : 创建一级 / 多级目录
5.creatNewFile();
6.listFiles();
7.getCanonicalPath() : 标准的,规范的路径
8.getAbsolutePath() : 绝对路径

四、递归输出D盘下的所有文件和文件夹
----------------------------------------------------
@Test
	public void tsScanFiles()
	{
		List<String> fileList = new ArrayList<String>();
		List<String> folderList = new ArrayList<String>();
		scanFiles("D:\\test",fileList,folderList);
		for(String s : fileList)
		{
			System.out.println("file : " + s);
		}
		for(String s : folderList)
		{
			System.out.println("folder : " + s);
		}
	}
	
	
	/**
	 * 浏览指定路径下的所有文件和文件夹
	 * @param desPath
	 * @param fileList
	 * @param folderList
	 */
	public void scanFiles(String desPath , List<String> fileList,List<String> folderList)
	{		
		File file = new File(desPath);
		File [] fs = file.listFiles();
		for(File f0 : fs)
		{
			if(f0.isFile()) /*如果是文件*/
			{
				fileList.add(f0.getAbsolutePath());
			}
			else
			{
				folderList.add(f0.getAbsolutePath());
				scanFiles(f0.getAbsolutePath(),fileList,folderList);
			}			
		}		
	}	



五、实现文件夹的复制
----------------------------------------------------

@Test
	public void tsCopyFolder()
	{
		copyFolder("D:\\test" , "F:\\ccc");
	}
	
	
	/**
	 * 拷贝文件夹
	 * @param src
	 * @param des
	 */
	public void copyFolder(String src,String des)
	{
		File file = new File(src);
		String rootFolderName = file.getName();
		des = des + "\\" + rootFolderName;	
		copyFolder_1(src,des);
	}
	
	public void copyFolder_1(String src,String des)
	{
		File file = new File(src);		
		File [] fs = file.listFiles();
		for(File f0 : fs)
		{
			if(f0.isFile()) /*如果是文件*/
			{
				copyFile(f0.getAbsolutePath(),des + "\\" + f0.getName());
			}
			else
			{
				String path = des + "\\"  +  f0.getName();
				File f = new File(path);
				f.mkdirs();
				copyFolder_1(f0.getAbsolutePath(),path);		
			}			
		}	
	}
	
	
	/**
	 * 复制文件
	 * @param src
	 * @param des
	 */
	public void copyFile(String src,String des)
	{
		try {
			
			FileInputStream fis = new FileInputStream(src);
			FileOutputStream fos = new FileOutputStream(des);
			byte [] buf = new byte[1024];
			int len = 0;
			while((len = fis.read(buf)) != -1)
			{
				fos.write(buf);
			}
			fis.close();
			fos.close();					
		} catch (Exception e) {
			e.printStackTrace();
		}
	}












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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值