对文件内容就行修改-java代码

摘要:
input=newArrayList<outputdata.add(outElement);}else{outputdata.add(element);j<Stringfilepath)throwsIOException{Filefile=newFile(filepath);file.exists()){file.createNewFile();i<

在实际的生产中,会遇到数据库版本的不同,sql语句的语法会出现改变,

我在这次的金仓数据库中就遇到了,V8版本的建表语句跟V7版本的不一样,所以需要对里面内容进行修改

对文件内容就行修改-java代码第1张

 从图中我们可以看到到,这个是金仓数据库v8版本导出的建表语句,这个语句在v7版本是不能运行的

我们需要做的就是

对文件内容就行修改-java代码第2张

在这分享一小段java代码,本人是个菜鸟,大家看到写得不好也请见谅!

首先在idea里面创建一个maven项目,具体的创建步骤就不多说了

对文件内容就行修改-java代码第3张

package com.gong;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SqlDeal {
    //按行读取文件内容到集合
    public static List<String> readFlie(File file){
        List<String> input = new ArrayList<String>();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
               // System.out.println(s);
                input.add(s);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return input;
    }

    //处理内容
    public static List<String> dealdata(List<String> inputdata){
        List<String> outputdata = new ArrayList<String>();
        for(int i=0;i<inputdata.size();i++){
          String element=inputdata.get(i);
          String outElement=null;
          if(element.contains("char")){
            outElement = element.replaceAll("char", "");
            outputdata.add(outElement);
          }else {
            outputdata.add(element);
          }
        }
        for(int j=0;j<outputdata.size();j++){
           String temp = outputdata.get(j);
           System.out.println(temp);
        }
        return outputdata;
    }

    //把内容写入文本文件中
    public static void writercontent(List<String> output,String filepath) throws IOException {
        File file = new File(filepath);
        if(!file.exists()){
            file.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fileWriter);
        for(int i=0;i<output.size();i++){
            bw.write(output.get(i).toString());
            bw.newLine();
        }
        bw.close();
    }

    public static void main(String[] args) throws IOException {
        //输入文件
        File infile = new File("D:\MyCode\sqldeal\inputdata\input.sql");
        List<String> inputdata=new ArrayList<String>();
        inputdata=readFlie(infile);//读取输入的文件内容
        List<String> onputdata=new ArrayList<String>();
        onputdata=dealdata(inputdata);//对内容进行处理
        //输出文件的路径
        String outputpath="D:\MyCode\sqldeal\outputdata\output.sql";
        writercontent(onputdata,outputpath);//把处理后的内容写入到文件
    }

}

运行代码,查看输出路径的文件内容

对文件内容就行修改-java代码第4张

成功!!

免责声明:文章转载自《对文件内容就行修改-java代码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SpringBoot:异步开发之异步调用Flink源码解析(四)——从Flink集群部署和任务提交模式看Flink任务的核心组件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PLSQL常用配置之窗口/版面保存、SQL格式化/美化、SQL注释去掉注释等快捷键配置、登陆历史修改配置

http://blog.csdn.net/hyeidolon/article/details/8251791   PLSQL常用配置之窗口/版面保存、SQL格式化/美化、SQL注释去掉注释等快捷键配置、登陆历史修改配置1、PL/SQL Developer记住登陆密码 在使用PL/SQL Developer时,为了工作方便希望PL/SQL Develop...

一个简单的MySQL数据库同步脚本

Talk is cheap,show you code. 1 mysqldump -h[remoteHost] -u[username] -p[password] -P[port] --databases [databasename1] [databasename2] > [D:mysqlsync emp.sql] 2 echo "备份完成"; 3...

在Ubuntu Kylin 16.04 LTS 上源码方式安装odoo

更新Ubuntu服务器软件源 运行以下代码: sudo apt-get update  #更新软件源   sudo apt-get dist-upgrade  #更新软件包,自动查找依赖关系   sudo shutdown -r now  #重启服务器,以更新改变的内容    新建系统用户用于运行Odoo程序 运行如下命令创建系统用户: sudo...

Mac添加环境变量的三种方法

法一: sudo vi /etc/paths 来编辑 paths,将环境变量添加到 paths 中。 vim 是一个编辑器,另外还有几个,如:Pico,Emacs。 Hint:输入环境变量时,不用一个一个地输入,只要拖动文件夹到 Terminal 里就可以了。   法二: 1.创建一个文件: sudo touch /etc/paths.d/mysql 2....

Nginx事件管理之事件处理流程

1. 概述 事件处理要解决的两个问题: "惊群" 问题,即多个 worker 子进程监听相同端口时,在 accept 建立新连接时会有争抢,引发不必要的上下文切换, 增加系统开销。 负载均衡问题。 这两个问题的解决需要依靠 Nginx 的 post 事件处理机制。Nginx 设计了两个 post 队列,一个是由被触发的监听连接的读事 件构成的 ngx_...

用Visual studio 2005打开Visual studio 2008 开发的项目

方法一参考: 1. 用记事本打开解决方案文件(扩展名为*.sln).将Microsoft Visual Studio Solution File, Format Version 10.00# Visual Studio 2008把上面这两句,改成Microsoft Visual Studio Solution File, Format Version 9....