您的位置:首页 > 编程语言 > Java开发

[JAVA]用JAVA文件读写操作截取文件中所有以x开头y结尾的字符串

2015-08-14 17:44 459 查看
最近在数据库的迁移中,在对SQL脚本复核的过程时,遇到一个重复劳动的情况:把所有的SELECT语句和CREATE语句分别截出来,当时手工复制了一天……后来用JAVA文件读写操作写了一个小工具,和大家分享一下,代码粗制烂造,能用就行……

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.String;
public class test1_cut_context {
public static void main(String args[]) throws IOException, IndexOutOfBoundsException{
File f=new File("D:"+File.separator+"filetest1.txt");//写入文件的位置
String temp="";
if(!f.exists()){ //如果文件不存在就创建
f.createNewFile();
}
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("D:"+File.separator+"bos_core.pl")
4000
,"GB2312")); //读入文件的位置,用FileInputStream这个类解决中文注释乱码问题
BufferedWriter bw=new BufferedWriter(new FileWriter(f));
temp=br.readLine();
while(temp!=null){
if(temp.contains("select")){
bw.write(temp,temp.indexOf("select"),temp.length()-temp.indexOf("select")); //select有可能是子查询,将其截出来
bw.newLine();
if(temp.contains(";")){
temp=br.readLine();
bw.newLine();
}
else
{
temp=br.readLine();
while(true){
bw.write(temp);
bw.newLine();
System.out.println(temp.length());
System.out.println("succeed");
if(temp.contains(";")){
bw.newLine();
temp=br.readLine();
break;
}
temp=br.readLine();
bw.flush();
}
}
}
else
temp=br.readLine();
}
br.close();
bw.close();
System.exit(0);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA 文件读写