Java Base IO Stream copy Keyboard entry directory, copy the. java file to the specified directory, the name of the specified directory, then rename the encrypted file to calculate the number of letters

Source: Internet
Author: User
Tags readfile

 PackageCom.swift.jinji;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileFilter;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.Scanner;/*get the input file directory from the console and then copy the. java file under the directory (containing subdirectories) to the E:/java folder and count the number of Java files. Hint: If there are files of the same name, if two Test01.java, The copy to the destination folder can have only one test01.java, and the other Test01.java to be modified to another name: the name can be randomly generated, as long as it is not repeated.*/ Public classCopyjavasamename { Public Static voidMain (string[] args) {File DestDir=NewFile ("E:/java"); if(!destdir.exists ()) {System.out.println ("The target directory does not exist and is about to be established.");        Destdir.mkdirs (); } File Srcdir=Getdir ();        Copydir (Srcdir,destdir); System.out.println ("Copy Complete"); }    Private Static voidcopydir (file srcdir, file DestDir) {intI=0; file[] Files=srcdir.listfiles (NewFileFilter () {@Override Public BooleanAccept (File pathname) {if(Pathname.getname (). toLowerCase (). EndsWith (". Java"))                return true; return false;        }});  for(File file:files) {String name=File.getname (); File DestFile=NewFile (destdir,name); if(!destfile.exists ())            {copyFile (file,destfile); }Else{String Nameg=destfile.getname () + "-Copy"; File Destfileg=NewFile (Destdir,nameg);            CopyFile (File,destfileg); }                    }    }    Private Static voidcopyFile (file file, file destfile) {bufferedoutputstream BOS=NULL; Bufferedinputstream bis=NULL; Try{BOS=NewBufferedoutputstream (NewFileOutputStream (destfile)); Bis=NewBufferedinputstream (Newfileinputstream (file)); byte[] buf=New byte[1024]; intLen;  while((Len=bis.read (BUF))!=-1) {bos.write (buf,0, Len); }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }    Private StaticFile Getdir () {Scanner scan=NewScanner (system.in); System.out.println ("Please enter a directory"); String Str=Scan.nextline (); File dir=NewFile (str); if(!dir.exists ()) {System.out.println ("Directory does not exist."); }        if(!dir.isdirectory ()) {System.out.println ("Not a Directory"); }        returndir; }}

Calculate the number of letters in an encrypted file

 PackageCom.swift.jinji;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.util.Comparator;ImportJava.util.Map;ImportJava.util.Scanner;ImportJava.util.TreeMap;/*in the D-disk directory there is an encrypted file A.txt (only English and digital files), the password is "Heima" when the password is entered correctly to read the data in the file. It is required to use code to simulate the process of reading the file, and to count the number of occurrences of each letter in the file, and to output the statistical results in the following format to the D-disk count.txt. A:2 a b:3 a c:4 a ......*/ Public classPASSWORDFILECHARACTERNUMS2 { Public Static voidMain (string[] args) {File passwordfile=NewFile ("D:/stu.txt"); String Password= "Heima";            ReadFile (Passwordfile,password); }    Private Static voidReadFile (File passwordfile, String password) {Scanner Scan=NewScanner (system.in);  while(true) {System.out.println ("Please enter a correct password for the file"); String Passwordinput=Scan.nextline (); if(passwordinput.equals (password)) {System.out.println ("The file password is correct.");  Break; }} bufferedreader br=NULL; BufferedWriter BW=NULL; Try{br=NewBufferedReader (NewFileReader (passwordfile)); File DestFile=NewFile ("Count.txt"); Bw=NewBufferedWriter (NewFileWriter (destfile)); Map<Character,Integer> map=NewTreemap<> (NewComparator<character>() {@Override Public intCompare (Character O1, Character O2) {intNum= (int) o1-(int) O2; returnnum;            }            });            String Line;  while((Line=br.readline ())! =NULL) {                Char[] chars=Line.tochararray ();  for(Charc:chars) {                    if(!Map.containskey (c)) {Map.put (c,1); }Else{map.put (c, Map.get (c)+1);            }}} System.out.println (map);  for(Character c:map.keyset ()) {Bw.write ("+c+": "+map.get (c) +" X ");                Bw.newline ();            Bw.flush (); } System.out.println ("Map output succeeded"); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally {            Try{bw.close ();            Br.close (); } Catch(IOException e) {e.printstacktrace (); }        }    }}

Java Base IO Stream copy Keyboard entry directory, copy the. java file to the specified directory, the name of the specified directory, then rename the encrypted file to calculate the number of letters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.