java操作文件

java操作文本文件個人認爲最繁雜的是修改操作!需要重寫整個文本!

public class txtfile {

public void txtfileQ() throws IOException{ //txt文件的查詢
FileReader fr =new FileReader("c://a.txt");
BufferedReader br = new BufferedReader(fr);
String Line=br.readLine();//從文件讀取一行字符串
// 依據讀取到的整行字符串是否不爲空,來判斷是夠到達文件結尾
for (int i=0;Line !=null;i++){
if(Line.indexOf("測試")>-1){ // 使用indexof來實現模糊查詢
System.out.println(Line+"1111111");
}
Line= br.readLine();
}
br.close();//關閉BufferedReader對象
fr.close();//關閉文件
}

//寫txt文件 txtfile.getProperty("txtlist")爲獲得配置文件內的文件路徑
public void txtfileW() throws IOException{
txtfile txtfile = new txtfile();
FileWriter fw=new FileWriter(txtfile.getProperty("txtlist"),true);//建立FileWriter對象,並實例化fw
// 將字符串寫入文件
for (int i=0;i<20;i++){
fw.write("這是本文檔的第"+i+"行"+"\r\n");
}
fw.write("測試耗費時間!");
fw.close();
}

//刪除文件一行操作,可以類比爲修改替換操作 txtfile.getProperty("newline")爲配置文件內定義的回車換行符
public void txtfileD() throws IOException{
String line = "";
txtfile txtfile = new txtfile();
try {
File file = new File(txtfile.getProperty("txtlist"));
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存該行前面的內容
for (int j = 1; (line = br.readLine()) != null
&& !line.equals("這是本文檔的第6行"); j++) {
buf = buf.append(line);
buf = buf.append(txtfile.getProperty("newline"));
}
// 將內容替換爲空
buf = buf.append("");
// 保存該行後面的內容
while ((line = br.readLine()) != null) {
buf = buf.append(line);
buf = buf.append(txtfile.getProperty("newline"));
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

//txt文件增加一行,追加到最後一行
public void txtfileA() throws IOException{
txtfile txtfile = new txtfile();
RandomAccessFile rf=new RandomAccessFile(txtfile.getProperty("txtlist"),"rw");
// 定義一個類RandomAccessFile的對象,並實例化。txtfile.getProperty("txtlist")爲獲得配置文件內的文件路徑
rf.seek(rf.length());
rf.write("追加到文件最後一行!".getBytes());
rf.close();//關閉文件流
}

//讀取配置文件獲得文件路徑
public String getProperty(String propertyname){
String propertyvalue = null;
Properties pro = new Properties ();
try {
FileInputStream fin = new FileInputStream("D://workspace//YNSMIMPL//WebRoot//WEB-INF//txt.properties");
pro.load(fin);
propertyvalue = pro.getProperty(propertyname);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return propertyvalue;
}
/*
* 遍歷文件夾內文件並判斷文件大小!
*/
public String SelectPath(String path){
File dir = new File(path);
File[] tempf=dir.listFiles();
String filenameN="";
if(dir.isDirectory()){
if(tempf.length>1){
for (int n=1;n<tempf.length;n++){
filenameN = path+"\\"+tempf[0].getName();
File a = new File(filenameN);
File b = new File(path+"\\"+tempf[n].getName());
if(a.length()>b.length()){
filenameN = path+"\\"+tempf[n].getName();
}else{
filenameN = path+"\\"+tempf[0].getName();
}
}
}else{
filenameN = path+"\\"+tempf[0].getName();
}
}
System.out.println(filenameN);
return filenameN;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章