?? fileope.java
字號(hào):
package cn.javass.bookmgr.util.fileutil;
import java.io.*;
import java.util.*;
/**
* 文件操作的工具類,主要實(shí)現(xiàn)文件的讀寫和刪除
*
* <p>Title: Java私塾第一個(gè)Java項(xiàng)目——圖書進(jìn)銷存系統(tǒng)(單機(jī)版)</p>
* <p>Description: 網(wǎng)址:<a href="http://www.javass.cn">http://www.javass.cn</a>
* 新電話:010-86835215 新地址:北京市海淀區(qū)廠洼路5號(hào)院深博達(dá)商務(wù)樓5層</p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: Java私塾</p>
* @author Java私塾
* @version 1.0
*/
public class FileOpe{
/**
* 如果文件存在,那么刪除文件
* @param fileName 文件路徑和名稱
* @throws Exception
*/
public static void deleteFile(String fileName)throws Exception{
if(f.exists()){
f.delete();
}
}
/**
* 把傳入的內(nèi)容輸出到指定的文件中去
* @param fileName 文件路徑和名稱
* @param str 要輸出的內(nèi)容
* @throws Exception
*/
public static void writeFile(String fileName,String str)throws Exception{
RandomAccessFile rf = new RandomAccessFile(fileName,"rw");
rf.write(str.getBytes("gb2312"));
rf.close();
}
/**
* 讀取指定的文件,把其中的內(nèi)容按行取出來(lái),組成一個(gè)集合
* @param fileName 文件路徑和名稱
* @return 集合,包含多個(gè)String,一個(gè)String是文件中的一行內(nèi)容
* @throws Exception
*/
public static Collection readFile(String fileName)throws Exception{
Collection col = new ArrayList();
try{
RandomAccessFile rf = new RandomAccessFile(fileName, "r");
long length = rf.length();
long pointer = 0L;
while (pointer < length) {
String str = rf.readLine();
str = new String(str.getBytes("iso8859-1"), "gb2312");
col.add(str);
pointer = rf.getFilePointer();
}
rf.close();
}catch(Exception err){
//
}
return col;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -