java開發(fā)之讀寫txt文件操作的實現(xiàn)
項目結(jié)構(gòu):
運行效果:
========================================================
下面是代碼部分:
========================================================
/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)容,然后進行寫入操作
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)多線程環(huán)境下的Map操作。它支持高并發(fā)的讀寫操作,通過分段鎖的方式實現(xiàn)線程安全,同時提供了一些高級功能,比如迭代器弱一致性和批量操作等。ConcurrentHashMap在高并發(fā)場景中具有重要的應(yīng)用價值2023-04-04spring cloud zuul 與 sentinel的結(jié)合使用操作
這篇文章主要介紹了spring cloud zuul 與 sentinel 的結(jié)合使用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java 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-12Spring如何動態(tài)自定義logback日志目錄詳解
這篇文章主要給大家介紹了關(guān)于Spring如何動態(tài)自定義logback日志目錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10使用 Spring Boot 實現(xiàn) WebSocket實時通信
本篇文章主要介紹了使用 Spring Boot 實現(xiàn) WebSocket實時通信,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10