日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java的知识点27——打印子孙级目录和文件的名称、统计文件夹的大小、编码与解码的应用

發(fā)布時(shí)間:2025/4/16 java 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。