面试

模拟计算器:

code

package com.heisejiuhuche.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Calculator {
    public static void main(String[] args) {
        try {
            calculate();
        } catch(IOException e) {
            System.out.println("计算失败...");
        }
    }

    private static void calculate() throws IOException {
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while((line = bufr.readLine()) != null) {
            if(line.equals("over"))
                break;
            char[] ch = line.toCharArray();
            for(char c : ch) {
                if(c == '+') {
                    String[] str = line.split("\\+");
                    int x = Integer.parseInt(str[0]);
                    int y = Integer.parseInt(str[1]);
                    System.out.println(x + y);
                }
                if(c == '*') {
                    String[] str = line.split("\\*");
                    int x = Integer.parseInt(str[0]);
                    int y = Integer.parseInt(str[1]);
                    System.out.println(x * y);
                }
                if(c == '/') {
                    String[] str = line.split("/");
                    int x = Integer.parseInt(str[0]);
                    int y = Integer.parseInt(str[1]);
                    System.out.println(x / y);
                }
                if(c == '-') {
                    String[] str = line.split("-");
                    int x = Integer.parseInt(str[0]);
                    int y = Integer.parseInt(str[1]);
                    System.out.println(x - y);
                }
            }
        }
        bufr.close();
    }
}

注释也不想写了,也不想给优化了,原生态代码,多年之后看看,会觉得当时好菜~

复制文件夹(包括文件和文件夹中的文件夹)

code

package com.heisejiuhuche.api;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyDirectory {
    public static void main(String[] args) {
        File path = new File("C:\\Users\\jeremy\\Documents\\javaTmp\\mp");
        File dest = new File("C:\\Users\\jeremy\\Documents\\mp\\");
        try {
            copyDir(path, dest);
        } catch(IOException e) {
            System.out.println("复制失败...");
        }
    }

    private static void copyDir(File src, File dest) throws IOException  {
        if(!dest.exists())
            dest.mkdir();
        File[] files = src.listFiles();
        for(int i = 0; i < files.length; i ++) {
            if(files[i].isDirectory()) {
                File newPath = new File(dest + "\\" + files[i].getName());
                newPath.mkdir();
                copyDir(files[i], newPath);
            }
            File newFile = new File(dest + "\\" + files[i].getName());
            if(!newFile.isDirectory()) {
                System.out.println(newFile);
                BufferedReader bufr = new BufferedReader(new FileReader(files[i]));
                BufferedWriter bufw = new BufferedWriter(new FileWriter(newFile));
                String line = null;
                while((line = bufr.readLine()) != null) {
                    bufw.write(line);
                    bufw.newLine();
                    bufw.flush();
                }
                bufr.close();
                bufw.close();
            }
        }
    }
}