package com.zjt.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;public abstract class ReadFile {
protected String fileName = null;

public ReadFile(String fileName){
this.fileName = fileName;

}
public ReadFile(){

}
/**
 * 根据文件名取文件信息
 * @param name 文件名
 * @return 文件内容
 * @throws Exception
 */
public String read(String name) throws Exception{
File file = new File(name);
FileReader read = new FileReader(file);
BufferedReader buf = new BufferedReader(read);
StringBuffer text = new StringBuffer();
String str = null;
str = buf.readLine();

while(str!=null){

text.append(str);
str = buf.readLine();
}
buf.close();
read.close();
return text.toString();
}

public String read() throws Exception{ 
return this.read(fileName);
}
/**
 * 将字符串text写入name文件中 
 * @param name 文件名
 * @param isReplace  是否替换原文件,false 为追加
 * @param text  要写入的内容
 * @throws Exception
 */
public void write(String name,boolean isReplace,String text) throws Exception{
File file = new File(name);
FileWriter write = new FileWriter(file,!isReplace);
BufferedWriter buf = new BufferedWriter(write);
buf.write(text);
buf.close();
write.close();
}
public void write(boolean isReplace,String text)throws Exception{
this.write(fileName,isReplace, text);
}
}------------------------------------------------
package com.zjt.file;import com.zjt.util.ReadFile;public class FileDispose extends ReadFile{

private String text;
public FileDispose(String name) {
super(name);

}
/**
 * 对字符串进行插入更新
 * 例:strInsert("abc","*",2); 返回结果 ab*c
 * @param file 被更新的字符串
 * @param str  更新字符串
 * @param charNumber 间隔几个字符
 * @return 更新后的字符串
 */
public String strInsert(String file,String  str,int charNumber) {
return this.insert(file, str, 0, charNumber);
}
/**
 * 对文件进行更新
 * @param str 更新字符串
 * @param charNumber 间隔几个字符
 * @throws Exception
 */
public void strInsert(String str,int charNumber)throws Exception{

this.text = read();

this.write(true, this.insert(text, str, 0, charNumber));
}
/**
 * 由beginNumber开始,endNumber结束 把字符串file中
 * 插入字符串 str
 * 例 insert("abc","*",0,1) 返回字符串为 a*b*c 代表由第○个字符开始,第隔3个字符插入一个*
 * @param file 被更新字符串
 * @param str  更新字符串
 * @param beginNumber 从第几位开始更新
 * @param endNumber 第几位结束更新
 * @return
 */
protected String insert(String file,String str,int beginNumber,int endNumber){
if(endNumber>=file.length())return file.substring(beginNumber,file.length());
else{
return file.substring(beginNumber,endNumber)+str+insert(file,str,endNumber,(endNumber*2-beginNumber>file.length())
?endNumber=file.length()
:endNumber*2-beginNumber);
}
}
public static void main(String[] args) {
try {
new FileDispose("f:/a.txt").strInsert("\r\n", 20);
} catch (Exception e) {
e.printStackTrace();
}

}
}

--------------------------------------------------
strInsert 方法主要是用于把一个字符串按固定的规律插入文件中  如:文件内容为 abcdefg 插入字符串为× 间隔字符串2则strInsert(fileName,"*",2) 修改为后的文件内容为 ab*cd*ef*g
可是如果文件太大的情况下会出现栈溢出的异常
哪位高手能帮忙解决一下?????

解决方案 »

  1.   

    package com.zjt.util; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter; public abstract class ReadFile {    protected String fileName = null;        public ReadFile(String fileName){       this.fileName = fileName;           }    public ReadFile(){           }    /**     * 根据文件名取文件信息     * @param name 文件名     * @return 文件内容     * @throws Exception     */    public String read(String name) throws Exception{       File file = new File(name);       FileReader read = new FileReader(file);       BufferedReader buf = new BufferedReader(read);       StringBuffer text = new StringBuffer();       String str = null;       str = buf.readLine();              while(str!=null){                      text.append(str);           str = buf.readLine();       }       buf.close();       read.close();       return text.toString();    }        public String read() throws Exception{        return this.read(fileName);    }    /**     * 将字符串text写入name文件中      * @param name 文件名     * @param isReplace  是否替换原文件,false 为追加     * @param text  要写入的内容     * @throws Exception     */    public void write(String name,boolean isReplace,String text) throws Exception{       File file = new File(name);       FileWriter write = new FileWriter(file,!isReplace);       BufferedWriter buf = new BufferedWriter(write);       buf.write(text);       buf.close();       write.close();    }    public void write(boolean isReplace,String text)throws Exception{       this.write(fileName,isReplace, text);    }}  package com.zjt.file; import java.util.Vector; import com.zjt.util.ReadFile; public class FileDispose extends ReadFile{        private String text;    public FileDispose(String name) {       super(name);           }    /**     * 对字符串进行插入更新     * 例:strInsert("abc","*",2); 返回结果 ab*c     * @param file 被更新的字符串     * @param str  更新字符串     * @param charNumber 间隔几个字符     * @return 更新后的字符串     */    public String strInsert(String file,String  str,int charNumber) {       return this.insert(file, str, 0, charNumber);    }    /**     * 对文件进行更新     * @param str 更新字符串     * @param charNumber 间隔几个字符     * @throws Exception     */    public void strInsert(String str,int charNumber)throws Exception{              this.text = read();              this.write(true, this.insert(text, str, 0, charNumber));    }    /**     * 由beginNumber开始,endNumber结束 把字符串file中     * 插入字符串 str     * 例 insert("abc","*",0,1) 返回字符串为 a*b*c 代表由第○个字符开始,第隔3个字符插入一个*     * @param file 被更新字符串     * @param str  更新字符串     * @param beginNumber 从第几位开始更新     * @param endNumber 第几位结束更新     * @return     */    protected String insert(String file,String str,int beginNumber,int endNumber){       if(endNumber>=file.length())return file.substring(beginNumber,file.length());       else{           return file.substring(beginNumber,endNumber)+str+insert(file,str,endNumber,(endNumber*2-beginNumber>file.length())                  ?endNumber=file.length()                         :endNumber*2-beginNumber);       }    }        public static void main(String[] args) {       try {           new FileDispose("f:/a.txt").strInsert("\r\n", 20);       } catch (Exception e) {           e.printStackTrace();       }           }        }