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("呵呵");}}}}}



原创粉丝点击