统计文件夹大小
package com.java.IO;
import java.io.File;
import java.util.Scanner;
/*
* 统计文件夹大小
*/
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//直接获取文件夹大小,获取到的是0
File dir = getDir();
long length = getFileLength(dir);
System.out.println(length);
}
public static File getDir() {
Scanner sc = new Scanner(System.in);
while(true) {
String line = sc.nextLine();
File file = new File(line);
if(!file.exists()) {
System.out.println(0);
}else if(file.isFile()) {
System.out.println(1);
}else {
return file;
}
}
}
public static long getFileLength(File dir) {
long len=0;
File[] subFiles=dir.listFiles();
for (File file : subFiles) {
if(file.isFile()) {
len=len+file.length();
}else if(file.isDirectory()) {
len=len+getFileLength(file);
}
}
return len;
}
}
删除文件夹
package com.java.IO;
import java.io.File;
public class Demo4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File dir = Demo3.getDir();
}
//删除文件夹
public static void deleteFile(File dir) {
File[] subFiles = dir.listFiles();
for (File file : subFiles) {
if(file.isFile()) {
file.delete();
}else {
deleteFile(file);
}
}
dir.delete();
}
}
文件夹的copy
package com.java.IO;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo5 {
public static void main(String[] args) throws IOException {
File src = Demo3.getDir();
File dest = Demo3.getDir();
if(!src.equals(dest)) {
copy(src,dest);
}else {
System.out.println("目标文件夹是源文件夹的子文件夹");
}
}
private static void copy(File src, File dest) throws IOException {
// TODO Auto-generated method stub
File newDir=new File(dest,src.getName());
newDir.mkdir();
File[] listFiles = src.listFiles();
for (File file : listFiles) {
if(file.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File(newDir,file.getName())));
int b;
while((b=bis.read())!=-1) {
bos.write(b);
}
bis.close();
bos.close();
}else {
copy(file,newDir);
}
}
}
}
将文件夹中的所有文件和文件夹名称按层级打印
import java.io.File;
import com.java.IO.Demo3;
public class Demo6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File dir = Demo3.getDir();
printLev(dir,0);
}
private static void printLev(File dir,int lev) {
// TODO Auto-generated method stub
File[] listFiles = dir.listFiles();
for (File file : listFiles) {
for(int i=0;i<=lev;i++) {
System.out.println("\t");
}
System.out.println(file);
if(file.isDirectory()) {
printLev(file,lev+1);
}
}
}
}
两种斐波那契数列的做法
package com.java.IO;
public class Demo7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// demo1();
System.out.println(fun(8));
}
public static int fun(int num) {
if(num==1||num==2)
return 1;
else
return fun(num-2)+fun(num-1);
}
private static void demo1() {
int[] arr=new int[8];
arr[0]=1;
arr[1]=1;
for (int i = 2; i < arr.length; i++) {
arr[i]=arr[i-1]+arr[i-2];
}
System.out.println(arr[arr.length-1]);
}
}
1000的阶乘所有0和尾部0的个数
package com.java.IO;
import java.math.BigInteger;
public class Demo8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger bi1 = new BigInteger("1");
for (int i = 1; i <=1000; i++) {
BigInteger bi2 = new BigInteger(i+"");
bi1 = bi1.multiply(bi2);
}
String str = bi1.toString();
// int count=0;
// for (int i = 0; i < str.length(); i++) {
// if('0'==str.charAt(i))
// count++;
// }
// System.out.println(count);
int count=0;
for (int i = 0; i < str.length(); i++) {
if('0'==str.charAt(str.length()-1-i)) {
count++;
}
else {
break;
}
}
System.out.println(count);
}
}
约瑟夫环,遇见3和3的倍数,排除掉,直到剩下一个数。
public static int getLucklyNum(int num) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i <=num; i++) {
list.add(i);
}
int count=1;
for(int i=0;list.size()!=1;i++) {
if(i==list.size()) {
i=0;
}
if(count%3==0) {
list.remove(i--);
}
}
return list.get(0);
}