• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 9次 | 进入软件测试论坛讨论

领测软件测试网 /*
*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本身的流操作非常完美。前面几个方法并不需要。
而且你的类写的不合适。后面几个方法应该用静态方法。  

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网