java 在线阅读_java开发_读写txt文件操作

1 packagecom.b510.txt;2

3 importjava.io.BufferedReader;4 importjava.io.File;5 importjava.io.FileInputStream;6 importjava.io.FileNotFoundException;7 importjava.io.FileOutputStream;8 importjava.io.FileReader;9 importjava.io.IOException;10 importjava.io.InputStreamReader;11 importjava.io.PrintWriter;12

13 /**

14 *@authorHongten15 *16 * @time 2011-12-12 201117 */

18 public classMyFile {19 @SuppressWarnings("static-access")20 public static voidmain(String[] args) {21 MyFile myFile = newMyFile();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 staticString filenameTemp;37

38 /**

39 * 创建文件40 *41 *@throwsIOException42 */

43 public static boolean creatTxtFile(String name) throwsIOException {44 boolean flag = false;45 filenameTemp = path + name + ".txt";46 File filename = newFile(filenameTemp);47 if (!filename.exists()) {48 filename.createNewFile();49 flag = true;50 }51 returnflag;52 }53

54 /**

55 * 写文件56 *57 *@paramnewStr58 * 新内容59 *@throwsIOException60 */

61 public static boolean writeTxtFile(String newStr) throwsIOException {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 = newFile(filenameTemp);76 //将文件读入输入流

77 fis = newFileInputStream(file);78 isr = newInputStreamReader(fis);79 br = newBufferedReader(isr);80 StringBuffer buf = newStringBuffer();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 = newFileOutputStream(file);92 pw = newPrintWriter(fos);93 pw.write(buf.toString().toCharArray());94 pw.flush();95 flag = true;96 } catch(IOException e1) {97 //TODO 自动生成 catch 块

98 throwe1;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 returnflag;117 }118

119 /**

120 * 读取数据121 */

122 public voidreadData1() {123 try{124 FileReader read = newFileReader(filenameTemp);125 BufferedReader br = newBufferedReader(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 publicString readDate() {138 //定义一个待返回的空字符串

139 String strs = "";140 try{141 FileReader read = new FileReader(newFile(filenameTemp));142 StringBuffer sb = newStringBuffer();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 returnstrs;159 }160 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值