public class test1 { / * * keyboard input from a folder, the folder size statistics * / public static void main (String [] args) { File the dir = the getFile (); . the System OUT .println (getFileLength (the dir)) ; } Private static Long getFileLength (File the dir) { Long len = 0 ; File [] Farr = dir.listFiles (); for (File SUBFILE: Farr) { IF (subFile.isFile ()) { len = len +subFile.length (); // file size is the length in bytes }the else{ len= len +getFileLength (SUBFILE); // recursive folder is encountered at } } returnlen; } public staticFile the getFile () {// Get keyboard input Scanner S1=new newScanner (the System.in); . the SystemOUT.println ("Please enter a folder path"); the while(to true) { String path=s1.nextLine (); file F1=new new File (path); IF (f1.exists ()) { IF (f1.isFile ()) { System. OUT .println ( " you entered is a file path, please re-enter a folder path " ); } the else { return F1; } } the else { . the System OUT .println ( " path you entered is not present, re-enter a folder path " ); } } } }
import java.io.File; public class test2 { public static void main(String[] args) { File dir = test1.getFile(); deleteFile(dir); } private static void deleteFile(File dir) { // 递归删除文件夹 File[] subFiles = dir.listFiles(); for (File subFile : subFiles) { if (subFile.isFile()) { subFile.delete(); } else { deleteFile(subFile); } } Dir.delete (); } }
Import java.io.BufferedInputStream The; Import java.io.BufferedOutputStream; Import java.io.File; Import a java.io.FileInputStream; Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; Import javax.swing.text.DefaultEditorKit.CopyAction; public class Test3 { / * * 2 files received from the keyboard folder path, the one folder to another folder * / public static void main (String [] args) throws IOException { File the src = test1.getFile (); File dest = test1.getFile(); copy(src,dest); } private static void copy(File src, File dest) throws IOException { File newDir = new File(dest,src.getName()); newDir.mkdir(); File[] subFiles = src.listFiles(); for (File subfile : subFiles) { if(subfile.isFile()){ BufferedInputStream b1 = new BufferedInputStream(new FileInputStream(subfile)); BufferedOutputStream b2 = new BufferedOutputStream( new FileOutputStream(new File(newDir,subfile.getName()))); int b; while ((b = b1.read()) != -1) { b2.write(b); } b1.close(); b2.close(); }else{ copy(subfile, newDir); } } }
Import java.io.File; Import java.io.PrintStream; public class Test4 { / * * get a folder from the keyboard, in accordance with the print file and folder hierarchy * / public static void main (String [] args) { File the dir = test1.getFile (); PrintLev (the dir, 0 ); } public static void PrintLev (File the dir, int Lev) { File [] subFiles = dir.listFiles (); for (the subfile File: subFiles) { for ( int I = 0; i <= lev; i ++) { System.out.print("\t"); } System.out.println(subfile); if (subfile.isDirectory()) { PrintLev(subfile,lev + 1); } } } }
import java.io.FileInputStream; public class test5 { /* * 斐波那契数列 */ public static void main(String[] args) { int x = fei(12); System.out.println(x); int[] arr = new int[12]; for (int i = 0; i < arr.length; i++) { arr[i] = fei(i+1); System.out.println(arr[i]); } } private static int fei(int n) { IF (n-n-||. 1 == == 2 ) { return . 1 ; } the else { return FEI (. 1-n-) + FEI (2-n- ); } } }
Import java.math.BigInteger; public class test6 { / * number of all zero factorial * 1000 and trailing zeros * / public static void main (String [] args) { // the demo1 (); // get all 0 demo2 (); // Get tail 0 } public static void demo2 () { a BigInteger B1 = new new a BigInteger ( ". 1" ); for ( int I =. 1; I <= 1000; I ++ ) { a BigInteger B2 = new new a BigInteger (I + " " ); b1 = b1.multiply(b2); } //System.out.println(b1); String str = b1.toString(); StringBuilder sb1 = new StringBuilder(str); str = sb1.reverse().toString(); int count = 0 ; for (int i = 0; i < str.length(); i++) { if ('0'!=str.charAt(i)) { break; } else { count++; } } System.out.println(count); } public static void demo1() { BigInteger b1 = new BigInteger("1"); for (int i = 1; i <= 1000; i++) { BigInteger b2 = new BigInteger(i+""); b1 = b1.multiply(b2); } //System.out.println(b1); String str = b1.toString(); int count = 0 ; for (int i = 0; i < str.length(); i++) { if ('0'==str.charAt(i)) { count= count +1 ; } } System.out.println(count); } }