亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java開發(fā)之讀寫txt文件操作的實現(xiàn)

 更新時間:2013年05月02日 16:59:29   作者:  
本篇文章介紹了,java開發(fā)之讀寫txt文件操作的實現(xiàn)。需要的朋友參考下

項目結(jié)構(gòu):

運行效果:

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

下面是代碼部分:

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

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

復(fù)制代碼 代碼如下:

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;

     /**
      * 創(chuàng)建文件
      *
      * @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
      *            新內(nèi)容
      * @throws IOException
      */
     public static boolean writeTxtFile(String newStr) throws IOException {
         // 先讀取原有文件內(nèi)容,然后進行寫入操作
         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();

             // 保存該文件原有的內(nèi)容
             for (int j = 1; (temp = br.readLine()) != null; j++) {
                 buf = buf.append(temp);
                 // System.getProperty("line.separator")
                 // 行與行之間的分隔符 相當(dāng)于“\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;
     }

     /**
      * 讀取數(shù)據(jù)
      */
     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;
     }
 }

相關(guān)文章

  • Java中使用ConcurrentHashMap實現(xiàn)線程安全的Map

    Java中使用ConcurrentHashMap實現(xiàn)線程安全的Map

    在Java中,ConcurrentHashMap是一種線程安全的哈希表,可用于實現(xiàn)多線程環(huán)境下的Map操作。它支持高并發(fā)的讀寫操作,通過分段鎖的方式實現(xiàn)線程安全,同時提供了一些高級功能,比如迭代器弱一致性和批量操作等。ConcurrentHashMap在高并發(fā)場景中具有重要的應(yīng)用價值
    2023-04-04
  • spring cloud zuul 與 sentinel的結(jié)合使用操作

    spring cloud zuul 與 sentinel的結(jié)合使用操作

    這篇文章主要介紹了spring cloud zuul 與 sentinel 的結(jié)合使用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • jdk11?jdk17多版本共存切換方式

    jdk11?jdk17多版本共存切換方式

    這篇文章主要介紹了jdk11?jdk17多版本共存切換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Java中對于雙屬性枚舉的使用案例

    Java中對于雙屬性枚舉的使用案例

    今天小編就為大家分享一篇關(guān)于Java中對于雙屬性枚舉的使用案例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java TimeoutException:服務(wù)調(diào)用超時異常的正確解決方案

    Java TimeoutException:服務(wù)調(diào)用超時異常的正確解決方案

    在現(xiàn)代軟件開發(fā)中,服務(wù)間通信是構(gòu)建分布式系統(tǒng)的基礎(chǔ),然而,網(wǎng)絡(luò)延遲、服務(wù)負(fù)載、資源競爭等因素都可能導(dǎo)致服務(wù)調(diào)用超時,TimeoutException是Java中表示服務(wù)調(diào)用超時的常見異常之一,本文將探討TimeoutException的成因及解決方案,需要的朋友可以參考下
    2024-12-12
  • SpringBoot配置和切換Tomcat流程詳解

    SpringBoot配置和切換Tomcat流程詳解

    這篇文章主要介紹了如何給springboot配置和切換默認(rèn)的Tomcat容器以及相關(guān)的經(jīng)驗技巧,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Spring如何動態(tài)自定義logback日志目錄詳解

    Spring如何動態(tài)自定義logback日志目錄詳解

    這篇文章主要給大家介紹了關(guān)于Spring如何動態(tài)自定義logback日志目錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • java 實現(xiàn)鏈棧存儲的方法

    java 實現(xiàn)鏈棧存儲的方法

    下面小編就為大家?guī)硪黄猨ava 實現(xiàn)鏈棧存儲的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Springboot 全局時間格式化三種方式示例詳解

    Springboot 全局時間格式化三種方式示例詳解

    時間格式化在項目中使用頻率是非常高的,當(dāng)我們的 API? 接口返回結(jié)果,需要對其中某一個 date? 字段屬性進行特殊的格式化處理,通常會用到 SimpleDateFormat? 工具處理,這篇文章主要介紹了3 種 Springboot 全局時間格式化方式,需要的朋友可以參考下
    2024-01-01
  • 使用 Spring Boot 實現(xiàn) WebSocket實時通信

    使用 Spring Boot 實現(xiàn) WebSocket實時通信

    本篇文章主要介紹了使用 Spring Boot 實現(xiàn) WebSocket實時通信,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10

最新評論