[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);
}
}
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对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统