Java读取文件

来源:互联网 发布:电子商务大数据分析 编辑:程序博客网 时间:2024/06/06 00:50
    这两天一直在做数据库迁移的工作,算是完成了,但是发现,当我从MySQL里导出的sql文件里的数据都是这样的:
INSERT INTO `t_admin_oper_type` VALUES (141, '认证服务管理', 100, 2, 2, 2);
INSERT INTO `t_admin_oper_type` VALUES (142, '页面配置', 100, 2, 3, 3);
INSERT INTO `t_admin_oper_type` VALUES (143, '删除认证服务', 100, 2, 2, 5);
INSERT INTO `t_admin_oper_type` VALUES (144, 'Protal配置', 100, 2, 3, 6);
在表名的地方多出了一对单引号,想来没事做,闲着,就写了一下,Java读取sql文件,再把sql语句重新读取成有效可以直接在Oracle的plsql中插入的数据。
代码如下:
public class SQLUtil {
public static void main(String[] args) throws IOException {
  String fileName= "t_admin.sql";
  InputStream file = new FileInputStream(fileName);
  BufferedReader reader = new BufferedReader(new InputStreamReader(file,"UTF-8"));
  String line = null;
  while((line = reader.readLine())!= null){
    StringBuffer buffer = new StringBuffer();
    int start = line.indexOf("INTO ");
    int end = line.indexOf(" VALUES");
    String tableName = line.substring(start+5, end);
    tableName = tableName.substring(1,tableName.length()-1);
    buffer.append(line.substring(0, start+5));
    buffer.append(tableName.toUpperCase());
    buffer.append(line.substring(end, line.length()));
    System.out.println(buffer.toString()+";");
  }
  reader.close();
 
}
}
运行后的语句结果为:
INSERT INTO t_admin_oper_type VALUES (141, '认证服务管理', 100, 2, 2, 2);
INSERT INTO t_admin_oper_type VALUES (142, '页面配置', 100, 2, 3, 3);
INSERT INTO t_admin_oper_type VALUES (143, '删除认证服务', 100, 2, 2, 5);
INSERT INTO t_admin_oper_type VALUES (144, 'Protal配置', 100, 2, 3, 6);
0 0
原创粉丝点击