Java文件夾統計案例

 

Java文件夾統計案例

遞歸打印子孫級目錄和文件的名稱:

public static void main(final String[] args) throws IOException {
		File src = new File("E:/java");
		printName(src,0);  //遞歸打印子孫級目錄和文件的名稱
		
		
	}
	public static void printName(File src,int deep) {
		//控制前面-,增加層次感
		for(int i = 0;i<deep;i++) {
			System.out.print("-");
		}
		//打印名稱
		System.out.println(src.getName());
		if(src == null||!src.exists()) {  //遞歸頭
			return;
		}else if(src.isDirectory()){  //目錄
			for(File s:src.listFiles()) {
				printName(s, deep+1);  //遞歸體
			}
		}
	}

統計文件夾大小、文件個數、文件夾個數:

public static void main(final String[] args) throws IOException {
		File src = new File("E:/java");
		cout(src);
		System.out.println("文件字節大小:"+len);
		System.out.println("文件個數:"+file);
		System.out.println("文件夾個數:"+directory);
		
	}
	
	private static long len = 0;
	private static long file = 0;
	private static long directory = -1; //算文件夾個數會包含自身
	public static void cout(File src) {
		//獲取大小 
		if(src.exists()||src!=null) {
			if(src.isFile()) {  //大小
				len+=src.length();  //所有文件大小進行累加
				file++;  //文件個數
			}else {  //子孫級
				for(File s:src.listFiles()) {
					cout(s);
				}
				directory++;  //文件夾個數
			}
		}
	}

文件字節流

文件字節輸入流:

/*文件字節輸入流*/
		//1、創建源
		File src =new File("C:\\Users\\lenovo\\Desktop\\新建文本文檔.txt");
		//2、選擇流
		InputStream is = new FileInputStream(src);
		//3、操作(分段讀取)
		byte[] flush = new byte[1024];  //緩衝容器(1024個字節=1k,1k1k的讀)
		int len;  //接收長度
		while((len=is.read(flush))!=-1) {
			//字節數組-->字符串(解碼)
			String str = new String(flush,0,len,"UTF-8");
			System.out.println(str);
		}
		//4、釋放資源
		is.close();

文件輸出流:

/*文件字節輸出流*/
		//1、創建源(系統會給你自動創建文件)
		/*File dest = new File("C:\\Users\\lenovo\\Desktop\\新建ws.txt");
		//2、選擇流
		OutputStream os = new FileOutputStream(dest,true); //加上true爲追加,加上false爲覆蓋(和不加效果一樣)
		//3、操作(寫出)
		String msg = "IO is so 4242I am ws\n";
		byte[] datas = msg.getBytes();  //字符串-->字節數組(編碼)
		os.write(datas, 0, datas.length);
		//4、釋放資源
		os.flush();  //清除
		os.close();*/

文件拷貝(注:輸入流、輸出流需分別關閉,先打開的後關閉):

//文件拷貝:文件字節輸入流、輸出流
		//1、創建源
		File src1 =new File("E:\\黨課\\計算機與信息表技術學院第二十期黨課初級班安排課程表.xlsx"); //源頭
		File src2 =new File("E:\\黨課\\王爍.xlsx");  //目的地
		//2、選擇流
		InputStream is = new FileInputStream(src1);
		OutputStream os = new FileOutputStream(src2);
		//3、操作(分段讀取)
		byte[] flush = new byte[1024];  //緩衝容器(1024個字節=1k,1k1k的讀)
		int len;  //接收長度
		while((len=is.read(flush))!=-1) {
			os.write(flush,0,len);
		}
		os.flush();
		//4、釋放資源。分別關閉,先打開的後關閉
		os.close();
		is.close();

文件字符流

文件拷貝:

//文件拷貝:文件字符輸入流、輸出流
		//1、創建源
		File src1 =new File("D:\\杞歡\\王爍.txt"); //源頭
		File src2 =new File("D:\\杞歡\\100.txt");  //目的地
		//2、選擇流
		Reader reader = new FileReader(src1);
		Writer writer = new FileWriter(src2);
		//3、操作(分段讀取)
		char[] flush = new char[1024];  //緩衝容器
		int len;  //接收長度
		while((len=reader.read(flush))!=-1) {
			writer.write(flush,0,len);
		}
		writer.flush();
		//4、釋放資源。分別關閉,先打開的後關閉
		writer.close();
		reader.close();

字節數組流

字節數組輸入流:

                //字節數組輸入流
		//1、創建源:字節數組,不要太大
		byte[] src = "hello ws".getBytes();
		//2、選擇流
		InputStream is = new ByteArrayInputStream(src);
		//3、操作(分段讀取)
		byte[] flush = new byte[3];  //緩衝容器
		int len;  //接收長度
		while((len=is.read(flush))!=-1) {
			//字節數組-->字符串(解碼)
			String str = new String(flush,0,len);
			System.out.println(str);
		}
		//4、釋放資源(可以不用處理)

字節數組輸出流:

		//字節數組輸出流(獲取數據:toByteArray())
		//1、創建源:內部維護
		byte[] dest = null;
		//2、選擇流:不關聯源
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//3、操作(寫出內容)
		String msg = "show me singing!!";
		byte[] datas = msg.getBytes();  //字符串-->字節數組(編碼)
		baos.write(datas,0,datas.length);
		baos.flush();
		//獲取數據
		dest = baos.toByteArray();
		System.out.println(dest.length+"-->"+new String(dest,0,baos.size()));
		//4、釋放資源(可以不用處理)  

IO綜合-對接流:

public static void main(final String[] args) throws IOException {
		//圖片轉爲字節數組
		byte[] datas = fileToByteArray("E:\\c語言\\`@7ZP)Q8`MH2F6ER~6]MNSM.png");
		System.out.println(datas.length);
		byteArrayToFile(datas, "E:\\c語言\\`MNSM.png");
	}
	//1、圖片讀取到字節數組
	/**
	 * 1)、圖片到程序  FileInputStream
	 * 2)、程序到字節數組 ByteArrayOutputStream
	 * @throws IOException 
	 */
	public static byte[] fileToByteArray(String filePath) throws IOException {
		//1、創建源
		File src = new File(filePath);
		//2、選擇流
		InputStream is = new FileInputStream(src);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//3、操作(分段讀取)
		byte[] flush = new byte[1024];  //緩衝容器
		int len; //接收長度
		while((len=is.read(flush))!=-1){
			baos.write(flush,0,len);  //寫出到字節數組當中
		}
		baos.flush();
		is.close();
		return baos.toByteArray();
	}
	
	//1、字節數組到文件
		/**
		 * 1)、字節數組到程序  ByteArrayInputStream 
		 * 2)、程序到文件   FileOutputStream
		 * @throws IOException 
		 * 
		 */
		public static void byteArrayToFile(byte[] src,String filePath) throws IOException {
			//1、創建源
			File dest =new File(filePath);
			//2、選擇流
			ByteArrayInputStream is =new ByteArrayInputStream(src);
			OutputStream os =new FileOutputStream(dest);
			//3、操作(分段讀取)
			byte[] flush = new byte[5];  //緩衝容器
			int len;  //接收長度
			while((len=is.read(flush))!=-1) {
				os.write(flush,0,len);
			}
			//4、釋放資源
			os.close();
		}

************************************************************************************************************

您的建議是博主更新最大的動力!!

如發現錯誤請在評論區評論,博主會仔細查看並修改的!!

希望對您有所幫助!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章