黑马程序员技术交流社区
标题:
【黑马程序员】java练习题,面试题
[打印本页]
作者:
huburt
时间:
2016-5-22 15:20
标题:
【黑马程序员】java练习题,面试题
public class Exercise4IOe {
public static void main(String[] args) {
/** 需求:1从键盘接收一个文件夹路径 ,统计该文件夹大小 */
// System.out.println(size(getFile()));
/** 需求:2删除该文件夹 */
// delete(new File("E:\\ie"));
/**需求:3,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中*/
// File src = new File("E:\\src");
// File dest = new File("E:\\src\\s");
// try {
// copy(src, dest);
// } catch (IOException e) {
// e.printStackTrace();
// }
/**
* 需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印, 例如:
* aaa是文件夹,里面有bbb.txt,ccc.txt,ddd.txt这些文件,
* 有eee这样的文件夹,eee中有fff.txt和ggg.txt,打印出层级来 aaa bbb.txt ccc.txt ddd.txt
*/
// File src = new File("E:\\src");
// print(src,0);
/** 斐波那契数列 */
// System.out.println(fun(7));
/** 需求:求出1000的阶乘所有零和尾部零的个数 */
// countZero(1000);
/** 约瑟夫环 */
System.out.println(josephus(100, 14));
}
/** 从键盘接收一个文件夹路径 */
public static File getFile() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件夹路径:");
while (true) {
File dir = new File(sc.nextLine());
if (!dir.exists()) {
System.out.println("输入的地址有误或者文件夹不存在!请重新输入:");
} else if (dir.isFile()) {
System.out.println("输入的地址是文件路径!请重新输入一个文件夹路径:");
} else {
return dir;
}
}
}
/** 需求:1统计该文件夹大小 */
public static long size(File dir) {
long size = 0;
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
size += size(f);
} else {
size += f.length();
}
}
return size;
}
/** 需求:2删除该文件夹 */
public static void delete(File file) {
if (file.exists()) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
delete(f);
}
}
file.delete();
}
}
/**
* 需求:3,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
*
* @throws IOException
*/
public static void copy(File src, File dest) throws IOException {
if (!dest.toString().contains(src.toString())) {
File file = new File(dest, src.getName());
if (src.isDirectory()) {
file.mkdirs();
for (File f : src.listFiles()) {
copy(f, file);
}
} else {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte[] b = new byte[1024 * 8];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bis.close();
bos.close();
}
} else {
System.out.println("目标文件夹是源文件或者源文件的子文件夹!无法拷贝!");
}
}
/**
* 需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印, 例如:
* aaa是文件夹,里面有bbb.txt,ccc.txt,ddd.txt这些文件,
* 有eee这样的文件夹,eee中有fff.txt和ggg.txt,打印出层级来 aaa bbb.txt ccc.txt ddd.txt
*/
public static void print(File file, int level) {
if (file.exists()) {
for (int i = 0; i < level; i++) {
System.out.print("\t");
}
System.out.println(file.getName());
if (file.isDirectory()) {
for (File f : file.listFiles()) {
print(f, level + 1);
}
}
} else {
System.out.println("文件不存在!");
}
}
/** 斐波那契数列 */
public static int fun(int day) {
if (day == 1 || day == 2) {
return 1;
} else {
return fun(day - 1) + fun(day - 2);
}
}
/** 需求:求出1000的阶乘所有零和尾部零的个数 */
public static void countZero(int num) {
BigInteger b1 = new BigInteger("1");
for (int i = 1; i <= num; i++) {
BigInteger b2 = new BigInteger(i + "");
b1 = b1.multiply(b2);
}
String a = b1.toString();
System.out.println(a);
int count = 0;
int zero = 0;
for (int i = 0; i < a.length(); i++) {
if ('0' == a.charAt(i)) {
count++;
zero++;
} else {
zero = 0;
}
}
System.out.println("零的个数:" + count);
System.out.println("尾部零的个数:" + zero);
}
/** 约瑟夫环 */
public static int josephus(int peoples, int count) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i <= peoples; i++) {
list.add(i);
}
int now = 1;
for (int i = 0; list.size() > 1; i++) {
if (i == list.size()) {
i = 0;
}
if (now % count == 0) {
list.remove(i--);
}
now++;
}
return list.get(0);
}
}
复制代码
作者:
loop
时间:
2016-5-22 15:58
沙发了,谢谢楼主分享
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2