posts - 134,  comments - 22,  trackbacks - 0
文件操作类:
package myTest;

import java.io.*;
import java.util.ArrayList;

public class FileOperate {
  
public FileOperate() {
  }

  
/**
   * 新規ディレクトリ(新建文件夹).
   * 
@param folderPath String 例 c:/fqf.
   * 
@author SongJincheng.
   * Create Date:2008/06/12.
   
*/
  
public void newFolder(String folderPath) {
    
try {
      String filePath 
= folderPath;
      filePath 
= filePath.toString();
      java.io.File myFilePath 
= new java.io.File(filePath);
      
if (!myFilePath.exists()) {
        myFilePath.mkdir();
      }
    }
    
catch (Exception e) {
      System.out.println(
"ディレクトリを新規するとき、エラーが発生しました."); // 新建文件夹是出错。
 
      e.printStackTrace();
    }
  }

  
/**
   * 规定编码格式,新規ファイル(新建文件).
   * 
@param filePathAndName String ファイルのパスとファイル名 例c:/fqf.txt           //文件路径.
   * 
@param fileContent String ファイル内容.   //文件内容
   * 
@author SongJincheng.
   * Create Date :2008/06/12.
   
*/
  
public void newFile(String filePathAndName, String fileContent) {

    
try {
      String filePath 
= filePathAndName;
      filePath 
= filePath.toString();
      File myFilePath 
= new File(filePath);
      
if (!myFilePath.exists()) {
        myFilePath.createNewFile();
      }

      String strContent 
= fileContent;
      Writer   out   
=   new   BufferedWriter(new   OutputStreamWriter(new   FileOutputStream(filePathAndName),   "EUC_JP")); 
      out.write(strContent);
      out.close();
   

    }
    
catch (Exception e) {
      System.out.println(
"ファイルを新規する時、エラーが発生しました.");   //创建文件是出错。
      e.printStackTrace();

    }

  }
  
//不规定编码格式
  public void newFile1(String filePathAndName, String fileContent) {

        
try {
          String filePath 
= filePathAndName;
          filePath 
= filePath.toString();
          File myFilePath 
= new File(filePath);
          
if (!myFilePath.exists()) {
            myFilePath.createNewFile();
          }
          FileWriter resultFile 
= new FileWriter(myFilePath,true);
          PrintWriter myFile 
= new PrintWriter(resultFile);
          String strContent 
= fileContent;
          myFile.print(strContent);         
          resultFile.close();

        }
        
catch (Exception e) {
          System.out.println(
"ファイルを新規する時、エラーが発生しました.");
          e.printStackTrace();

        }

      }
      
  
  
  
  
  
/**
   * 新規ファイル 、新規あるいは内容追加を指定できる  //(新建文件 可以选择新建或是追加模式).
   * 
@param filePathAndName String ファイルのパスとファイル名 例c:/fqf.txt.  //文件路径  
   * 
@param arrayList ArrayList<String[]> ファイル内容リスト.    //文件内容
   * 
@param addFlag Boolean テキスト追加の形で処理するフラグ true:ファイルの最後に追加,false:上書き.    //新建,追加标志 true为追加模式
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void newFile(String filePathAndName, ArrayList<String> arrayList,Boolean addFlag) {

    
try {
      String filePath 
= filePathAndName;
      filePath 
= filePath.toString();
      File myFilePath 
= new File(filePath);
      
if (!myFilePath.exists()) {
        myFilePath.createNewFile();
      }     
     FileWriter resultFile 
= new FileWriter(myFilePath,addFlag); //テキスト追加の形で処理する.
     
      
      PrintWriter myFile 
= new PrintWriter(resultFile);
      
      
if (arrayList != null) {
      
          ArrayList
<String> contentList = arrayList;
          
          
for(int i = 0 ; i<contentList.size();i++) { 
              StringBuffer contents 
= new StringBuffer();
              contents 
= contents.append(contentList.get(i));
              myFile.print(contents.toString());
          }
          resultFile.close();
      }
      
    }
    
catch (Exception e) {
      System.out.println(
"ファイルを新規する時、エラーが発生しました.");
      e.printStackTrace();

    }

  }   

  
  
/**
   * ファイル内容を追加する.
   * 
@param filePathAndName String ファイルのパスとファイル名 例c:/fqf.txt
   * 
@param fileContent String ファイル内容.
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void addFileContents(String filePathAndName, String fileContent) {

    
try {
      String filePath 
= filePathAndName;
      filePath 
= filePath.toString();
      File myFilePath 
= new File(filePath);
      
if (!myFilePath.exists()) {
          myFilePath.createNewFile();
         
      }
      RandomAccessFile rf
=new RandomAccessFile(filePath,"rw");
      
      rf.seek(rf.length());
//pointは最後になる.
      rf.writeBytes(fileContent);     
      rf.close();
     
    }
    
catch (Exception e) {
      System.out.println(
"ファイル内容を追加する時、エラーが発生しました.");
      e.printStackTrace();

    }

  }

  
/**
   * ファイルを削除    //文件删除
   * 
@param filePathAndName String ファイルのパスとファイル名 例 c:/fqf.txt
   * 
@param fileContent String
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void delFile(String filePathAndName) {
    
try {
      String filePath 
= filePathAndName;
      filePath 
= filePath.toString();
      java.io.File myDelFile 
= new java.io.File(filePath);
      myDelFile.delete();

    }
    
catch (Exception e) {
      System.out.println(
"ファイルを削除する時、エラーが発生しました.");
      e.printStackTrace();

    }

  }

  
/**
   * フォルダを削除.    //文件夹删除
   * 
@param filePathAndName String ファイルのパスとファイル名 例 c:/fqf.     //文件夹路径
   * 
@param fileContent String フォルダパス.
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void delFolder(String folderPath) {
    
try {
      delAllFile(folderPath); 
//全部の内容を削除する.    文件夹内所有内容删除
      String filePath = folderPath;
      filePath 
= filePath.toString();
      java.io.File myFilePath 
= new java.io.File(filePath);
      myFilePath.delete(); 
//空フォルダを削除     //删除空文件夹

    }
    
catch (Exception e) {
      System.out.println(
"フォルダを削除する時、エラーが発生しました.");
      e.printStackTrace();

    }

  }

  
/**
   * フォルダ中のファイルを全部削除する.   文件夹中的内容全部删除
   * 
@param path String フォルダパス 例 c:/fqf.       //文件路径
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void delAllFile(String path) {
    File file 
= new File(path);
    
if (!file.exists()) {
      
return;
    }
    
if (!file.isDirectory()) {
      
return;
    }
    String[] tempList 
= file.list();
    File temp 
= null;
    
for (int i = 0; i < tempList.length; i++) {
      
if (path.endsWith(File.separator)) {
        temp 
= new File(path + tempList[i]);
      }
      
else {
        temp 
= new File(path + File.separator + tempList[i]);
      }
      
if (temp.isFile()) {
        temp.delete();
      }
      
if (temp.isDirectory()) {
        delAllFile(path
+"/"+ tempList[i]); //まず、ファイルを削除する      首先删除文件
        delFolder(path+"/"+ tempList[i]); //後は、フォルダを削除する     删除文件夹
      }
    }
  }

  
/**
   * 一つファイルをコピーする.  复制一个文件
   * 
@param oldPath String 元ファイルのパス 例 c:/fqf.txt.   源文件路径
   * 
@param newPath String コピー先 例 f:/fqf.txt.               目标文件路径
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void copyFile(String oldPath, String newPath) {
    
try {
      
int bytesum = 0;
      
int byteread = 0;
      File oldfile 
= new File(oldPath);
      
if (oldfile.exists()) { //ファイルが存在するとき
        InputStream inStream = new FileInputStream(oldPath); //元ファイルを読み込む
        FileOutputStream fs = new FileOutputStream(newPath);
        
byte[] buffer = new byte[1444];
        
//int length;
        while ( (byteread = inStream.read(buffer)) != -1) {
          bytesum 
+= byteread; //ファイルのサイズ
          System.out.println(bytesum);
          fs.write(buffer, 
0, byteread);
        }
        inStream.close();
      }
    }
    
catch (Exception e) {
      System.out.println(
"エラーが発生しました.");
      e.printStackTrace();

    }

  }

  
/**
   * フォルダをコピーする.   文件夹复制
   * 
@param oldPath String 元ファイルのパス 例 c:/fqf     源文件夹路径
   * 
@param newPath String コピー先 例 f:/fqf/ff             目标路径
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void copyFolder(String oldPath, String newPath) {

    
try {
      (
new File(newPath)).mkdirs(); //フォルダがない場合は、新規する   如果没有文件夹就新建
      File a=new File(oldPath);
      String[] file
=a.list();
      File temp
=null;
      
for (int i = 0; i < file.length; i++) {
        
if(oldPath.endsWith(File.separator)){
          temp
=new File(oldPath+file[i]);
        }
        
else{
          temp
=new File(oldPath+File.separator+file[i]);
        }

        
if(temp.isFile()){
          FileInputStream input 
= new FileInputStream(temp);
          FileOutputStream output 
= new FileOutputStream(newPath + "/" +
              (temp.getName()).toString());
          
byte[] b = new byte[1024 * 5];
          
int len;
          
while ( (len = input.read(b)) != -1) {
            output.write(b, 
0, len);
          }
          output.flush();
          output.close();
          input.close();
        }
        
if(temp.isDirectory()){//サブフォルダの場合   有子文件夹的情况
          copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
        }
      }
    }
    
catch (Exception e) {
      System.out.println(
"フォルダをコピーする時、エラーが発生しました.");
      e.printStackTrace();

    }

  }

  
/**
   * ファイルを移動する.           移动文件夹
   * 
@param oldPath String 例 c:/fqf.txt.
   * 
@param newPath String 例 d:/fqf.txt.
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void moveFile(String oldPath, String newPath) {
    copyFile(oldPath, newPath);
    delFile(oldPath);

  }

  
/**
   * 指定したところに移動する.  指定地点移动文件夹
   * 
@param oldPath String 例 c:/fqf.txt.
   * 
@param newPath String 例 d:/fqf.txt.
   * 
@author SongJingcheng.
   * Create Date :2008/06/12.
   
*/
  
public void moveFolder(String oldPath, String newPath) {
    copyFolder(oldPath, newPath);
    delFolder(oldPath);

  }
}

读取文件常用方法:
File myfile = new File("c:/test.txt");             
            BufferedReader reader
= new BufferedReader(new InputStreamReader(
                    
new FileInputStream(fl_result_knp),"UTF-8"));            
            String line
= "";
            
while( (line = reader.readLine())!=null){
             
System.out.println(line);
            }





posted on 2008-07-29 13:05 TRE-China R&D 阅读(281) 评论(0)  编辑 收藏 引用 所属分类: JAVA
只有注册用户登录后才能发表评论。