字符流

  1 范例:
  2 public class PetDemo {
  3     public static void main(String[] args) throws IOException {
  4         BufferedWriter bw = null;
  5         BufferedReader br = null;
  6         BufferedWriter modify_bw = null;
  7         try {
  8             //1.创建模板文件
  9             bw = new BufferedWriter(new FileWriter(new File("E:\Java实训课总结\2016.3.26" + File.separator + "pet.template")));
 10             bw.write("替换前:");
 11             bw.append("您好!我的名字是{name},我是一只{type}。我的主人是{master}。");
 12             bw.flush();
 13             //2.读取模板文件的信息
 14             br = new BufferedReader(new FileReader("E:\Java实训课总结\2016.3.26" + File.separator + "pet.template"));
 15             String line = br.readLine(); ★★★同一个对象,readLine每用到一次 ,就是一行!!!★★★
 16             System.out.println(line);
 17             //3.替换文件信息后,写入新的文本文件中
 18             String modify_line = line;
 19             modify_line = line.replace("替换前:", "替换后:");
 20             ★★★每次更新内容就赋值一次!!!★★★
 21             modify_line = modify_line.replace("{name}", "lili");
 22             modify_line = modify_line.replace("{type}", "拉布拉多");
 23             modify_line = modify_line.replace("{master}", "王刚");
 24             System.out.println(modify_line);
 25             modify_bw = new BufferedWriter(new FileWriter(new File("E:\Java实训课总结\2016.3.26" + File.separator + "pet.txt")));
 26             modify_bw.append(modify_line);
 27             modify_bw.flush();
 28         } catch (IOException e) {
 29             e.printStackTrace();
 30         } finally {
 31             modify_bw.close();
 32             br.close();
 33             bw.close();
 34         }
 35     }
 36 }
 37 
 38 
 39 范例1:/*方法1:UUID*/
 40 public class FileMethodDemo {
 41     public void creatFileFolder(String path) throws IOException{
 42         File file = new File(path);
 43         if (!file.exists()) {
 44             //创建文件夹
 45             file.mkdir();
 46             System.out.println("创建文件成功!");
 47         }
 48     }
 49     
 50     public void creatFile(String fileName) throws IOException{
 51         File file = new File(fileName);
 52         if (!file.exists()) {
 53             //创建文件
 54             file.createNewFile();
 55             System.out.println("创建文件成功!");
 56         }
 57     }
 58     
 59     //删除文件
 60     public void delFile(String fileName){
 61         File file = new File(fileName);
 62         if (file.exists()) {
 63             file.delete();
 64             System.out.println("删除文件成功!");
 65         }
 66     }
 67     
 68     //清理文件
 69     public void delAllFile(String path){
 70         File file = new File(path);
 71         if (file.exists()) {
 72             file.delete();
 73         }
 74     }
 75 }
 76 
 77 //测试
 78 public class D_demotxt {
 79     public static void main(String[] args) throws IOException {
 80         FileMethodDemo fm = new FileMethodDemo();    //调用FileMethodDemo中的方法
 81         String path = "E:\Java实训课总结\2016.3.25\Demo100";
 82         //清理文件中的已有的文件
 83         fm.delAllFile(path);
 84         try {
 85             //1.创建文件夹
 86             fm.creatFileFolder(path);
 87             //2.创建文件
 88             ArrayList<String> nameArr = new ArrayList<String>();
 89             //随机唯一的名字,放在集合中
 90             for (int i = 0; i < 100; i++) {
 91                 UUID uuid = UUID.randomUUID();
 92                 String fileName = uuid.toString();
 93                 nameArr.add(fileName);
 94             }
 95             
 96             //3.生成100个文件名字
 97             for (int i = 0; i < nameArr.size(); i++) {
 98                 String filepath = path + File.separator + nameArr.get(i) + ".txt";
 99                 fm.creatFile(filepath);
100             }
101             
102             //3.找出开头a的文件
103             for (int i = 0; i < 100; i++) {
104                 if (nameArr.get(i).startsWith("a")) {
105                     String str = path + File.separator + nameArr.get(i) + ".txt";
106                     fm.delFile(str);
107                     System.out.println(str);
108                 }
109             }
110         } catch (IOException e) {
111             e.printStackTrace();
112         }
113     }    
114 }
115 
116 
117 范例1:/*方法2:Map*/
118 /*学生类*/
119 public class Student {
120     private String name;
121     private String[] xin = new String[]{"丁","王","徐","汤","独孤","李"};
122     private String[] ming = new String[]{"强","求败","建国","援朝","翠花","狗蛋","嫚","凯","程琳"};
123     
124     public Student(){
125         Random rd = new Random();
126         this.name = xin[rd.nextInt(xin.length)] + ming[rd.nextInt(ming.length)];
127     }
128     
129     public String getName() {
130         return name;
131     }
132     public void setName(String name) {
133         this.name = name;
134     }
135 }
136 
137 /*测试类*/
138 public class ImproveDulName {
139     public static void main(String[] args){
140         Map<Integer, Student> map = new HashMap<Integer, Student>();
141         /*存数据*/
142         for (int i = 0; i <= 15; i++) {
143             map.put(i, new Student());
144         }
145         //标识重复的名字
146         String dulName = null;
147         for (int j = 0; j < map.size(); j++) {
148             for (int j2 = j+1; j2 < map.size(); j2++) {
149                 if (map.get(j).getName().equals(map.get(j2).getName())) {
150                     map.get(j).setName(map.get(j).getName() + "*");
151                     map.put(j, map.get(j));
152                     if (map.get(j2).getName().startsWith("徐")) {
153                         System.out.println("学号:" + (j2 + 1) + "," + map.get(j2).getName());
154                     }
155                     break;
156                 } else{
157                     map.put(j, map.get(j));
158                 }
159             }
160         }
161     
162         //构建字符流对象
163         BufferedWriter bw = null;
164         try {
165             bw = new BufferedWriter(new FileWriter("E:\Java实训课总结\2016.3.24\ImprovedulplicateName.txt"));
166             for (int j = 0; j < map.size(); j++) {
167                 bw.append("学号:" + (j + 1) + "," + map.get(j).getName());
168                 bw.newLine();
169                 bw.flush();
170             }
171         } catch (Exception e) {
172             e.printStackTrace();
173         } finally {
174             try {
175                 bw.close();
176             } catch (IOException e) {
177                 e.printStackTrace();
178             }
179         }
180     }    
181 }
182 
183 
184 范例1:/*方法3:重复3次以上的名字打星号*/
185 /*学生类*/
186 public class Student {
187     private String familyName;
188     private String givenName;
189     private int flag;//重名的标记
190     private String name;
191     private int count;
192     
193     public Student(){
194         //随机填充一个名字
195         Random rd = new Random();
196         this.familyName = MyUtil.familyName[rd.nextInt(MyUtil.familyName.length)];
197         this.givenName = MyUtil.givenName[rd.nextInt(MyUtil.givenName.length)];
198         this.flag = 0;//假设初始的状态都不同
199         this.name = this.familyName + this.givenName;
200         this.count = 1;
201     }
202 
203     public Student(String familyName, String givenName, int flag) {
204         this.familyName = familyName;
205         this.givenName = givenName;
206         this.flag = flag;
207         this.name = this.familyName + this.givenName;
208         this.count = 1;
209     }
210     
211     @Override
212     public String toString() {
213         return "Student [familyName=" + familyName + ", givenName=" + givenName + ", flag=" + flag + ", name=" + name
214                 + ", count=" + count + "]";
215     }
216 
217     public String getName() {
218         return name;
219     }
220     public void setName() {
221         this.name = this.familyName + this.givenName;
222     }
223     public String getFamilyName() {
224         return familyName;
225     }
226     public void setFamilyName(String familyName) {
227         this.familyName = familyName;
228     }
229     public String getGivenName() {
230         return givenName;
231     }
232     public void setGivenName(String givenName) {
233         this.givenName = givenName;
234     }
235     public int getFlag() {
236         return flag;
237     }
238     public void setFlag(int flag) {
239         this.flag = flag;
240     }
241     public int getCount() {
242         return count;
243     }
244     public void setCount(int count) {
245         this.count = count;
246     }
247 
248     //测试
249     public static void main(String[] args) {
250         List<Student> al = new ArrayList<Student>();
251         for (int i = 0; i < 50; i++) {
252             Student stu = new Student();
253             al.add(stu);
254             System.out.println(stu);
255         }
256         
257         StudentOP sop = new StudentOP();
258 //        sop.changeFlagforDulName(al);
259         sop.changeFlagforThrName(al);
260         //测试修改成功与否
261         for (Student stu : al) {
262             System.out.println(stu);
263         }
264         
265         //测试写文件
266         sop.WriteToTxt(al);
267     }
268 }
269 /*学生操作类*/
270 public class StudentOP {
271     //1.对学生对象的姓名重复的,把flag重置为1
272     public void changeFlagforDulName(List<Student> stuArr){
273         //遍历数组,把重名的flag重置为1
274         for (int i = 0; i < stuArr.size(); i++) {
275             for (int j = i + 1; j < stuArr.size(); j++) {
276                 if (stuArr.get(i).getName().equals(stuArr.get(j).getName())) {
277                     stuArr.get(i).setFlag(1);
278 //                    stuArr.get(j).setFlag(1);
279                 }
280             }
281         }
282     }
283     
284     //2.重复三次的姓名标记
285     public void changeFlagforThrName(List<Student> stuArr){
286         //遍历数组,把重名的flag重置为1
287         changeFlagforDulName(stuArr);
288         for (int i = 0; i < stuArr.size(); i++) {
289             int count = stuArr.get(i).getCount();
290             for (int j = i + 1; j < stuArr.size(); j++) {
291                 if (stuArr.get(i).getName().equals(stuArr.get(j).getName())) {
292                     stuArr.get(i).setCount(++count);
293                     int my = stuArr.get(i).getCount();
294                     stuArr.get(j).setCount(my);
295                 }
296             }
297         }
298     }
299     
300     public void WriteToTxt(List<Student> stuArr){
301         BufferedWriter bw = null;
302         try {
303             bw = new BufferedWriter(new FileWriter("E:\Java实训课总结\2016.3.28\100rd_name.txt"));
304             for (Student stu : stuArr) {
305 //                if (stu.getFlag() == 1) {
306                 if (stu.getCount() == 3) {
307                     bw.write(stu.getName() + "*");
308                 } else {
309                     bw.write(stu.getName());
310                 }
311                 bw.newLine();
312             }
313         } catch (IOException e) {
314             e.printStackTrace();
315         } finally {
316             try {
317                 bw.flush();
318                 bw.close();
319             } catch (IOException e) {
320                 e.printStackTrace();
321             }
322         }
323     }
324 }
325 /*随机姓和名类*/
326 public class MyUtil {
327     public static String[] familyName = {"王","李","徐","温","张","刘"};
328     public static String[] givenName = {"丽丽","峰","仲基","嫚","学友","佳丽","凯"};
329 }
原文地址:https://www.cnblogs.com/ivy-xu/p/5330535.html