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