java基礎IO流 複製鍵盤錄入的目錄,複製其中的.java檔案到指定目錄,指定目錄中有重名,則改名 對加密檔案計算字母個數

來源:互聯網
上載者:User

標籤:tree   return   加密檔案   格式   ati   檔案目錄   array   blog   can   

package com.swift.jinji;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Scanner;/*從控制台擷取輸入的檔案目錄然後將該目錄(包含子目錄)下的.java檔案複製到e:/java檔案夾中,並統計java檔案的個數.提示:如果有相同的名稱的檔案,如果兩個Test01.java,則拷貝到目標檔案夾時只能有一個Test01.java,另一個Test01.java要修改為另一個名稱:該名稱可隨機產生,只要不重複即可.*/public class CopyJavaSameName {    public static void main(String[] args) {        File destDir=new File("e:/java");        if(!destDir.exists()) {            System.out.println("目標目錄不存在,即將建立.");            destDir.mkdirs();        }        File srcDir=getDir();        copyDir(srcDir,destDir);        System.out.println("複製完成");    }    private static void copyDir(File srcDir, File destDir) {        int i=0;        File[] files=srcDir.listFiles(new FileFilter() {            @Override            public boolean accept(File pathname) {                if(pathname.getName().toLowerCase().endsWith(".java"))                return true;                return false;            }});        for(File file:files) {            String name=file.getName();            File destFile=new File(destDir,name);            if(!destFile.exists()) {                copyFile(file,destFile);            }else {                                String nameG=destFile.getName()+"-副本";                File destFileG=new File(destDir,nameG);                copyFile(file,destFileG);            }                    }    }    private static void copyFile(File file, File destFile) {        BufferedOutputStream bos=null;        BufferedInputStream bis=null;        try {            bos=new BufferedOutputStream(new FileOutputStream(destFile));            bis=new BufferedInputStream(new FileInputStream(file));            byte[] buf=new byte[1024];            int len;            while((len=bis.read(buf))!=-1) {                bos.write(buf, 0, len);            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    private static File getDir() {                Scanner scan=new Scanner(System.in);        System.out.println("請輸入一個目錄");        String str=scan.nextLine();        File dir=new File(str);        if(!dir.exists()) {            System.out.println("目錄不存在.");        }        if(!dir.isDirectory()) {            System.out.println("不是一個目錄");        }        return dir;    }}

對加密檔案計算字母個數

package com.swift.jinji;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Comparator;import java.util.Map;import java.util.Scanner;import java.util.TreeMap;/*在d盤目錄下有一個加密檔案a.txt(檔案裡只有英文和數字),密碼是“heima”當密碼輸入正確時才能讀取檔案裡的資料。現要求用代碼來類比讀取檔案的過程,並統計檔案裡各個字母出現的次數,並把統計結果按照如下格式輸出到d盤的count.txt中。a:2個b:3個c:4個............*/public class PasswordFileCharacterNums2 {    public static void main(String[] args) {        File passwordFile=new File("d:/stu.txt");        String password="heima";        readFile(passwordFile,password);            }    private static void readFile(File passwordFile, String password) {        Scanner scan =new Scanner(System.in);        while(true) {                        System.out.println("請輸入一個正確的檔案密碼");            String passwordInput=scan.nextLine();            if(passwordInput.equals(password)) {                System.out.println("檔案密碼正確.");                break;            }        }        BufferedReader br=null;        BufferedWriter bw=null;        try {            br=new BufferedReader(new FileReader(passwordFile));            File destFile=new File("count.txt");            bw=new BufferedWriter(new FileWriter(destFile));            Map<Character,Integer> map=new TreeMap<>(new Comparator<Character>() {                @Override                public int compare(Character o1, Character o2) {                    int num=(int)o1-(int)o2;                    return num;                }            });            String line;            while((line=br.readLine())!=null) {                char[] chars=line.toCharArray();                for(char c: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)+"個");                bw.newLine();                bw.flush();            }            System.out.println("map輸出成功");                                            } catch (FileNotFoundException e) {            e.printStackTrace();        } catch(IOException e) {            e.printStackTrace();        }finally {            try {                bw.close();                br.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}

 

java基礎IO流 複製鍵盤錄入的目錄,複製其中的.java檔案到指定目錄,指定目錄中有重名,則改名 對加密檔案計算字母個數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.