Java对SQL改装

文章介绍了Java方法loadfile用于加载并处理文件内容,ls_addCollate函数对SQL语句进行整理,writeFileContext用于写入文件,展示了如何使用BufferedWriter和Files.write进行文件操作。
摘要由CSDN通过智能技术生成
	List<String> fileContent=new ArrayList<String>();
	public String sAllWork(String psFileName)
	{
		List<String> vls_fileCOntent=loadfile(readPath+"\\"+psFileName);
		this.fileContent=ls_addCollate(vls_fileCOntent);
		//sWriteFile(psFileName);
		try {
			writeFileContext(this.fileContent, writePath+"\\"+psFileName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.fileContent=null;
		return null;
	}
	
	public List<String> loadfile(String filename)
	{
		File file = new File(filename);
	    StringBuilder localStrBulider = new StringBuilder();
	    List<String> fileheap=new ArrayList<String>();
	    if(file.isFile() && file.exists()) {
	        try {

	        	FileInputStream pfile = new FileInputStream(filename);//首先,获取文件。
    	        InputStreamReader isr = new InputStreamReader(pfile,"Unicode");//将读取文件的格式转换为文件的格式,
    	        int temp;
    	        int lasttemp=0;
    	        //https://blog.csdn.net/Gadxiong/article/details/80098161
    	        String rs="";
    	        while ((temp=isr.read())!=-1){//循环遍历
    	            //System.out.print((char)temp);//输出文件里的内容
    	            rs+=(char)temp;    	            
    	            if (lasttemp==13&&temp==10)
    	            {
    	            	fileheap.add(rs);
    	            	rs="";
    	            }
    	            lasttemp=temp;
    	        }
    	        isr.close();//关闭isr
    	        pfile.close();//关闭file
	        }catch (Exception e) {
	            // TODO Auto-generated catch block
	        }
	    }else {
	    	
	    }
	    return fileheap;
	}
	public List<String> ls_addCollate(List<String> pls_sql)
	{
		List<String> rls=new ArrayList<String>();
		int vn_len=pls_sql.size();
		boolean vb_started=false;
		boolean vb_onlyCopy=false;
		for (int wn=0;wn<vn_len;wn++)
		{
			String ws_row=pls_sql.get(wn);
			try {
				ws_row=trimnull(ws_row);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (vb_started==false)
				if (ws_row.startsWith("CREATE TABLE"))
					vb_started=true;
			if (vb_started)
			{
				if (ws_row.startsWith("GO"))
					vb_onlyCopy=true;
				if (vb_onlyCopy==false)
				{
					if (ws_row.contains("varchar]"))
					{
						ws_row=sReplace(ws_row);
					}
				}
			}

			rls.add(ws_row);
		}
		return rls;
	}
	
	private String sReplace(String psRow)
	{
		String rsRow=psRow;
		int vn_loc=rsRow.indexOf(")");
		if (vn_loc==-1)
			return rsRow;
		rsRow=rsRow.replace(")", ") collate Chinese_PRC_CS_AS ");
		return rsRow;
	}
	
	public  static void writeFileContext(List<String> strings, String path) throws Exception 
	{//https://blog.csdn.net/yanhhuan/article/details/109074873
        if(null==strings||strings.size()==0||path==null||"".equals(path)){
            return;
        }
        File file = new File(path);
        //如果没有文件就创建
        if (!file.isFile()) {
            file.createNewFile();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
        for (String l : strings) {
            writer.write(l + "\r\n");
        }
        writer.close();
    }
	
	public String sWriteFile(String psFile)
	{
		//https://blog.csdn.net/jake_Aaron/article/details/120163767
		Path ps=Paths.get(writePath+"\\"+psFile);
		//Stream<String> lines = Files.lines(ps,  Charset.forName("UTF-8"));
        //lines.forEach(System.out::println);
        try {
			Files.write(Paths.get(writePath+"\\"+psFile), this.fileContent, StandardCharsets.UTF_8);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return null;
	}
	
	private String sWriteFile_utffail(String psFile)
	{
		
		try {
			Path output = Paths.get(writePath+"\\"+psFile);
		    Files.write(output, this.fileContent);
		} catch (Exception e) {
		    e.printStackTrace();
		}

		return null;
	}
	/**
	 * 去除字符串中的null域
	 * @param string
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String trimnull(String string) throws UnsupportedEncodingException
    {//https://blog.csdn.net/vamlichunyan/article/details/49765365
		ArrayList<Byte> list = new ArrayList<Byte>();
		byte[] bytes = string.getBytes("UTF-8");
		for(int i=0;bytes!=null&&i<bytes.length;i++){
			if(0!=bytes[i]){
				list.add(bytes[i]);
			}
		}
		byte[] newbytes = new byte[list.size()];
		for(int i = 0 ; i<list.size();i++){
			newbytes[i]=(Byte) list.get(i); 
		}
		String str = new String(newbytes,"UTF-8");
	    return str;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值