java开发之读写txt文件操作的实现

时间:2023-11-17 06:00:23 

项目结构:

java开发之读写txt文件操作的实现

运行效果:

java开发之读写txt文件操作的实现

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

下面是代码部分:

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

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


package com.b510.txt;

 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;

 /**
  * @author Hongten
  *
  * @time 2011-12-12 2011
  */
 public class MyFile {
     @SuppressWarnings("static-access")
     public static void main(String[] args) {
         MyFile myFile = new MyFile();
         try {
             for (int i = 0; i < 5; i++) {
                 myFile.creatTxtFile("test");
                 myFile.writeTxtFile("显示的是追加的信息" + i);
                 String str = myFile.readDate();
                 System.out.println("*********\n" + str);
             }
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }

     private static String path = "txt/";
     private static String filenameTemp;

     /**
      * 创建文件
      *
      * @throws IOException
      */
     public static boolean creatTxtFile(String name) throws IOException {
         boolean flag = false;
         filenameTemp = path + name + ".txt";
         File filename = new File(filenameTemp);
         if (!filename.exists()) {
             filename.createNewFile();
             flag = true;
         }
         return flag;
     }

     /**
      * 写文件
      *
      * @param newStr
      *            新内容
      * @throws IOException
      */
     public static boolean writeTxtFile(String newStr) throws IOException {
         // 先读取原有文件内容,然后进行写入操作
         boolean flag = false;
         String filein = newStr + "\r\n";
         String temp = "";

         FileInputStream fis = null;
         InputStreamReader isr = null;
         BufferedReader br = null;

         FileOutputStream fos = null;
         PrintWriter pw = null;
         try {
             // 文件路径
             File file = new File(filenameTemp);
             // 将文件读入输入流
             fis = new FileInputStream(file);
             isr = new InputStreamReader(fis);
             br = new BufferedReader(isr);
             StringBuffer buf = new StringBuffer();

             // 保存该文件原有的内容
             for (int j = 1; (temp = br.readLine()) != null; j++) {
                 buf = buf.append(temp);
                 // System.getProperty("line.separator")
                 // 行与行之间的分隔符 相当于“\n”
                 buf = buf.append(System.getProperty("line.separator"));
             }
             buf.append(filein);

             fos = new FileOutputStream(file);
             pw = new PrintWriter(fos);
             pw.write(buf.toString().toCharArray());
             pw.flush();
             flag = true;
         } catch (IOException e1) {
             // TODO 自动生成 catch 块
             throw e1;
         } finally {
             if (pw != null) {
                 pw.close();
             }
             if (fos != null) {
                 fos.close();
             }
             if (br != null) {
                 br.close();
             }
             if (isr != null) {
                 isr.close();
             }
             if (fis != null) {
                 fis.close();
             }
         }
         return flag;
     }

     /**
      * 读取数据
      */
     public void readData1() {
         try {
             FileReader read = new FileReader(filenameTemp);
             BufferedReader br = new BufferedReader(read);
             String row;
             while ((row = br.readLine()) != null) {
                 System.out.println(row);
             }
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

     public String readDate() {
         // 定义一个待返回的空字符串
         String strs = "";
         try {
             FileReader read = new FileReader(new File(filenameTemp));
             StringBuffer sb = new StringBuffer();
             char ch[] = new char[1024];
             int d = read.read(ch);
             while (d != -1) {
                 String str = new String(ch, 0, d);
                 sb.append(str);
                 d = read.read(ch);
             }
             System.out.print(sb.toString());
             String a = sb.toString().replaceAll("@@@@@", ",");
             strs = a.substring(0, a.length() - 1);
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
         return strs;
     }
 }


标签:java,操作txt
0
投稿

猜你喜欢

  • Java通过What、Why、How了解弱引用

    2021-11-01 00:06:20
  • 详解WPF中用户控件和自定义控件的使用

    2023-07-25 12:20:26
  • Android 仿微信发动态九宫格拖拽、删除功能

    2022-08-28 12:51:03
  • java字符串遍历的几种常用方法总结

    2022-08-19 06:36:41
  • Android O添加桌面快捷方式的示例

    2022-12-27 07:53:19
  • java GUI编程之paint绘制操作示例

    2023-11-24 17:58:39
  • c# 递归访问文件夹(删掉歌词文件)

    2022-02-11 02:52:16
  • Java内部类知识汇总

    2023-08-18 14:06:54
  • Spring boot集成Kafka消息中间件代码实例

    2022-11-06 21:53:48
  • Java的Struts框架中<results>标签的使用方法

    2022-04-16 16:08:22
  • Java多线程实现Runnable方式

    2022-06-29 17:09:46
  • 使用游长编码对字符串压缩 Run Length编码示例

    2022-02-18 06:58:51
  • Java爬取豆瓣电影数据的方法详解

    2021-12-12 16:21:06
  • C#与js实现去除textbox文本框里面重复记录的方法

    2022-08-02 03:14:06
  • jar包手动添加到本地maven仓库的步骤详解

    2023-11-23 05:09:37
  • 手写redis@Cacheable注解 参数java对象作为key值详解

    2022-04-26 11:30:36
  • Java编程利用socket多线程访问服务器文件代码示例

    2023-10-11 15:36:37
  • 关于mybatis if else if 条件判断SQL片段表达式取值和拼接问题

    2023-02-04 18:51:21
  • Dwr3.0纯注解(纯Java Code配置)配置与应用浅析二之前端调用后端

    2023-08-19 17:32:33
  • java模拟斗地主发牌功能

    2023-06-26 08:01:34
  • asp之家 软件编程 m.aspxhome.com