当前位置:编程学习 > JSP >>

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

答案:/*
*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++) {
                ent

上一个:给大家一个在SERVER端执行可执行程序的JAVABEAN,呵呵~~~
下一个:SCJP考题中的陷阱

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,