java log into_提取log文件中的insert into 语句插入数据库

该代码段展示了如何从一个SQL文件中读取内容,特别是寻找并提取包含'insert into'语句的行,然后将这些内容写入新的SQL文件。主要使用了Java的IO流操作,包括FileInputStream、FileOutputStream、BufferedReader等。
摘要由CSDN通过智能技术生成

package test;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

public class readsql {

public static void main(String[] args) {

String readpath ="c:/readme.sql";//读取文件路劲

String writepath="c:/write.sql";//写入文件路径

StringBuffer

content=readTxtFile(readpath);//提取readme.sql文件内容提取含有insert语句

返回在content中

writeTxtFile(writepath,content.toString());//将content内容写入write.sql文件中

}

// 读取文件

public static StringBuffer readTxtFile(String readpath)

{

StringBuffer sb = new StringBuffer() ;

try {

String encoding = "GBK";

File file = new File(readpath);

if (file.isFile() && file.exists()) { //

判断文件是否存在

InputStreamReader read = new InputStreamReader(new

FileInputStream(file), encoding);// 考虑到编码格式

BufferedReader bufferedReader = new

BufferedReader(read);

String lineTxt = null;

while ((lineTxt = bufferedReader.readLine()) != null) {

String lineTxtnew=lineTxt+"\\r\\n";

if (lineTxtnew.length()>=11 && "insert

into".equals(lineTxtnew.substring(0, 11))) {

sb = sb.append(lineTxt).append("\n\r");

}

}

read.close();

System.out.println("Read successfully");

} else {

System.out.println("找不到指定的文件");

}

} catch (Exception e) {

System.out.println("读取文件内容出错");

e.printStackTrace();

}

return sb;

}

//写入文件

public static void writeTxtFile(String writepath,String

content){

File file = new File(writepath);

try (FileOutputStream fop = new

FileOutputStream(file)) {

if (!file.exists()) {

file.createNewFile();

}

byte[] contentInBytes =

content.getBytes();

fop.write(contentInBytes);

fop.flush();

fop.close();

System.out.println("Write

successfully");

} catch (IOException e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值