又重做了一遍IO流的作业,改了一些bug
作业
- 键盘录入一个文件夹路径,删除该文件夹(包含文件夹内容)
import java.io.File;
import java.util.Scanner;
/*1、键盘录入一个文件夹路径,删除该文件夹(包含文件夹内容)*/
public class Test01 {
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()) {
deleteFile(file);
System.out.println("删除成功!");
}else {
System.out.println("您的录入有误,请重新录入");
flag = true;
}
}while(flag);
}
private static void deleteFile(File file) {
File[] files = file.listFiles();
for(File f : files) {
if(f.isFile()) {
f.delete();
}else {
deleteFile(f);
}
}
file.delete();//每次循环结束,一定要记得删掉已经空了的文件夹
}
}
2、键盘录入一个文件夹路径,统计该文件夹下的各种后缀名的文件的个数
例如:.txt有10个,.java有30个......
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Scanner;
/*2、键盘录入一个文件夹路径,统计该文件夹下的各种后缀名的文件的个数
例如:.txt有10个,.java有30个......*/
//当你计算的文件夹中有正在编辑的文件,譬如说docx文件,那么就会多一个临时的docx文件
public class Test02 {
private static LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>();
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()) {
countFile(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()-2));
}else {
System.out.println("您的录入有误,请重新录入");
flag = true;
}
}while(flag);
}
private static void countFile(File file) {
File[] files = file.listFiles();
for(File f : files) {
if(f.isFile()) {
String[] strs = f.getName().split("\\.");
String key = "." + strs[strs.length-1];//获取后缀名
//getName()方法可以获取路径中最后一个文件(加后缀)的名字,如果最后一个是目录也可以获取它的名字
map.put(key, map.containsKey(key)?map.get(key)+1:1);
}else {
countFile(f);//如果是目录,递归调用此方法
}
}
}
}
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 Test04 {
//此题关键是每次复制的时候,是目录的时候,要先创建目录,再复制
public static void main(String[] args) throws Exception {
//源文件夹路径
File srcDir = inputDir();
File dir = inputDir();
//目标文件夹路径
File destDir = new File(dir,srcDir.getName());
destDir.mkdir();//求你了,一定一定要记得创建文件夹啊!!
//调用文件夹复制方法
copyDir(srcDir,destDir);
System.out.println("复制成功!");
}
//文件夹复制方法
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 srcFile, File destFile) throws Exception {
//用字节流字节流字节流!!!
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
//是BufferedInputStream!!!,没有BufferedFileInputStream. InputStream是接口不能创建对象,必须用FileInputStream!!!
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] b = new byte[1024];//数组必须是偶数,汉字是两个字节编码,不是偶数会乱码!!!
int len = 0;//覆盖数组长度
while((bis.read(b))!=-1) {
bos.write(b,0,len);
}
bis.close();
bos.close();
}
//输入文件夹路径的方法一(可保证最终输入的是文件夹);
private static File inputDir() {
Scanner sc = new Scanner(System.in);
File file;
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;
}
//输入文件夹路径的方法二(可保证最终输入的是文件夹);
private static File inputDir2() {
Scanner sc = new Scanner(System.in);
boolean flag;
while(true) {
System.out.println("请录入一个文件夹路径:");
String dir = sc.nextLine();
File file = new File(dir);
if(file.isDirectory()) {
return file;
}else {
System.out.println("您的录入有误,请重新录入");
flag = true;
}
}
}
}
4 在D盘下的a/b/c/d中创建一个文件,名称叫HelloWorld.txt
5 键盘录入一个字符串,表示一个文件夹路径,如果不是文件夹路径则提示重新录入
打印当前文件夹下,所有的大于20M的后缀名是.mp4的文件的绝对路径
import java.io.File;
import java.util.Scanner;
/*5 键盘录入一个字符串,表示一个文件夹路径,如果不是文件夹路径则提示重新录入
打印当前文件夹下,所有的大于20M的后缀名是.mp4的文件的绝对路径*/
public class Test05 {
public static void main(String[] args) {
File file = inputDir();
printAbsolutePath(file);
}
private static void printAbsolutePath(File file) {
File[] files = file.listFiles();
for(File f : files) {
if(f.isDirectory()) {
printAbsolutePath(f);
}else {
if(f.length()>20*1024*1024 && f.getName().endsWith(".mp4")){
System.out.println(f.getAbsolutePath());
}
}
}
}
private static File inputDir() {
Scanner sc = new Scanner(System.in);
File file;
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;
}
}
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.io.IOException;
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 Test06 {
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";
String[] split = str.split(" ");
LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>();
for(String s : split) {
map.put(s, map.containsKey(s)?map.get(s)+1:1);
}
BufferedWriter bw = new BufferedWriter(new FileWriter("F:/count.txt"));//便于关闭
for(Entry<String,Integer> entry : map.entrySet()) {
String s = entry.getKey() + "=" + entry.getValue();
System.out.println(s);
bw.write(s);
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.LinkedList;
import java.util.List;
import java.util.Random;
/*7 产生10个1-100的随机数,并放到一个数组中
(1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
(2)把数组中的数字放到当前文件夹的number.txt文件中*/
public class Test07 {
public static void main(String[] args) throws Exception {
int[] arr = new int[10];
Random r = new Random();
List<Integer> list = new LinkedList<Integer>();
BufferedWriter bw = new BufferedWriter(new FileWriter("number.txt"));
for(int n = 0; n < 10; n++) {
int num = r.nextInt(100)+1;
arr[0] = num;
if(num > 10) {
list.add(num);
System.out.println(num);
}
bw.write(Integer.toString(num));//字符串字符串字符串,数字会乱码!!!
bw.newLine();
bw.flush();
}
}
}
Properties练习二:
一个软件,免费使用3次,超过3次要续费。
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Properties;
/*Properties练习二:
一个软件,免费使用3次,超过3次要续费。*/
public class Test06 {
public static void main(String[] args) throws Exception {
Properties p = new Properties();
FileInputStream fis = new FileInputStream("config.properties");
p.load(fis);
//Object object = p.get("count");//get返回的是Object类型的
String s = p.getProperty("count");
int count = 0;
if(s==null) {
System.out.println("欢迎首次使用本软件");
count++;
p.setProperty("count", count+"");
}else {
String value = p.getProperty("count");
count = Integer.parseInt(value)+1;
if(count>3) {
System.out.println("您的次数用完了哦,继续使用请充值!");
return;
}
p.setProperty("count", count+"");
System.out.println("欢迎第" + count + "次使用本软件");
}
FileWriter fw = new FileWriter("config.properties");
p.store(fw,null);
}
}