再给大家一个进行IO操作的JAVABEAN,吐血奉献哦~~~~搜集和调试了很久,没有问题哦~~~~

发表于:2007-07-04来源:作者:点击数: 标签:
/* *Author:tyfun *DateTime:2002.12.19 *Package:com.westarsoft.io */ package com.westarsoft.io; import java .io.*; public class FileOperation { private static String str = new String(); private static String fileName = new String(); private
/*
*Author:tyfun
*DateTime:2002.12.19
*Package:com.westarsoft.io
*/

package com.westarsoft.io;

import java.io.*;

public class FileOperation {
    private static String str = new String();
    private static String fileName = new String();
    private static String filePath = new String();
    private static String text = new String();
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public String getFilePath() {
        return filePath;
    }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    /*
     *USE:
     *setStr(String)
     *setFileName(String)
     */
    public void writeFile() {
        try {
            PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
            pw.println(str);
            pw.close();
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
    /*
     *USE:
     *readFile(String)
     */    
    public String readFile(String fileName) {
        String record = "";
        String readFile = "";
        int recCount = 0;
        try {
            FileReader fr = new FileReader(fileName);
               BufferedReader br = new BufferedReader(fr);
               record = new String();
               while ((record = br.readLine()) != null) {
                 //recCount++;
                 //System.out.println(recCount + ": " + record);
                 readFile = readFile+"<br>"+record;
                 //readFile = readFile+record;
            }
            br.close();
            fr.close();
         }
         catch (IOException e) {
            System.out.println("Uh oh, got an IOException error!");
             e.printStackTrace();
        }
        return readFile;
    }
    /*
     *USE:
     *copyFile(String,String)
     */
     public void copyFile(String from,String to) {
        File fromFile,toFile;
        fromFile = new File(from);
        toFile = new File(to);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            toFile.createNewFile();
              fis = new FileInputStream(fromFile);
              fos = new FileOutputStream(toFile);
              int bytesRead;
              byte[] buf = new byte[4 * 1024];
              while((bytesRead=fis.read(buf))!=-1) {
                fos.write(buf,0,bytesRead);
              }
              fos.flush();
              fos.close();
              fis.close();
        }
        catch(Exception e) {
              System.out.println(e);
        }
    }
    /*
     *USE:
     *emptyDir()
     */
    public void emptyDir(String directory) {
        File path = new File(directory);
        if(path.isDirectory()) {
            File[] entries = path.listFiles( );
            for(int i=0; i<entries.length; i++) {
                entries[i].delete( );
            }
        }
    }
    /*
     *USE:
     *deleteDirectory()
     */
    public void delDir(File directory) {
        if(directory.isDirectory()) {
            try {
                File[] entries = directory.listFiles( );
                for(int i=0; i<entries.length; i++) {
                    if(entries[i].isDirectory()) {
                        delDir(entries[i]);
                    }
                    else {
                        entries[i].delete( );
                    }
                }
                directory.delete();
            }
            catch(Exception e) {
                System.out.println(e);
            }
        }
    }
    /*
     *USE:
     *setFilePath(String)
     *setText(String)
     */
    public static boolean fullTextSearch(){
        try {
            File file = new File(filePath);
            long fileLength = 0;
            fileLength = file.length();
            FileInputStream fis = new FileInputStream(filePath);
            byte[] buf = new byte[(int)fileLength];
            int bytesRead = 0;
            StringBuffer sbfFile = new StringBuffer();
            StringBuffer sbfText = new StringBuffer();
            String src = "";
            String search = "";
            while((bytesRead = fis.read(buf)) != -1){
                for (int i = 0; i < buf.length; i++) {
                    sbfFile = sbfFile.append(buf[i]);
                }
            }
            src = sbfFile.toString();
            byte[] bufText = new byte[text.length()];
            bufText = text.getBytes();
            for (int j = 0; j < bufText.length; j++) {
                sbfText = sbfText.append(bufText[j]);
            }
            search = sbfText.toString();
            if(src.indexOf(search)>0){
                return true;
            }
            else{
                return false;
            }
        }
        catch (Exception ex) {
            return false;
        }
    }
}

[版主注释]
sonymusic 于 2003-1-14 11:13:21 加贴在 Java程序设计 ←返回版面         

其实Java本身的流操作非常完美。前面几个方法并不需要。
而且你的类写的不合适。后面几个方法应该用静态方法。  

原文转自:http://www.ltesting.net