2017.08.15-File练习
来源:互联网 发布:外卖人8.6源码下载 编辑:程序博客网 时间:2024/06/07 03:30
package com.shuhuadream.work;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.TreeMap;/*1."sdfgzxcvasdfxcvdf"获取该字符串中的每个字母出现的次数。希望打印结果a(1)c(2)d(3).....按照字母自然顺序打印 使用TreeMap*/public class Work01 {public static void main(String[] args) { Map<Character, Integer> map = new TreeMap<Character, Integer>(); String s = new String("sdfgzxcvasdfxcvdf"); char[] c = s.toCharArray(); for (int i = 0; i < c.length; i++) { if (map.get(c[i])==null) { map.put(c[i], 1); }else { map.put(c[i], map.get(c[i])+1); } } Set<Character> set = map.keySet(); Iterator<Character> it = set.iterator(); while (it.hasNext()) { Character character = (Character) it.next(); System.out.println(character+"("+map.get(character)+")"); } } }
package com.shuhuadream.work;import java.io.File;import java.util.HashSet;import java.util.Set;/*2.指定一个路径,删除其下面(含子文件夹中)的同名,同大小,同修改时间的重复文件,只保留一个(利用集合HashSet<MyFile>).递归*/public class Work02 {static Set<FileInfo> set = new HashSet<FileInfo>();public static void main(String[] args) {File file = new File("d:\\demo");fileTool(file);}public static void fileTool(File file){if (file.isDirectory()) {File[] childFiles = file.listFiles();for (File childFile : childFiles) {if (childFile.isFile()) {boolean b = set.add(new FileInfo(childFile.getName(),childFile.getTotalSpace(),childFile.lastModified()));System.out.println(b);System.out.println(set);if(b==false){childFile.delete();System.out.println("哈哈");}}else{fileTool(childFile);}}}}}
package com.shuhuadream.work;public class FileInfo{private String name;private long big;private long time;public FileInfo(String name, long big, long time) {super();this.name = name;this.big = big;this.time = time;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Long getBig() {return big;}public void setBig(Long big) {this.big = big;}public long getTime() {return time;}public void setTime(long time) {this.time = time;}public int hashCode() {final int prime = 31;int result = 1;result = prime * result + (int) (big ^ (big >>> 32));result = prime * result + ((name == null) ? 0 : name.hashCode());result = prime * result + (int) (time ^ (time >>> 32));return result;}public boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;FileInfo other = (FileInfo) obj;if (big != other.big)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (time != other.time)return false;return true;}public String toString() {return "FileInfo [name=" + name + ", big=" + big + ", time=" + time + "]";}}
package com.shuhuadream.work;import java.io.File;/*3.指定一个路径,将其下面(含子文件夹中)的以".txt"结尾的文件移动到指定的另一个路径下; renameTo()以上两道题都是递归来写。*/public class Work03 {public static void main(String[] args) {File file = new File("d:\\demo");fileTool(file);}public static void fileTool(File file){if (file.isDirectory()) {File[] childFiles = file.listFiles();for (File childFile : childFiles) {if (childFile.isFile()) {if (childFile.getName().endsWith(".txt")) {System.out.println("哈哈");childFile.renameTo(new File("d:\\d",childFile.getName()));//System.out.println("哈哈");}}else{fileTool(childFile);System.out.println("呵呵");}}}}}
阅读全文
0 0
- 2017.08.15-File练习
- File练习
- 21File练习
- day23(File递归练习)
- file类递归练习
- File类的使用练习
- 黑马程序员:File类练习
- 【opencv练习13 - File 输入输出】
- file上传文件操作【练习】
- file文件下载操作【练习】
- day23<File类递归练习>
- java例程练习(File类)
- Java——File类练习
- 一个简单的File练习代码
- File类,IO流练习题目
- file递归和一些综合练习
- Java_基础—File类练习
- java File类之listFiles练习
- go中包的概念、导入与可见性
- 机器学习6
- 一个ajax异步调用并自动刷新的例子
- 《Java编程思想》--Bruce Eckel 读书笔记
- 2017.8.15总结4-数学题
- 2017.08.15-File练习
- 7
- 2017"百度之星"程序设计大赛
- ShadowDrawing(阴影画图)~demo
- 8
- 9
- 第二章 身份验证 (二) Realm + Authenticator及AuthenticationStrategy
- JavaScript作用域专题
- 使用Bot Framework建立你的第一个聊天机器人