java開發(fā)之讀寫txt文件操作的實(shí)現(xiàn)
項(xiàng)目結(jié)構(gòu):

運(yùn)行效果:

========================================================
下面是代碼部分:
========================================================
/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;
/**
* 創(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)容,然后進(jìn)行寫入操作
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 自動(dòng)生成 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() {
// 定義一個(gè)待返回的空字符串
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實(shí)現(xiàn)線程安全的Map
在Java中,ConcurrentHashMap是一種線程安全的哈希表,可用于實(shí)現(xiàn)多線程環(huán)境下的Map操作。它支持高并發(fā)的讀寫操作,通過分段鎖的方式實(shí)現(xiàn)線程安全,同時(shí)提供了一些高級(jí)功能,比如迭代器弱一致性和批量操作等。ConcurrentHashMap在高并發(fā)場景中具有重要的應(yīng)用價(jià)值2023-04-04
spring cloud zuul 與 sentinel的結(jié)合使用操作
這篇文章主要介紹了spring cloud zuul 與 sentinel 的結(jié)合使用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java TimeoutException:服務(wù)調(diào)用超時(shí)異常的正確解決方案
在現(xiàn)代軟件開發(fā)中,服務(wù)間通信是構(gòu)建分布式系統(tǒng)的基礎(chǔ),然而,網(wǎng)絡(luò)延遲、服務(wù)負(fù)載、資源競爭等因素都可能導(dǎo)致服務(wù)調(diào)用超時(shí),TimeoutException是Java中表示服務(wù)調(diào)用超時(shí)的常見異常之一,本文將探討TimeoutException的成因及解決方案,需要的朋友可以參考下2024-12-12
Spring如何動(dòng)態(tài)自定義logback日志目錄詳解
這篇文章主要給大家介紹了關(guān)于Spring如何動(dòng)態(tài)自定義logback日志目錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
java 實(shí)現(xiàn)鏈棧存儲(chǔ)的方法
下面小編就為大家?guī)硪黄猨ava 實(shí)現(xiàn)鏈棧存儲(chǔ)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Springboot 全局時(shí)間格式化三種方式示例詳解
時(shí)間格式化在項(xiàng)目中使用頻率是非常高的,當(dāng)我們的 API? 接口返回結(jié)果,需要對(duì)其中某一個(gè) date? 字段屬性進(jìn)行特殊的格式化處理,通常會(huì)用到 SimpleDateFormat? 工具處理,這篇文章主要介紹了3 種 Springboot 全局時(shí)間格式化方式,需要的朋友可以參考下2024-01-01
使用 Spring Boot 實(shí)現(xiàn) WebSocket實(shí)時(shí)通信
本篇文章主要介紹了使用 Spring Boot 實(shí)現(xiàn) WebSocket實(shí)時(shí)通信,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

