1. 程式人生 > >java基礎IO流 復制鍵盤錄入的目錄,復制其中的.java文件到指定目錄,指定目錄中有重名,則改名 對加密文件計算字母個數

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

tac exceptio lean urn ext java基礎 reader ring 完成

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文件到指定目錄,指定目錄中有重名,則改名 對加密文件計算字母個數