標籤:public ace 提高效率 個人 des efi 函數 ade str
Java關於檔案操作
最近工作中遇到的問題涉及到很多的檔案操作,包括檔案的讀寫,在目前的目錄,任意目錄的檔案建立,目錄建立。涉及到檔案的建立要求當父級目錄不存在時建立父級目錄。
- 建立目錄
有兩個函數,mkdir(),mkdirs();經過測試,發現mkdir()只適用於建立父級目錄存在的情況,當父級目錄不存在的時候,無法建立檔案夾。而mkdirs()當父級目錄不存在的時候依次建立父級目錄再建立檔案夾。代碼如下;
public boolean CreatDir(String dirpath){
File dir = new File(dirpath);
if (dir.exists()) {
System.out.println("建立目錄" + dirpath + "失敗,目標目錄已經存在");
return false;
}
//添加分隔字元,也就是斜杠
if (!dirpath.endsWith(File.separator)) {
dirpath = dirpath + File.separator;
}
//建立目錄
if (dir.mkdirs()) {
System.out.println("建立目錄" + dirpath + "成功!");
return true;
} else {
System.out.println("建立目錄" + dirpath + "失敗!");
return false;
}
}
由於提供的dirpath很多情況下不知道父目錄是否存在,個人比較喜歡使用mkdirs()來創檔案夾。
2. 建立單個檔案
建立檔案時先判斷父級目錄是否存在,不存在則建立父級目錄再建立檔案,代碼如下;
public boolean CreateFile(String filepath){
File file = new File(filepath);
if(file.exists()){
System.out.println("建立檔案"+filepath+"失敗"+", 檔案已存在");
return false;
}
if(filepath.endsWith(File.separator)){
System.out.println("建立檔案失敗"+",建立目標是目錄!");
return false;
}
//檢查父級目錄是否存在,不存在就建立
if(!file.getParentFile().exists()){
System.out.println("目標檔案的父級目錄不存在,準備建立父級目錄");
if(!file.getParentFile().mkdirs()){
return false;
}
}
//建立檔案
try{
if(file.createNewFile()){
System.out.println("建立檔案"+filepath+"成功");
return true;
} else {
System.out.println("建立檔案"+filepath+"失敗");
return false;
}
}catch (IOException e){
e.printStackTrace();
System.out.println("建立檔案"+filepath+"失敗"+e.getMessage());
return false;
}
}
3.讀檔案
- 檔案的讀操作,使用帶緩衝的位元組流,以位元組的形式讀取檔案,並寫到參數指定的輸出資料流out,
- 引用如下部落格:http://www.cnblogs.com/coderworld/p/java-io-file.html#_label2
- 參考部落格:http://www.cnblogs.com/stevendes/p/5483884.html
- 代碼如下:按字元讀檔案,使用BufferedReader提高效率
public String ReadFileBychars(String filename){
File file = new File(filename);
String content = "";
//判斷檔案是否存在
if(!file.exists()||!file.isFile()){
return "";
}
FileReader fir = null;
BufferedReader bir = null;
try{
fir = new FileReader(file);
bir = new BufferedReader(fir);
String temp = "";
while((temp=bir.readLine())!=null){
content = content + temp;
}
} catch (IOException e){
e.printStackTrace();
}finally {
try{
bir.close();
fir.close();
}catch (IOException e){
e.printStackTrace();
}
}
return content;
}
- 按位元組讀檔案,每次讀一個位元組,將檔案內容返回,代碼如下:
- 參考部落格:http://www.cnblogs.com/stevendes/p/5483884.html
public String ReadFileByBytes(String filename){
File file = new File(filename);
String content = "";
if(!file.exists()||!file.isFile()){
return new String();
}
FileInputStream fis = null;
try{
fis= new FileInputStream(file);
int size = fis.available();
for(int i=0; i<size; i++){
content = content + (char)fis.read();
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
return content;
}
4.寫檔案
- 檔案的寫操作,使用帶緩衝的位元組流,從參數指定的輸入資料流in讀取,並以位元組形式寫到目標檔案。
- 引用如下部落格:http://www.cnblogs.com/coderworld/p/java-io-file.html#_label2
- 參考部落格:http://www.cnblogs.com/stevendes/p/5483884.html
- 由於工作需要,在寫檔案時,需要傳入內容寫入檔案。代碼如下:
public boolean writeFileByFileWriter(String filePath, String content) {
File file = new File(filePath);
synchronized (file) {
FileWriter fw = null;
try {
fw = new FileWriter(filePath);
fw.write(content);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try{
fw.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
java檔案操作