package common.fileOperate;
import java.io.*;
public class FileRw {
public FileRw() {
}
/**
* 讀取文件filePath中一行的數據,并返回這個(gè)數據
*
* @param filePath
* @return
* @throws FileNotFoundException
*/
public String ReadFileOneLine(String filePath) throws FileNotFoundException {
String currentRecord = null;// 保存文本的變量
// 創(chuàng )建新的BufferedReader對象
BufferedReader file = new BufferedReader(new FileReader(filePath));
String returnStr = null;
try {
// 讀取一行數據并保存到currentRecord變量中
currentRecord = file.readLine();
} catch (IOException e) {// 錯誤處理
System.out.println("讀取數據錯誤.");
}
if (currentRecord == null)
// 如果文件為空
returnStr = "沒(méi)有任何記錄";
else {// 文件不為空
returnStr = currentRecord;
}
// 返回讀取文件的數據
return returnStr;
}
/**
* 讀取文件中的所有內容
*
* @param filePath
* @return
* @throws FileNotFoundException
*/
public String ReadFile(String filePath) throws Exception {
//String picturefolderurl=readpro.prop.getProperty("picturefolderurl");
File file = new File(filePath);
BufferedReader reader = null;
String laststr = "";
try {
System.out.println("以行為單位讀取文件內容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次讀入一行,直到讀入null為文件結束
while ((tempString = reader.readLine()) != null) {
//顯示行號
System.out.println("line " + line + ": " + tempString);
laststr = laststr + tempString;
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return laststr;
}
/**
* 寫(xiě)文件操作 寫(xiě)為一行
*
* @param filePath
* 文件路徑
* @param tempcon
* 寫(xiě)入的內容
* @throws FileNotFoundException
*/
public void WriteFile(String filePath, String tempcon)
throws FileNotFoundException {
try {
// 創(chuàng )建PrintWriter對象,用于寫(xiě)入數據到文件中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
// 用文本格式打印整數Writestr
pw.println(tempcon);
// 清除PrintWriter對象
pw.close();
} catch (IOException e) {
// 錯誤處理
System.out.println("寫(xiě)入文件錯誤" + e.getMessage());
}
}
/**
* 文件的寫(xiě)入 將要分行的數組以數組的形式傳入
*
* @param filePath(文件路徑)
* @param fileName(文件名)
* @param args[]
* @throws IOException
*/
public void writeFile(String filePath, String[] args) throws IOException {
FileWriter fw = new FileWriter(filePath);
PrintWriter out = new PrintWriter(fw);
for (int i = 0; i < args.length; i++) {
out.write(args[i]);
out.println();
}
fw.close();
out.close();
}
/**
* 判斷文件是否存在
*
* @return
*/
public boolean IsFileExists(String filePath) {
File f = new File(filePath);
if (f.exists()) {// 檢查File.txt是否存在
return true;
} else {
return false;
}
}
/**
* 創(chuàng )建新文件
*
* @param filePath
* @return
*/
public boolean CreateFile(String filePath) {
boolean flag = true;
File f = new File(filePath);
if (f.exists()) {// 檢查File.txt是否存在
f.delete();
try {
f.createNewFile();
} catch (IOException e) {
flag = false;
}
} else {
try {
f.createNewFile();
} catch (IOException e) {
flag = false;
}
}
return false;
}
/**
* 把要寫(xiě)入的字符串寫(xiě)到文件里面
* @param str 要寫(xiě)入的內容
* @param destFilePath 目標文件地址
* @throws Exception
*/
public static void writefilewithmqhxhtm(String str, String destFilePath)
throws Exception {
File temp = new File(destFilePath);
DataOutputStream outs = new DataOutputStream(new FileOutputStream(temp));
outs.write(str.getBytes());
outs.close();
}
/* 下面這一般你可以用來(lái)測試java應用程序來(lái)讀取文件,將前面的"http://"去掉后你可以運行:java FileRw 來(lái)測試。 */
public static void main(String args[]) throws Exception {
FileRw fm = new FileRw();
String filepath = "c:/test.txt";
System.out.println(fm.IsFileExists(filepath));
System.out.println(fm.ReadFile(filepath));
if (!fm.IsFileExists(filepath)) {
fm.CreateFile(filepath);
// fm.WriteFile(filepath, "asf");
// System.out.println(fm.ReadFileOneLine(filepath));
String[] str = new String[3];
str[0] = "0=0";
str[1] = "1=1";
str[2] = "2=2";
try {
fm.writeFile(filepath, str);
} catch (IOException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
}
}
}
}
聯(lián)系客服