1.[代码][Java]代码
package com.autocould.zzstart;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
public class ReadLines {
//向指定文件打印
public static void writerLines(StringBuffer outString, String filePathAndName) {
String fileContent = outString.toString();
try {
File f = new File(filePathAndName);
if (!f.exists()) {
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
BufferedWriter writer = new BufferedWriter(write);
writer.write(fileContent);
writer.close();
} catch (Exception e) {
System.out.println("outWriteFiled");
e.printStackTrace();
}
}
// 向剪贴板写入信息
protected static void setClipboardText(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
// 读取系统剪贴板
protected static String getClipboardText(Clipboard clip) throws Exception{
Transferable clipT = clip.getContents(null);
if (clipT != null) {
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
//首字母大写
public static String upCaseName(String name) {
if (null == name || "".equals(name)) {
return null;
}
return name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
}
//首字母小写
public static String lowerCaseName(String name) {
if (null == name || "".equals(name)) {
return null;
}
return name.substring(0, 1).toLowerCase() + name.substring(1, name.length());
}
// 去除双空格
public static StringBuffer removeTrim(StringBuffer fileContent){
for (int j = 0; j < fileContent.length()-1; j++) {
char c1 = fileContent.charAt(j);
char c2 = fileContent.charAt(j+1);
if(c1 == ' ' && c2 == ' '){
fileContent.deleteCharAt(j);
j--;
}
}
return fileContent;
}
// 以utf8读取文件
public static StringBuffer readFile(String filePathAndName) {
StringBuffer fileContent = new StringBuffer();
String string = null;
try {
File f = new File(filePathAndName);
if (f.isFile() && f.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(f), "UTF-8");
BufferedReader reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
fileContent.append(line).append("\n");
}
read.close();
}
} catch (Exception e) {
System.out.println("error");
e.printStackTrace();
}
return fileContent;
}
// 把通篇文字去除每行的多余空白
public static String[] fomateString(StringBuffer fileContent) {
String string = removeTrim(fileContent).toString();
String[] A = string.split("\n");
for (int i = 0; i < A.length; i++) {
A[i] = A[i].trim();
}
if (A.length <= 2) {
return null;
}else{
return A;
}
}
//获取纯注释与private对象
public static List getParam(String[] string) {
List list = new ArrayList();
for (int i = 0; i < string.length; i++) {
String temp = string[i].replace('/', ' ').replace('*', ' ').trim();
if ("".equals(temp) || temp.startsWith("Set ")
|| temp.startsWith("set ") || temp.startsWith("Get ")
|| temp.startsWith("get ") || temp.startsWith("private ")
|| temp.startsWith("@")) {
continue;
} else {
String[] e;
if (string.length > i + 1) {
String[] tearr = string[i + 1].split(" ");
e = new String[3];
e[0] = temp;
e[1] = tearr[1];
e[2] = tearr[2].replace(";", "");
list.add(e);
}else{
e = new String[1];
e[0] = temp;
list.add(e);
}
}
}
return list;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws Exception {
//目标读取
java.awt.datatransfer.Clipboard clip=java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
String fileContenttemp = getClipboardText(clip);
StringBuffer fileContent = new StringBuffer(fileContenttemp);
String[] A = fomateString(fileContent);
if (A == null) {
System.out.println("readFile ERROR");
return;
}
if (A.length <= 3) {
System.out.println("File is too short ! it must has over 3 lines !");
return;
}
List params = getParam(A);
String outString1 = "$.set$($);// $";//set项目 注释 4 inparam UP-attribute UP-attribute remark
String outString2 = "$ $ = null;//$";//定义变量 3 type LO-attribute remark
String outString3 = "$ = $.get$();// $";//变量赋值 4 LO-attribute outParam UP-attribute remark
//使用变量
String inParam = "isExistSecMaFacilTblIn";
String outParam = "inSt";
String remark = "";
String type = "";
String attribute = "";
StringBuffer outString = new StringBuffer();;
for (int i = 0; i < params.size(); i++) {//inparam.setAAA(AAA);//AAA
String[] cell = params.get(i);
if(cell.length<3){
System.out.println(cell[0]+"can not find the Variable!");
continue;
}
remark = cell[0];
type = cell[1];
attribute = cell[2];
outString.append("\n").append(new StringBuffer(outString1));
outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, inParam);//参数2
outString.replace(outString.indexOf("$"), outString.indexOf("$")+1,upCaseName(attribute));//参数3
outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, upCaseName(attribute));//参数4
outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, remark);//参数1
}
//for (int i = 0; i < params.size(); i++) { //String ss = "";
//String[] cell = params.get(i);
//if(cell.length<3){
//System.out.println(cell[0]+"can not find the Variable!");
//continue;
//}
// remark = cell[0];
// type = cell[1];
// attribute = cell[2];
//outString.append("\n").append(new StringBuffer(outString2));
//outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, type);//参数1
//outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, lowerCaseName(attribute));//参数4
//outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, remark);//参数4
//
//}
//
//for (int i = 0; i < params.size(); i++) {
//String[] cell = params.get(i);
//if(cell.length<3){
//System.out.println(cell[0]+"can not find the Variable!");
//continue;
//}
// remark = cell[0];
// type = cell[1];
// attribute = cell[2];
//outString.append("\n").append(new StringBuffer(outString3));
//outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, lowerCaseName(attribute));//参数1
//outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, outParam);//参数4
//outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, upCaseName(attribute));//参数4
//outString.replace(outString.indexOf("$"), outString.indexOf("$")+1, remark);//参数4
//}
java.awt.datatransfer.Clipboard clipboard=java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
setClipboardText(clipboard,outString.toString());
}
}