Java的知识点27——打印子孙级目录和文件的名称、统计文件夹的大小、编码与解码的应用
生活随笔
收集整理的這篇文章主要介紹了
Java的知识点27——打印子孙级目录和文件的名称、统计文件夹的大小、编码与解码的应用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
打印子孫級(jí)目錄和文件的名稱
package cn.dym12;import java.io.File;/*** 遞歸:方法自己調(diào)用自己* 打印子孫級(jí)目錄和文件的名稱* @author Administrator**/ public class DirDemo03 {public static void main(String[] args) {File src=new File("D:\\java\\workspace\\D1\\src\\cn");printName(src,0);}//打印子孫級(jí)目錄和文件的名稱public static void printName(File src,int deep) {//控制前面層次的for(int i=0;i<deep;i++) {System.out.print("-");}//打印名稱System.out.println(src.getName());if(null==src||!src.exists()) { //遞歸頭return;}else if(src.isDirectory()) { //是目錄for(File s:src.listFiles()) {printName(s,deep+1); //遞歸體}}} }統(tǒng)計(jì)文件夾的大小
package cn.dym12;import java.io.File;/*** 統(tǒng)計(jì)文件夾的大小* @author Administrator**/public class DirDemo05 {public static void main(String[] args) {File src=new File("D:\\java\\workspace\\D1");count(src);System.out.println(len);}private static long len=0;public static void count(File src) {//獲取大小if(null!=src&&src.exists()) {if(src.isFile()) { //大小len+=src.length();}else { //子孫級(jí)for(File s:src.listFiles()) {count(s);}}}} }使用面向?qū)ο?#xff1a;統(tǒng)計(jì)文件夾的大小
package cn.dym12;import java.io.File;/*** 使用面向?qū)ο?#xff1a;統(tǒng)計(jì)文件夾的大小* @author Administrator**/ public class DirDemo05 {//大小private long len;//文件夾路徑private String path;//文件的個(gè)數(shù)private int fileSize;//文件夾的個(gè)數(shù)private int dirSize;private File src;public DirDemo05(String path) {this.path = path;this.src = new File(path);count(this.src);} private void count(File src) { //獲取大小if(null!=src && src.exists()) {if(src.isFile()) { len+=src.length(); //大小this.fileSize++;}else { //子孫級(jí)this.dirSize++;for(File s:src.listFiles()) {count(s);}}}} public long getLen() {return len;}public int getFileSize() {return fileSize;}public int getDirSize() {return dirSize;}public static void main(String[] args) {DirDemo05 dir=new DirDemo05("D:/java/workspace/D1");System.out.println(dir.getLen()+"-->"+dir.getFileSize()+"-->"+dir.getDirSize());DirDemo05 dir2=new DirDemo05("D:\\java\\workspace\\D1\\src\\cn\\dym12\\FileDemo03.java");System.out.println(dir2.getLen()+"-->"+dir2.getFileSize()+"-->"+dir2.getDirSize());}}UTF-8? ? 一個(gè)中文占3個(gè)字節(jié)?
編碼:字符串 ——》 字節(jié)
package cn.dym12; import java.io.UnsupportedEncodingException; /*** 編碼:字符串 ——》 字節(jié)* @author Administrator**/ public class ContentEncode {public static void main(String[] args) throws UnsupportedEncodingException {String msg ="性命生命使命a";//編碼: 字節(jié)數(shù)組byte[] datas = msg.getBytes(); //默認(rèn)使用工程的字符集//GBKSystem.out.println(datas.length);//編碼: 其他字符集datas = msg.getBytes("UTF-16LE");System.out.println(datas.length);datas = msg.getBytes("Utf-8");System.out.println(datas.length);}}解碼: 字節(jié)->字符串?
package cn.dym12;import java.io.UnsupportedEncodingException;/*** 解碼: 字節(jié)->字符串* @author **/ public class ContentDecode {public static void main(String[] args) throws UnsupportedEncodingException {String msg ="性命生命使命a";//編碼: 字節(jié)數(shù)組byte[] datas = msg.getBytes(); //默認(rèn)使用工程的字符集//解碼: 字符串 String?(byte[] bytes, int offset, int length, String charsetName)msg = new String(datas,0,datas.length,"utf8");System.out.println(msg);//亂碼: //1)、字節(jié)數(shù)不夠msg = new String(datas,0,datas.length-2,"utf8");System.out.println(msg);msg = new String(datas,0,datas.length-1,"utf8");System.out.println(msg);//2)、字符集不統(tǒng)一msg = new String(datas,0,datas.length-1,"gbk");System.out.println(msg); } }總結(jié)
以上是生活随笔為你收集整理的Java的知识点27——打印子孙级目录和文件的名称、统计文件夹的大小、编码与解码的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java的知识点26——File_API
- 下一篇: Java的知识点28——文件编码、IO流