Java:逐行读、写文件、文件目录过滤的用法
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class HelloWord { public static void main(String[] args) throws IOException { String filePath = "my File.txt"; File file = new File(filePath); InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); List<String> lineItems=new ArrayList<String>(); String line = null; while ((line = bufferedReader.readLine()) != null) { String[] items = line.split(","); String item= items[1]; if(item.indexOf("花园")!=-1){ lineItems.add(line); } } bufferedReader.close(); inputStreamReader.close(); for(String my : lineItems){ System.out.println(my); } } }
- 写文件用法:
List<String> lines = new ArrayList<>(); String wfilePath="e:\\1.txt"; File wfile = new File(wfilePath); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(wfile), "utf-8"); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); for (String eLine : lines) { bufferedWriter.write(eLine); bufferedWriter.newLine(); } bufferedWriter.close(); outputStreamWriter.close(); System.out.println("Complete...");
- 文件目录过滤:
实例:
1 package com.dx.hibernate5.test; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStreamReader; 11 import java.io.OutputStreamWriter; 12 import java.io.UnsupportedEncodingException; 13 import java.util.ArrayList; 14 import java.util.HashMap; 15 import java.util.List; 16 import java.util.Map; 17 18 public class HelloWord { 19 public static void main(String[] args) throws IOException { 20 String filePath = "E:\\丽水"; 21 String wfilePath = "E:\\丽水_New"; 22 parserCountyVsCode(filePath); 23 24 parseCityCounty(filePath, wfilePath); 25 26 System.out.println("Complete..."); 27 } 28 29 private static void parserCountyVsCode(String filePath) 30 throws UnsupportedEncodingException, FileNotFoundException, IOException { 31 File file = new File(filePath); 32 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8"); 33 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 34 // 缙云县:5782 35 // 云和县:5784 36 // 开发区:578B 37 // 龙泉市:5786 38 // 景宁县:5789 39 // 青田县:5783 40 // 景宁畲族自治县:5789 41 // 松阳县:5788 42 // 庆元县:5785 43 // 莲都区:5781 44 // 遂昌县:5787 45 List<String> countyList = new ArrayList<>(); 46 countyList.add("5782"); 47 countyList.add("5784"); 48 countyList.add("578B"); 49 countyList.add("5786"); 50 countyList.add("5789"); 51 countyList.add("5783"); 52 countyList.add("5788"); 53 countyList.add("5785"); 54 countyList.add("5781"); 55 countyList.add("5787"); 56 57 Map<String, String> lineItems = new HashMap<String, String>(); 58 String line = null; 59 while ((line = bufferedReader.readLine()) != null) { 60 String[] items = line.split(","); 61 62 String address = items[3]; 63 String county = items[2]; 64 65 if (address.indexOf("莲都区") != -1) { 66 lineItems.put("莲都区", county); 67 } else if (address.indexOf("开发区") != -1) { 68 lineItems.put("开发区", county); 69 } else if (address.indexOf("青田县") != -1) { 70 lineItems.put("青田县", county); 71 } else if (address.indexOf("缙云县") != -1) { 72 lineItems.put("缙云县", county); 73 } else if (address.indexOf("遂昌县") != -1) { 74 lineItems.put("遂昌县", county); 75 } else if (address.indexOf("松阳县") != -1) { 76 lineItems.put("松阳县", county); 77 } else if (address.indexOf("云和县") != -1) { 78 lineItems.put("云和县", county); 79 } else if (address.indexOf("庆元县") != -1) { 80 lineItems.put("庆元县", county); 81 } else if (address.indexOf("景宁县") != -1 || address.indexOf("景宁畲族自治县") != -1) { 82 lineItems.put("景宁县", county); 83 } else if (address.indexOf("龙泉市") != -1) { 84 lineItems.put("龙泉市", county); 85 } else { 86 Boolean hasContains = false; 87 for (String coun : countyList) { 88 if (coun.equals(county)) { 89 hasContains = true; 90 } 91 } 92 if (!hasContains) { 93 System.out.println(line); 94 } 95 } 96 } 97 98 bufferedReader.close(); 99 inputStreamReader.close(); 100 101 for (Map.Entry<String, String> kvEntry : lineItems.entrySet()) { 102 System.out.println(kvEntry.getKey() + ":" + kvEntry.getValue()); 103 } 104 } 105 106 private static void parseCityCounty(String filePath, String wfilePath) 107 throws UnsupportedEncodingException, FileNotFoundException, IOException { 108 Map<String, String> idVsCountry = new HashMap<>(); 109 idVsCountry.put("5782", "缙云县"); 110 idVsCountry.put("5784", "云和县"); 111 idVsCountry.put("578B", "开发区"); 112 idVsCountry.put("5786", "龙泉市"); 113 idVsCountry.put("5789", "景宁县"); 114 idVsCountry.put("5783", "青田县"); 115 idVsCountry.put("5788", "松阳县"); 116 idVsCountry.put("5785", "庆元县"); 117 idVsCountry.put("5781", "莲都区"); 118 idVsCountry.put("5787", "遂昌县"); 119 120 List<String> lines = new ArrayList<>(); 121 lines.add("abc0"); 122 lines.add("abc1"); 123 lines.add("abc2"); 124 File file = new File(filePath); 125 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8"); 126 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 127 String line = null; 128 while ((line = bufferedReader.readLine()) != null) { 129 String[] items = line.split(","); 130 131 String address = items[3]; 132 String county = items[2]; 133 String newLine = items[0] + ",丽水市," + idVsCountry.get(county) + "," + address; 134 // System.out.println(newLine); 135 lines.add(newLine); 136 } 137 138 bufferedReader.close(); 139 inputStreamReader.close(); 140 141 File wfile = new File(wfilePath); 142 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(wfile), "utf-8"); 143 BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); 144 for (String eLine : lines) { 145 bufferedWriter.write(eLine); 146 bufferedWriter.newLine(); 147 } 148 149 bufferedWriter.close(); 150 outputStreamWriter.close(); 151 } 152 }
基础才是编程人员应该深入研究的问题,比如:
1)List/Set/Map内部组成原理|区别
2)mysql索引存储结构&如何调优/b-tree特点、计算复杂度及影响复杂度的因素。。。
3)JVM运行组成与原理及调优
4)Java类加载器运行原理
5)Java中GC过程原理|使用的回收算法原理
6)Redis中hash一致性实现及与hash其他区别
7)Java多线程、线程池开发、管理Lock与Synchroined区别
8)Spring IOC/AOP 原理;加载过程的。。。
【+加关注】。