指定位置???一定要分析你的文件内容才能找到指定位置吧?应该就是字符创操作的问题了。 public void HandleFileStream( String OpenPathFileName,String SavePathFileName ) {
String readline = "";
try{
RandomAccessFile openfile = new RandomAccessFile( this.OpenPathFileName,"r" );
File file = new File( this.SavePathFileName + "temp" );
System.out.println("保存アドレス" + this.SavePathFileName );
RandomAccessFile savefile = new RandomAccessFile( file,"rw" ); readline = openfile.readLine();
while( true ) {
if( readline != null ) {
System.out.println( readline );
if( readline != null ) {
savefile.writeBytes( this.StringHandle( readline ) );
savefile.writeBytes("\n");
}
readline = openfile.readLine();
} else {
savefile.close();
openfile.close();
break;
}
}
File filesource = new File( this.SavePathFileName );
if( filesource.exists() == true ) {
filesource.delete();
}
file.renameTo( new File( this.SavePathFileName ) );
}catch( FileNotFoundException ee ) {
System.out.println(ee.getMessage());
}catch( IOException e ) {
System.out.println(e.getMessage());
}
} public String StringHandle( String str ) {
String ret = "";
int head = -1;
int end;
boolean flg = false;
String handledstringfrist = "";
String handledstringtwo = "";
if( str != null ) {
for( int i = 0;i < this.handletype.length;i++ ) {
end = this.handletype[i].length();
head = str.indexOf( this.handletype[i] );
System.out.println( "head=>" + head );
if( end > 0 ) {
if( head != -1 ) {
flg = true;
handledstringfrist = str.substring( 0,head );
handledstringtwo = str.substring( head + end,str.length() );
//handledstring = str.substring( head, head + end );
System.out.println( "head前=>" + handledstringfrist + handledstringtwo );
ret = handledstringfrist + handledstringtwo + this.handletype[i];
System.out.println( "head後=>" + ret );
break;
}
}
}
}
if( flg == false )
ret = str; return ret;
}
String readline = "";
try{
RandomAccessFile openfile = new RandomAccessFile( this.OpenPathFileName,"r" );
File file = new File( this.SavePathFileName + "temp" );
System.out.println("保存アドレス" + this.SavePathFileName );
RandomAccessFile savefile = new RandomAccessFile( file,"rw" ); readline = openfile.readLine();
while( true ) {
if( readline != null ) {
System.out.println( readline );
if( readline != null ) {
savefile.writeBytes( this.StringHandle( readline ) );
savefile.writeBytes("\n");
}
readline = openfile.readLine();
} else {
savefile.close();
openfile.close();
break;
}
}
File filesource = new File( this.SavePathFileName );
if( filesource.exists() == true ) {
filesource.delete();
}
file.renameTo( new File( this.SavePathFileName ) );
}catch( FileNotFoundException ee ) {
System.out.println(ee.getMessage());
}catch( IOException e ) {
System.out.println(e.getMessage());
}
} public String StringHandle( String str ) {
String ret = "";
int head = -1;
int end;
boolean flg = false;
String handledstringfrist = "";
String handledstringtwo = "";
if( str != null ) {
for( int i = 0;i < this.handletype.length;i++ ) {
end = this.handletype[i].length();
head = str.indexOf( this.handletype[i] );
System.out.println( "head=>" + head );
if( end > 0 ) {
if( head != -1 ) {
flg = true;
handledstringfrist = str.substring( 0,head );
handledstringtwo = str.substring( head + end,str.length() );
//handledstring = str.substring( head, head + end );
System.out.println( "head前=>" + handledstringfrist + handledstringtwo );
ret = handledstringfrist + handledstringtwo + this.handletype[i];
System.out.println( "head後=>" + ret );
break;
}
}
}
}
if( flg == false )
ret = str; return ret;
}
import java.io.*;
import java.util.*;public class FileControl extends Object { public FileControl() {
} public static void getChildrenAndReplace(String directory){
try{
File theFile = new File(directory);
String[] files = theFile.list();
String fileName;
for(int i=0;i<files.length;i++){
fileName = files[i];
File child = new File(directory+"/"+fileName);
if(child.isDirectory()){
if(!child.isHidden()) getChildrenAndReplace(directory+"/"+files[i]);
}
else if(!child.isHidden()&&fileName.toUpperCase().indexOf(".HTM")>=0) replaceString(child,"gb_2312-80","iso-8859-1");
}
}catch(Exception e){e.printStackTrace(System.out);}
} public static void replaceString(File file,String newStr,String oldStr){
RandomAccessFile theFile = null;
try{
theFile = new RandomAccessFile(file,"rw");
String content = theFile.readLine();
int pointer = content.indexOf(oldStr);
StringBuffer theBuffer = new StringBuffer(content);
int initIndex = 0;
StringBuffer newContent = null;
while(true){
if(pointer!=-2) pointer = content.toUpperCase().indexOf(oldStr.toUpperCase());
if(pointer>=0){
theBuffer.replace(pointer,pointer+oldStr.length(),newStr);
pointer=-2;
}
content = theFile.readLine();
if(newContent==null) newContent = theBuffer;
else newContent.append("\n"+theBuffer.toString());
if(content!=null) theBuffer = new StringBuffer(content);
else break;
}
int length=Integer.parseInt(""+file.length());
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file),length));
dos.writeBytes(newContent.toString());
dos.flush();
System.out.println("over file:"+file.getPath());
}catch(Exception e){e.printStackTrace(System.out);}
finally{
try{
theFile.close();
}catch(Exception e){e.printStackTrace(System.out);}
}
}