【JAVA百炼成仙】特别篇——(三个IO练习题)
生活随笔
收集整理的這篇文章主要介紹了
【JAVA百炼成仙】特别篇——(三个IO练习题)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
🔎這里是JAVA成仙路,關(guān)注我學(xué)習(xí)JAVA不迷路
👍如果對你有幫助,給博主一個(gè)免費(fèi)的點(diǎn)贊以示鼓勵(lì)
歡迎各位🔎點(diǎn)贊👍評(píng)論收藏??
前言:本章給大家來了三個(gè)IO小練習(xí)題,代碼是當(dāng)時(shí)初學(xué)時(shí)寫的,寫的不咋地,僅供參考哈。
JAVA成仙路從基礎(chǔ)開始講,后續(xù)會(huì)講到JAVA高級(jí),中間會(huì)穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)韼椭?#xff01;
文章目錄
- 一、將所有JAVA文件內(nèi)容添加到一個(gè)文件中
- 二、統(tǒng)計(jì)文件中每個(gè)單詞出現(xiàn)的數(shù)量
- 三、將一個(gè)目錄下的所有文件復(fù)制到另一個(gè)新目錄
- 四、完結(jié)
一、將所有JAVA文件內(nèi)容添加到一個(gè)文件中
```java package A1;import java.io.*;/*** @author yeqv* @program A2* @Classname a4* @Date 2022/1/18 17:06* @Email w16638771062@163.com*/ public class a4 {public static void main(String[] args) throws FileNotFoundException {File file = new File("D:\\idea\\ja");thow(file);}public static void thow(File file) {//判斷文件名是否為目錄if (file.isDirectory()) {//轉(zhuǎn)為FILE類型數(shù)組File[] files = file.listFiles();//遍歷子目錄for (File file1 : files) {//判斷是否為子目錄,如果是就遞歸if (file1.isDirectory()) {thow(file1);}//判斷子目錄是否是普通文件,并且后綴是javaif (file1.isFile() && file1.getName().endsWith(".java")) {//new一個(gè)輸入流對象和輸出流對象,并創(chuàng)建一個(gè)文件try (var a = new FileOutputStream("D:\\java123.java", true); var b = new FileInputStream(file1.getAbsolutePath())) {//將對象b的內(nèi)容添加到文件a中b.transferTo(a);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}}//判斷文件是否是普通文件,并且后綴是javaif (file.isFile() && file.getName().endsWith(".java")) {//new一個(gè)輸入流對象和輸出流對象,并創(chuàng)建一個(gè)文件try (var a = new FileOutputStream("D:\\java123.java", true); var b = new FileInputStream(file.getAbsolutePath())) {//將對象b的內(nèi)容添加到文件a中b.transferTo(a);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}} }二、統(tǒng)計(jì)文件中每個(gè)單詞出現(xiàn)的數(shù)量
package demo;import java.io.*; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern;/*** @author yeqv* @program A2* @Classname A3* @Date 2022/1/19 20:55* @Email w16638771062@163.com*/ public class A3 {public static void main(String[] args) {Set<String> set = new HashSet<>();List<String> list = new ArrayList<>();File file = new File("D:\\jdk.txt");Pattern pattern = Pattern.compile("\s[a-zA-Z]+\s");String len;try {BufferedReader buff = new BufferedReader(new FileReader(file));while ((len = buff.readLine()) != null) {Matcher matcher = pattern.matcher(len);while (matcher.find()) {set.add(matcher.group());list.add(matcher.group());}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}int b = 1;for (String s : set) {int a = 0;for (String s1 : list) {if (s.equals(s1)) {a += 1;}}System.out.printf("%d單詞:%s 數(shù)量:%d%n", b, s, a);b += 1;}}}三、將一個(gè)目錄下的所有文件復(fù)制到另一個(gè)新目錄
package demo;import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths;/*** @author yeqv* @program A2* @Classname A5* @Date 2022/1/20 16:04* @Email w16638771062@163.com*/ public class A5 {//復(fù)制目錄public static void main(String[] args) throws IOException {String a = "D:\\金山打字";String b = "D:\\銀山打字";copy1(a, b);}public static void copy1(String a, String b) throws IOException {copy2(new File(a), new File(b));}public static void copy2(File file, File file1) throws IOException {if (!file1.exists()) {file1.mkdirs();}if (file.isDirectory()) {File[] files = file.listFiles();for (File file2 : files) {if (file2.isDirectory()) {copy2(file2, new File(file1.getPath(), file2.getName()));} else {Files.copy(file2.toPath(), Paths.get(file1.getPath() + "\\" + file2.getName()));}}}}}四、完結(jié)
JAVA基礎(chǔ)完結(jié),下一個(gè)專欄是23種設(shè)計(jì)模式,以虹貓藍(lán)兔七俠傳的故事為主題。新專欄下周一開更,歡迎h(huán)xd們捧場! 🎈🎈🍕🍕
總結(jié)
以上是生活随笔為你收集整理的【JAVA百炼成仙】特别篇——(三个IO练习题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Alexa, Stop Spying o
- 下一篇: B站Up主-山地人-这位老哥2019年的