public static void main(String[] args) {
// changeToByte(-1);
// jiechengqiuhe(10);
//int sum=totalFileCount("E:\\test\\jar\\account");
int sum=totalFileCountUseStack("E:\\test\\jar\\account");
System.out.print("总文件个数=" + sum);
}
/**
* 1.打印一个整数的32位二进制
* 思路:原数据与32位二位进制相与,0与1相与&为0,1与1相与&为1
*/
public static void changeToByte(int num) {
StringBuffer bufferResult = new StringBuffer();
for (int a = 31; a >= 0; a--) {
if ((num & (1 << a)) == 0) {
bufferResult.append("0");
} else {
bufferResult.append("1");
}
}
System.out.println(bufferResult.toString());
}
/**
* 2.求:1!+2!+3!+...n!之和
* 思路 n的阶乘是n-1的阶层之和*n,
*/
public static void jiechengqiuhe(int n) {
int j = 1;
int sum = 0;
for (int i = 1; i <= n; i++) {
j = j * i;
sum = sum + j;
}
System.out.println(sum);
}
/**
* 3.统计一个文件夹里面文件的个数
* 思路1.递归查询
*/
static int sum = 0;
public static int totalFileCount(String filePath) {
try {
File file = new File(filePath);
if (!file.exists()) {
sum = 0;
} else {
for (File fileChild : file.listFiles()) {
if (fileChild.isFile()) {
sum++;
} else {
totalFileCount(fileChild.getAbsolutePath());
}
}
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return sum;
}
/**
* 3.统计一个文件夹里面文件的个数
* 思路2.使用堆栈来循环查询
*/
public static int totalFileCountUseStack(String filePath) {
int sumNew = 0;
try {
File file = new File(filePath);
if (!file.exists()) {
sumNew = 0;
} else {
if(file.isFile())
{
return 1;
}
Stack<File> stack=new Stack<>();
// stack.add(file);
stack.push(file);//add 和push效果一样
while(!stack.isEmpty())//判断目录文件夹是否已经循环完成
{
File fileChild=stack.pop();
for(File fileGrandson :fileChild.listFiles() )
{
if (fileGrandson.isFile()) {
sumNew++;
}
if(fileGrandson.isDirectory())
{
stack.push(fileGrandson);
}
}
}
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return sumNew;
}
/**
* 4.冒泡排序 .
* 思路:将最大值的往上冒泡
*/
public void bubbleSort() {
int length = arr.length;
for (int a = length - 1; a >= 0; a--) {
for (int b = 0; b < a; b++) {
if (arr[b] > arr[b + 1]) {
int temp = arr[b];
arr[b] = arr[b + 1];
arr[b + 1] = temp;
}
}
}
for (int m = 0; m < length; m++) {
System.out.println(arr[m]);
}
}