项目结构:
运行效果:
========================================================
下面是代码部分:
========================================================
/Text/src/com/b510/txt/MyFile.java
1 package com.b510.txt; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.FileReader; 9 import java.io.IOException; 10 import java.io.InputStreamReader; 11 import java.io.PrintWriter; 12 13 /** 14 * @author Hongten 15 * 16 * @time 2011-12-12 2011 17 */ 18 public class MyFile { 19 @SuppressWarnings("static-access") 20 public static void main(String[] args) { 21 MyFile myFile = new MyFile(); 22 try { 23 for (int i = 0; i < 5; i++) { 24 myFile.creatTxtFile("test"); 25 myFile.writeTxtFile("显示的是追加的信息" + i); 26 String str = myFile.readDate(); 27 System.out.println("*********\n" + str); 28 } 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 35 private static String path = "txt/"; 36 private static String filenameTemp; 37 38 /** 39 * 创建文件 40 * 41 * @throws IOException 42 */ 43 public static boolean creatTxtFile(String name) throws IOException { 44 boolean flag = false; 45 filenameTemp = path + name + ".txt"; 46 File filename = new File(filenameTemp); 47 if (!filename.exists()) { 48 filename.createNewFile(); 49 flag = true; 50 } 51 return flag; 52 } 53 54 /** 55 * 写文件 56 * 57 * @param newStr 58 * 新内容 59 * @throws IOException 60 */ 61 public static boolean writeTxtFile(String newStr) throws IOException { 62 // 先读取原有文件内容,然后进行写入操作 63 boolean flag = false; 64 String filein = newStr + "\r\n"; 65 String temp = ""; 66 67 FileInputStream fis = null; 68 InputStreamReader isr = null; 69 BufferedReader br = null; 70 71 FileOutputStream fos = null; 72 PrintWriter pw = null; 73 try { 74 // 文件路径 75 File file = new File(filenameTemp); 76 // 将文件读入输入流 77 fis = new FileInputStream(file); 78 isr = new InputStreamReader(fis); 79 br = new BufferedReader(isr); 80 StringBuffer buf = new StringBuffer(); 81 82 // 保存该文件原有的内容 83 for (int j = 1; (temp = br.readLine()) != null; j++) { 84 buf = buf.append(temp); 85 // System.getProperty("line.separator") 86 // 行与行之间的分隔符 相当于“\n” 87 buf = buf.append(System.getProperty("line.separator")); 88 } 89 buf.append(filein); 90 91 fos = new FileOutputStream(file); 92 pw = new PrintWriter(fos); 93 pw.write(buf.toString().toCharArray()); 94 pw.flush(); 95 flag = true; 96 } catch (IOException e1) { 97 // TODO 自动生成 catch 块 98 throw e1; 99 } finally { 100 if (pw != null) { 101 pw.close(); 102 } 103 if (fos != null) { 104 fos.close(); 105 } 106 if (br != null) { 107 br.close(); 108 } 109 if (isr != null) { 110 isr.close(); 111 } 112 if (fis != null) { 113 fis.close(); 114 } 115 } 116 return flag; 117 } 118 119 /** 120 * 读取数据 121 */ 122 public void readData1() { 123 try { 124 FileReader read = new FileReader(filenameTemp); 125 BufferedReader br = new BufferedReader(read); 126 String row; 127 while ((row = br.readLine()) != null) { 128 System.out.println(row); 129 } 130 } catch (FileNotFoundException e) { 131 e.printStackTrace(); 132 } catch (IOException e) { 133 e.printStackTrace(); 134 } 135 } 136 137 public String readDate() { 138 // 定义一个待返回的空字符串 139 String strs = ""; 140 try { 141 FileReader read = new FileReader(new File(filenameTemp)); 142 StringBuffer sb = new StringBuffer(); 143 char ch[] = new char[1024]; 144 int d = read.read(ch); 145 while (d != -1) { 146 String str = new String(ch, 0, d); 147 sb.append(str); 148 d = read.read(ch); 149 } 150 System.out.print(sb.toString()); 151 String a = sb.toString().replaceAll("@@@@@", ","); 152 strs = a.substring(0, a.length() - 1); 153 } catch (FileNotFoundException e) { 154 e.printStackTrace(); 155 } catch (IOException e) { 156 e.printStackTrace(); 157 } 158 return strs; 159 } 160 }