前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java开发_读写txt文件操作

java开发_读写txt文件操作

作者头像
Hongten
发布2018-09-13 16:36:39
2.2K0
发布2018-09-13 16:36:39
举报
文章被收录于专栏:HongtenHongten

项目结构:

运行效果:

========================================================

下面是代码部分:

========================================================

/Text/src/com/b510/txt/MyFile.java

代码语言:javascript
复制
  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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-06-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档