作业
- 键盘录入一个文件夹路径,删除该文件夹(包含文件夹内容)
import java.io.File;
import java.util.Scanner;
/*键盘录入一个文件夹路径,删除该文件夹(包含文件夹内容)
File里面的length()方法*/
public class Homework01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag;
do {
flag = false;
System.out.println("请录入一个文件夹路径:");
String dir = sc.nextLine();
File file = new File(dir);
if(file.isDirectory()) {
if(deleteDir(file)) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}else {
System.out.println("您的录入有误,请重新录入!");
flag = true;
}
}while(flag);
}
private static boolean deleteDir(File file) {
File[] files = file.listFiles();
for(File f : files) {
if(f.isFile()) {
f.delete();
}else {
deleteDir(f);
}
}
//file.delete();
return file.delete();
}
}
2、键盘录入一个文件夹路径,统计该文件夹下的各种后缀名的文件的个数
例如:.txt有10个,.java有30个......
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Scanner;
/*键盘录入一个文件夹路径,统计该文件夹下的各种后缀名的文件的个数
例如:.txt有10个,.java有30个......*/
public class Homework02 {
private static LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>();
//这样定义,要在主方法以及自定义方法中使用,map需要定义为静态
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag;
do {
flag = false;
System.out.println("请录入一个文件夹路径:");
String dir = sc.nextLine();
File file = new File(dir);
if(file.isDirectory()) {
fileCount(file);
StringBuilder sb = new StringBuilder();
for(Entry<String,Integer> entry : map.entrySet()) {
sb.append(entry.getKey()+"有"+entry.getValue()+"个,");
}
System.out.println(sb.substring(0,sb.length()-1));
}else {
System.out.println("您的录入有误,请重新录入!");
flag = true;
}
}while(flag);
}
private static void fileCount(File file) {
File[] files = file.listFiles();
for(File f : files) {
if(f.isFile()) {
String[] strs = f.getName().split("\\.");
String key = "." + strs[strs.length-1];
map.put(key, map.containsKey(key)?map.get(key)+1:1);
}else {
fileCount(f);
}
}
//由于这是一个递归的方法,所以最终结果map集合的遍历不能放在这,数量会有问题
//可以将map集合定义为全局变量,在主方法遍历
/*StringBuilder sb = new StringBuilder();
for(Entry<String,Integer> entry : map.entrySet()) {
String str = entry.getKey()+"有"+entry.getValue()+"个";
System.out.println(str);
}*/
}
}
3、键盘录入一个文件夹路径,作为源文件夹;键盘录入一个文件夹路径,作为目标文件夹
写代码将源文件夹拷贝到目标文件夹中
a
b
b/a
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
/*键盘录入一个文件夹路径,作为源文件夹;键盘录入一个文件夹路径,作为目标文件夹
写代码将源文件夹拷贝到目标文件夹中
a
b
b/a*/
public class Homework03 {
public static void main(String[] args) throws Exception {
File srcDir= inputDir();
/*String[] strs = dir1.split("\\\\");
String s = strs[strs.length-1];*/
File dest = inputDir();
File destDir = new File(dest,srcDir.getName());
destDir.mkdir();
copyDir(srcDir,destDir);
}
private static void copyDir(File srcDir, File destDir) throws Exception {
File[] files = srcDir.listFiles();
for(File f : files) {
File srcFile = f;
File destFile = new File(destDir,f.getName());
if(f.isFile()) {
copyFile(srcFile,destFile);
}else {
destFile.mkdir();
copyDir(srcFile,destFile);
}
}
}
private static void copyFile(File f1, File f2) throws Exception {
/*BufferedReader br = new BufferedReader(new FileReader(f1));
BufferedWriter bw = new BufferedWriter(new FileWriter(f2));
String str = "";
while((str=br.readLine())!=null) {
bw.write(str);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
用字符流会乱码,字节流就不会,还不限制图片什么的,啧啧啧
*/
BufferedInputStream bis= new BufferedInputStream(new FileInputStream(f1));
BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream(f2));
int len = 0;
byte[] b = new byte[1024];
while((len = bis.read(b))!=-1) {
bos.write(b,0,len);
}
bis.close();
bos.close();
}
private static File inputDir() {
File file;
Scanner sc = new Scanner(System.in);
boolean flag;
do {
flag = false;
System.out.println("请录入一个文件夹路径:");
String dir = sc.nextLine();
file = new File(dir);
if(!file.isDirectory()){
System.out.println("您的录入有误,请重新录入!");
flag = true;
}
}while(flag);
return file;
}
}
4 在D盘下的a/b/c/d中创建一个文件,名称叫HelloWorld.txt
5 键盘录入一个字符串,表示一个文件夹路径,如果不是文件夹路径则提示重新录入
打印当前文件夹下,所有的大于20M的后缀名是.mp4的文件的绝对路径
6 用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
/*用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)*/
public class Homework06 {
public static void main(String[] args) throws Exception {
String str = "If you want to change your fate I think you must come to the ujiuye to learn java";
LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>();
String[] strs = str.split(" ");
for(String key : strs) {
map.put(key, map.containsKey(key)?map.get(key)+1:1);
}
BufferedWriter bw = new BufferedWriter(new FileWriter("F:/count.txt"));
for(Entry<String,Integer> entry : map.entrySet()) {
bw.write(entry.getKey()+"=" + entry.getValue());
bw.newLine();
bw.flush();
}
bw.close();
}
}
7 产生10个1-100的随机数,并放到一个数组中
(1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
(2)把数组中的数字放到当前文件夹的number.txt文件中
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/*产生10个1-100的随机数,并放到一个数组中
(1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
(2)把数组中的数字放到当前文件夹的number.txt文件中*/
public class Homework07 {
public static void main(String[] args) throws Exception {
List<Integer> list = new ArrayList<Integer>();
Random r = new Random();
int[] arr = new int[10];
for(int n = 0;n < arr.length;n++) {
int num = r.nextInt(100)+1;
arr[n] = num;
if(num > 10) {
list.add(num);
System.out.println(num);
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("number.txt"));
for(Integer n : arr) {
bw.write(Integer.toString(n));//最好写入字符串!!!!不然乱码
bw.newLine();
bw.flush();
}
bw.close();
}
}