stream练习
将字符串转换为键值对存储
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import java.util.ArrayList; import java.util.Map; import java.util.stream.Collectors; public class streamPractice02 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("zhangsan,12"); list.add("lisi,17"); list.add("wangwu,15"); list.add("zhaoliu,13");
Map<String, Integer> collect = list.stream().filter(s -> Integer.parseInt(s.split(",")[1]) >= 13) .collect(Collectors.toMap( s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1]))); System.out.println(collect); } }
|
IO练习
删除文件及其文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.io.File;
public class fileDemo02 { public static void main(String[] args) { File f=new File("D:\\Python爬虫"); deleteFile(f); }
private static void deleteFile(File f) { File[] files = f.listFiles(); assert files != null; for (File file : files) { if(file.isFile()){ file.delete(); } else { deleteFile(file); } } f.delete(); } }
|
查找本地所有盘符下以jpg为结尾的图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import java.io.File;
public class fileDemo01 { public static void main(String[] args) { findDir(); }
private static void findDir() { File[] f = File.listRoots(); for (File file : f) { findDir(file); } }
private static void findDir(File f) { File[] files = f.listFiles(); if (files != null) { for (File file : files) { if (file.isFile() && file.getName().endsWith("jpg")) { System.out.println(file); } else { findDir(file); } } } } }
|
统计文件大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import java.io.File;
public class fileDemo03 { public static void main(String[] args) { File f = new File("D:\\admin"); long lon = getlen(f); System.out.println(lon);
}
private static long getlen(File f) { long len = 0; File[] src = f.listFiles(); assert src != null; for (File file : src) { if (file.isFile()) { len = len + file.length(); } else { len = len + getlen(file); } } return len; } }
|
统计某文件夹下文件的总个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| mport java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Set;
public class fileDemo04 { public static void main(String[] args) { File file = new File(" "); HashMap<String, Integer> hm = getCount(file); System.out.println(hm); } private static HashMap<String, Integer> getCount(File file) { HashMap<String, Integer> hashMap = new HashMap<>(); File[] files = file.listFiles(); assert files != null; for (File f : files) { if (f.isFile()) { String[] arr = f.getName().split("\\."); if (arr.length >= 2) { String name = arr[arr.length - 1]; if (hashMap.containsKey(name)) { int count = hashMap.get(name); count++; hashMap.put(name, count); } else { hashMap.put(name, 1); } } } else { HashMap<String, Integer> sonDir = getCount(f); Set<Map.Entry<String, Integer>> entries = sonDir.entrySet(); for (Map.Entry<String, Integer> entry : entries) { String key = entry.getKey(); if (hashMap.containsKey(key)) { int value = hashMap.get(key) + entry.getValue(); hashMap.put(key, value); } else { hashMap.put(key, entry.getValue()); } } } } return hashMap; } }
|
文件拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
public class fileDemo05 { public static void main(String[] args) throws IOException {
File src = new File("");
File object = new File("");
copyDir(src, object);
}
private static void copyDir(File src, File object) throws IOException { object.mkdir();
File[] files = src.listFiles();
for (File file : files) { if (file.isFile()) { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(object); byte[] bytes = new byte[1024 * 4]; int len; while ((len = fis.read(bytes)) != -1) { fos.write(bytes, 0, len); fos.close(); fis.close(); } } else { copyDir(file,new File(object,file.getName())); }
} } }
|
图片加解密
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
public class fileDemo06 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream(""); FileOutputStream fos = new FileOutputStream("");
byte[] bytes = new byte[1024 * 5]; int len; while ((len = fis.read(bytes)) != -1) { fos.write(len ^ 20); }
} }
|
文件复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.io.*;
public class fileDemo07 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("..\\Javaplus\\a.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("..\\Javaplus\\b.txt")); String len; while ((len = reader.readLine()) != null) { System.out.println(len); writer.write(len); writer.newLine(); }
writer.close(); reader.close();
} }
|
a.txt
3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必得裨补阙漏,有所广益。
8.愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。
4.将军向宠,性行淑均,晓畅军事,试用之于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。
2.宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。
1.先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。
9.今当远离,临表涕零,不知所言。
6.臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。
7.先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐付托不效,以伤先帝之明,故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。
5.亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。
出师表按序号进行排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import java.io.*; import java.util.ArrayList; public class fileDemo08 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("..\\Javaplus\\csb.txt")); String len; ArrayList<String> list = new ArrayList<>();
while ((len = br.readLine()) != null) {
list.add(len);
} list.sort((o1, o2) -> { int s1 = Integer.parseInt(o1.split("\\.")[0]); int s2 = Integer.parseInt(o2.split("\\.")[0]); return s1 - s2; }); for (String s : list) { System.out.println(s); } br.close();
} }
|
csb.txt
3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必得裨补阙漏,有所广益。
8.愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。
4.将军向宠,性行淑均,晓畅军事,试用之于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。
2.宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。
1.先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。
9.今当远离,临表涕零,不知所言。
6.臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。
7.先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐付托不效,以伤先帝之明,故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。
5.亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。
爬取网页上的姓氏,生成名字假数据
写入到本地文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| package demo07;
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class Test1 { public static void main(String[] args) throws IOException {
String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0"; String boyNameNet = "http://www.haoming8.cn/baobao/10881.html"; String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";
String familyNameStr = webCrawler(familyNameNet); String boyNameStr = webCrawler(boyNameNet); String girlNameStr = webCrawler(girlNameNet);
ArrayList<String> familyNameTempList = getData(familyNameStr,"(.{4})(,|。)",1); ArrayList<String> boyNameTempList = getData(boyNameStr,"([\\u4E00-\\u9FA5]{2})(、|。)",1); ArrayList<String> girlNameTempList = getData(girlNameStr,"(.. ){4}..",0);
ArrayList<String> familyNameList = new ArrayList<>(); for (String str : familyNameTempList) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); familyNameList.add(c + ""); } } ArrayList<String> boyNameList = new ArrayList<>(); for (String str : boyNameTempList) { if(!boyNameList.contains(str)){ boyNameList.add(str); } } ArrayList<String> girlNameList = new ArrayList<>();
for (String str : girlNameTempList) { String[] arr = str.split(" "); Collections.addAll(girlNameList, arr); }
ArrayList<String> list = getInfos(familyNameList, boyNameList, girlNameList, 70, 50); Collections.shuffle(list);
BufferedWriter bw = new BufferedWriter(new FileWriter("..\\Javaplus\\names.txt")); for (String str : list) { bw.write(str); bw.newLine(); } bw.close();
}
public static ArrayList<String> getInfos(ArrayList<String> familyNameList,ArrayList<String> boyNameList,ArrayList<String> girlNameList, int boyCount,int girlCount){ HashSet<String> boyhs = new HashSet<>(); while (true){ if(boyhs.size() == boyCount){ break; } Collections.shuffle(familyNameList); Collections.shuffle(boyNameList); boyhs.add(familyNameList.get(0) + boyNameList.get(0)); } HashSet<String> girlhs = new HashSet<>(); while (true){ if(girlhs.size() == girlCount){ break; } Collections.shuffle(familyNameList); Collections.shuffle(girlNameList); girlhs.add(familyNameList.get(0) + girlNameList.get(0)); } ArrayList<String> list = new ArrayList<>(); Random r = new Random(); for (String boyName : boyhs) { int age = r.nextInt(10) + 18; list.add(boyName + "-男-" + age); } for (String girlName : girlhs) { int age = r.nextInt(8) + 18; list.add(girlName + "-女-" + age); } return list; }
private static ArrayList<String> getData(String str, String regex,int index) { ArrayList<String> list = new ArrayList<>(); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); while (matcher.find()){ list.add(matcher.group(index)); } return list;
}
public static String webCrawler(String net) throws IOException { StringBuilder sb = new StringBuilder(); URL url = new URL(net); URLConnection conn = url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream()); int ch; while ((ch = isr.read()) != -1){ sb.append((char)ch); } isr.close(); return sb.toString(); } }
|
压缩文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package demo07;
import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream;
public class zip { public static void main(String[] args) throws IOException { File src = new File(" "); File dest = new File("");
unzip(src, dest); }
private static void unzip(File src, File dest) throws IOException {
ZipInputStream zis = new ZipInputStream(new FileInputStream(src)); ZipEntry z; while ((z = zis.getNextEntry()) != null) {
if (z.isDirectory()) {
File f = new File(dest, z.getName()); f.mkdir();
} else { FileOutputStream fos = new FileOutputStream(new File(dest, z.getName())); int b; while ((b = zis.read()) != -1) { fos.write(b); } fos.close(); } } zis.close(); } }
|