zoukankan      html  css  js  c++  java
  • 生成大量插入语句,并将语句写入txt文件中

    
    import java.io.*;
    
    /**
     * Created by czz on 2019/9/23.
     */
    public class TTest {
        /**
         * 生成大量插入语句,并将语句写入txt文件中
         * 本例为生成一千条以上的ip地址数据插入数据库
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception{
            String filename = "c:"+File.separator+"e.txt";
            File file = new File(filename);
            StringBuilder sb = new StringBuilder();
            for (int i=2;i<255;i++){
                String ip = "192.168.52."+i;
                String str = "insert into iptable (nicname,ip,ip,mask) values ('test','"+ip+"','255.255.255.0');
    ";
                sb.append(str);
            }
    
            for (int i=2;i<255;i++){
                String ip = "192.168.53."+i;
                String str = "insert into iptable (nicname,ip,mask) values ('test','"+ip+"','255.255.255.0');
    ";
                sb.append(str);
            }
    
            for (int i=2;i<255;i++){
                String ip = "192.168.54."+i;
                String str = "insert into iptable (nicname,ip,mask) values ('test','"+ip+"','255.255.255.0');
    ";
                sb.append(str);
            }
    
            for (int i=2;i<255;i++){
                String ip = "192.168.55."+i;
                String str = "insert into iptable (nicname,ip,mask) values ('test','"+ip+"','255.255.255.0');
    ";
                sb.append(str);
            }
    
            String ss = sb.toString();
            System.out.println(ss);
            byte[] b = ss.getBytes();  //将字符串转换成字节数
            OutputStream out = null;![](https://img2018.cnblogs.com/blog/1480523/201910/1480523-20191016181410606-1043685942.png)
    
    
            try {
                out = new FileOutputStream(file);  //实例化OutpurStream
                out.write(b);    //写入
            }catch(Exception e){
                e.printStackTrace();
            } finally {
                if (out != null){
                    try {
                        out.close();    //关闭
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    大家有兴趣也可以关注我的公众号查看文章。

  • 相关阅读:
    git rm与git rm --cached
    成小胖学习ActiveMQ·基础篇
    Nginx负载均衡的详细配置及使用案例详解.
    win10安装git fatal: open /dev/null or dup failed: No such file or directory错误解决方法
    linux系统下安装两个或多个tomcat
    linux环境下安装nginx步骤
    JDK里常见容器总结
    深入理解HashMap
    如何再一台电脑上配置多个tomcat同时运行
    洛谷P1530 分数化小数 Fractions to Decimals
  • 原文地址:https://www.cnblogs.com/caozz/p/createText.html
Copyright © 2011-2022 走看看