当前位置: 技术问答>java相关
一个简单问题,给分!
来源: 互联网 发布时间:2017-04-30
本文导语: 我想对本机或远程机上的已经存在的文件进行修改(在指定位置插入字符或在指定位置插入一行),请问如何实现?最好有源程序!一定给分。在此先谢过! | 指定位置???一定要分析你的文件...
我想对本机或远程机上的已经存在的文件进行修改(在指定位置插入字符或在指定位置插入一行),请问如何实现?最好有源程序!一定给分。在此先谢过!
|
指定位置???一定要分析你的文件内容才能找到指定位置吧?
应该就是字符创操作的问题了。
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 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;
}
应该就是字符创操作的问题了。
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 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;
}
|
我已前写的一个,是替换指定目录下(包括子目录)的所有.html文件里面的指定字符串,你看看修改一下对你有没有用。
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=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);}
}
}
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=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);}
}
}
|
:)那你再想办法吧,俺能力到此
|
文件要是只读呢?我要完成的任务就是这样的,你会遇到麻烦的。
|
有读写权限就够,重新写一次进去就可以.不用删除.
|
修改文件并不难,肯定是覆盖原文件了,关键是有没有相关权限!
远程系统文件更是需要权限的了!这方面花点功夫吧!!
远程系统文件更是需要权限的了!这方面花点功夫吧!!
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。