日期:2014-05-20  浏览次数:20725 次

java如何文本修改写入
读取一个

strsql=strsql+ "   select   *   from   table   ";

strsql=strsql+ "   where   cola=a   ";

strsql=strsql+ "   and   colb=b   ";

之类的文本       把前面的strsql=strsql+ "     和后面的 ";     都灭了   ;

写如一个新的文本


微微详细点,初学java。

修改的要吐了。

谢谢


解决马上结贴。分不够可以加的

------解决方案--------------------
写个简单的正则
对你读进来的每一行,执行者个语句

a为你读入的字符串

a = a.replaceAll( "^.*\ "|\ "; ", " ");
------解决方案--------------------
楼上的正则有问题,没有考虑贪婪问题

偶有空,给你写了段代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class SqlGetter {

public static void main(String[] args) {
String str = "strsql=strsql+\ " select * from table \ "; ";
System.out.println(getSqlStr(str));
}

public static String getSqlStr(String sqlLine) {
String patternString = "^.*?\ "|\ ";$ ";
Pattern pattern = null;
try {
pattern = Pattern.compile(patternString);
} catch (PatternSyntaxException e) {
System.out.println( "Pattern syntax error ");
return sqlLine;
}

Matcher matcher = pattern.matcher(sqlLine);
return matcher.replaceAll( " ");
}

}

------解决方案--------------------
import java.io.RandomAccessFile;

public class DumpFile {
public static void main(String[] args) throws Exception {
RandomAccessFile f1 = new RandomAccessFile( "c:/temp/1.txt ", "r ");
RandomAccessFile f2= new RandomAccessFile( "c:/temp/2.txt ", "rw ");
while(true){
String lineText = f1.readLine();
if(lineText==null){
break;
}
f2.writeBytes(lineText.replaceAll( "^.*?\ "|\ ";$ " , " ") + "\r\n ");
}
f2.close();
f1.close();
}
}

------解决方案--------------------
恩 来学习的 接分