├── .gitignore ├── README.md ├── conf └── conf.properties ├── pom.xml └── src └── main └── java └── daiyun ├── cnc └── fanuc │ ├── job │ └── FanucCncInfoCollect.java │ └── sdk │ ├── Fanuc.java │ ├── FanucCncAPI.java │ ├── GetClassAttr.java │ ├── JnaStructure.java │ ├── MyStructure.java │ ├── OutObject.java │ ├── RefObject.java │ └── StringHelper.java ├── conf ├── Configurable.java ├── Configuration.java ├── Configured.java └── SysConfig.java └── utils ├── LocalFlieUtil.java └── TimeFormat.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | log/ 3 | target/ 4 | lib/ 5 | */test/ 6 | *.iml 7 | *.dll 8 | *.h 9 | .classpath 10 | .project 11 | .settings/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## fanuc cnc infomation collect. -------------------------------------------------------------------------------- /conf/conf.properties: -------------------------------------------------------------------------------- 1 | quartz=0/5 * * * * ? 2 | addr=192.168.1.101 3 | port=8193 4 | timeout=2 5 | dataFilePre=changjiang-02- 6 | dataDir=data/ 7 | collectInterval=100 8 | debug=false -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | godaiyun 8 | FanucCNCInfo 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | maven-assembly-plugin 15 | 16 | false 17 | 18 | jar-with-dependencies 19 | 20 | 21 | 22 | daiyun.cnc.fanuc.job.FanucCncInfoCollect 23 | 24 | 25 | 26 | 27 | 28 | make-assembly 29 | package 30 | 31 | assembly 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.apache.maven.plugins 39 | maven-compiler-plugin 40 | 3.1 41 | 42 | 1.8 43 | 1.8 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.apache.logging.log4j 53 | log4j-core 54 | 2.0-rc1 55 | 56 | 57 | 58 | org.apache.logging.log4j 59 | log4j-api 60 | 2.0-rc1 61 | 62 | 63 | 64 | com.google.guava 65 | guava 66 | 19.0 67 | 68 | 69 | 70 | 71 | net.java.dev.jna 72 | jna 73 | 4.1.0 74 | 75 | 76 | 77 | org.json 78 | json 79 | 20160810 80 | 81 | 82 | 83 | org.quartz-scheduler 84 | quartz 85 | 2.3.0 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /src/main/java/daiyun/cnc/fanuc/job/FanucCncInfoCollect.java: -------------------------------------------------------------------------------- 1 | package daiyun.cnc.fanuc.job; 2 | 3 | import daiyun.cnc.fanuc.sdk.Fanuc; 4 | import daiyun.cnc.fanuc.sdk.FanucCncAPI; 5 | import daiyun.conf.SysConfig; 6 | import daiyun.utils.LocalFlieUtil; 7 | import daiyun.utils.TimeFormat; 8 | import org.json.JSONArray; 9 | import org.json.JSONObject; 10 | 11 | import java.io.File; 12 | import java.lang.reflect.Field; 13 | import java.util.Date; 14 | import java.util.HashSet; 15 | import java.util.Set; 16 | 17 | /** 18 | * @author daiyun 19 | * @date 2018-07-05 17:27. 20 | */ 21 | public class FanucCncInfoCollect { 22 | 23 | private FanucCncAPI fanucCncAPI; 24 | 25 | public static void main(String[] args) { 26 | FanucCncInfoCollect fanucAPITest = new FanucCncInfoCollect(); 27 | fanucAPITest.start(); 28 | } 29 | 30 | public void start() { 31 | fanucCncAPI = new FanucCncAPI(SysConfig.conf.getString("addr"), 32 | SysConfig.conf.getInteger("port").shortValue(), 33 | SysConfig.conf.getLong("timeout")); 34 | 35 | // 连接机台 36 | if (fanucCncAPI.connectCNC()) { 37 | 38 | try { 39 | while (true) { 40 | JSONObject jsonFile = new JSONObject(); 41 | 42 | long s = System.currentTimeMillis(); 43 | 44 | // 主轴位置信息 45 | jsonFile.put("position", position()); 46 | 47 | // 阻力 48 | // jsonFile.put("resistance", resistance()); 49 | 50 | // 刀号 51 | jsonFile.put("tool", toolNum()); 52 | 53 | // 转速 54 | jsonFile.put("S", rpm()); 55 | 56 | // 进给 57 | jsonFile.put("F", getF()); 58 | 59 | // 计算休眠时间 60 | 61 | // 信息写入文件 62 | if (!SysConfig.conf.getBoolean("debug")) { 63 | long e = System.currentTimeMillis(); 64 | Date currentTime = new Date(); 65 | String collectTime = TimeFormat.dateFormat("yyyy-MM-dd-HH-mm", currentTime); 66 | LocalFlieUtil.appendFile(SysConfig.conf.getString("dataDir") + File.separator + TimeFormat.dateFormat("yyyy-MM-dd", currentTime) + File.separator, 67 | SysConfig.conf.getString("dataFilePre") + collectTime + ".txt", 68 | jsonFile.toString()); 69 | 70 | long sleepMillis = SysConfig.conf.getInteger("collectInterval") - (e - s); 71 | if (sleepMillis > 0) { 72 | Thread.sleep(sleepMillis); 73 | } 74 | 75 | } else { 76 | System.out.println(jsonFile); 77 | Thread.sleep(2000); 78 | } 79 | } 80 | } catch (Exception e) { 81 | e.printStackTrace(); 82 | } finally { 83 | fanucCncAPI.cancle(); 84 | } 85 | } 86 | } 87 | 88 | public JSONObject position() { 89 | JSONObject json = new JSONObject(); 90 | int positiontype = -1; 91 | Fanuc.ODBPOS odbpos = fanucCncAPI.getPosition(positiontype); 92 | json.put("time", System.currentTimeMillis()); 93 | Set axis = new HashSet<>(); 94 | axis.add("X"); 95 | axis.add("Y"); 96 | axis.add("Z"); 97 | JSONArray jsonArray = asisInfo(odbpos, positiontype, axis); 98 | json.put("info", jsonArray); 99 | return json; 100 | } 101 | 102 | private JSONObject rpm() { 103 | JSONObject json = new JSONObject(); 104 | Fanuc.ODBACT odbacts = fanucCncAPI.getS(); 105 | json.put("time", System.currentTimeMillis()); 106 | json.put("info", odbacts.data); 107 | return json; 108 | } 109 | 110 | private JSONObject getF() { 111 | JSONObject json = new JSONObject(); 112 | Fanuc.ODBACT odbacts = fanucCncAPI.getF(); 113 | json.put("time", System.currentTimeMillis()); 114 | json.put("info", odbacts.data); 115 | return json; 116 | } 117 | 118 | private JSONObject toolNum() { 119 | JSONObject json = new JSONObject(); 120 | json.put("info", fanucCncAPI.getToolInfo()); 121 | json.put("time", System.currentTimeMillis()); 122 | return json; 123 | } 124 | 125 | private JSONObject toolCount() { 126 | JSONObject json = new JSONObject(); 127 | json.put("info", fanucCncAPI.getToolMachinedPart()); 128 | json.put("time", System.currentTimeMillis()); 129 | return json; 130 | } 131 | 132 | private JSONObject resistance() { 133 | JSONObject json = new JSONObject(); 134 | json.put("info", fanucCncAPI.getResistance()); 135 | json.put("time", System.currentTimeMillis()); 136 | return json; 137 | } 138 | 139 | /** 140 | * 获取数据单位 141 | * 142 | * @param type 143 | * @return 144 | */ 145 | public String getUnitType(Short type) { 146 | String unit = ""; 147 | switch (type) { 148 | case 0: 149 | unit = "mm"; 150 | break; 151 | case 1: 152 | unit = "inch"; 153 | break; 154 | case 2: 155 | unit = "degree"; 156 | break; 157 | default: 158 | break; 159 | } 160 | return unit; 161 | } 162 | 163 | /** 164 | * 0 : absolute position 165 | * 1 : machine position 166 | * 2 : relative position 167 | * 3 : distance to go 168 | * -1 : all type 169 | *

170 | * long data; position data 171 | * short dec; place of decimal point of positiondata 172 | * short unit; unit of position data 173 | * short disp; status of display 174 | * char name; axis name 175 | * char suff; subscript of axis name 176 | * 177 | * @param odbpos 178 | * @param type 179 | * @param axis 180 | * @return 181 | */ 182 | public JSONArray asisInfo(Fanuc.ODBPOS odbpos, int type, Set axis) { 183 | JSONArray resJson = new JSONArray(); 184 | Class userCla = odbpos.getClass(); 185 | 186 | for (int i = 1; i <= Fanuc.MAX_AXIS; i++) { 187 | try { 188 | Field f = userCla.getField("p" + i); 189 | f.setAccessible(true); 190 | Fanuc.POSELMALL p = (Fanuc.POSELMALL) f.get(odbpos); 191 | if (p != null) { 192 | String axisName = ((char) p.abs.name) + ""; 193 | if (!axis.contains(axisName)) { 194 | continue; 195 | } 196 | JSONObject json = new JSONObject(); 197 | json.put("name", axisName); 198 | switch (type) { 199 | case -1: { 200 | json.put("abs", formatAxisInfo(p.abs)); 201 | json.put("mach", formatAxisInfo(p.mach)); 202 | json.put("rel", formatAxisInfo(p.rel)); 203 | json.put("dist", formatAxisInfo(p.dist)); 204 | break; 205 | } 206 | case 0: { 207 | json.put("abs", formatAxisInfo(p.abs)); 208 | break; 209 | } 210 | case 1: { 211 | json.put("mach", formatAxisInfo(p.mach)); 212 | break; 213 | } 214 | case 2: { 215 | json.put("rel", formatAxisInfo(p.rel)); 216 | break; 217 | } 218 | case 3: { 219 | json.put("dist", formatAxisInfo(p.dist)); 220 | break; 221 | } 222 | default: { 223 | 224 | } 225 | } 226 | 227 | resJson.put(json); 228 | } 229 | } catch (NoSuchFieldException e) { 230 | e.printStackTrace(); 231 | } catch (IllegalAccessException e) { 232 | e.printStackTrace(); 233 | } 234 | } 235 | return resJson; 236 | } 237 | 238 | /** 239 | * 处理主轴位置参数 240 | * 241 | * @param info 242 | * @return 243 | */ 244 | public JSONObject formatAxisInfo(Fanuc.POSELM info) { 245 | JSONObject json = new JSONObject(); 246 | short point = info.dec; 247 | int data = info.data.intValue(); 248 | double position = data * Math.pow(10, -point); 249 | json.put("data", position); 250 | json.put("unit", getUnitType(info.unit)); 251 | return json; 252 | } 253 | 254 | } 255 | -------------------------------------------------------------------------------- /src/main/java/daiyun/cnc/fanuc/sdk/FanucCncAPI.java: -------------------------------------------------------------------------------- 1 | package daiyun.cnc.fanuc.sdk; 2 | 3 | import org.json.JSONObject; 4 | 5 | /** 6 | * @author daiyun 7 | * @date 2018-06-28 10:45. 8 | */ 9 | public class FanucCncAPI { 10 | 11 | protected String ip; 12 | protected short port; 13 | protected long timeout; 14 | 15 | protected Fanuc fanuc = Fanuc.FANUC; 16 | protected final int MAX_ASIS = 8; 17 | protected int[] flibHndl = new int[MAX_ASIS]; 18 | 19 | public FanucCncAPI(String ip, short port, long timeout) { 20 | this.ip = ip; 21 | this.port = port; 22 | this.timeout = timeout; 23 | } 24 | 25 | /** 26 | * connect 27 | * 28 | * @return 29 | */ 30 | public boolean connectCNC() { 31 | short res = fanuc.cnc_allclibhndl3(ip, port, timeout, flibHndl); 32 | if (res != Fanuc.EW_OK) { 33 | return false; 34 | } 35 | return true; 36 | } 37 | 38 | /** 39 | * free handle 40 | */ 41 | public void cancle() { 42 | fanuc.cnc_freelibhndl(flibHndl[0]); 43 | } 44 | 45 | /** 46 | * position 47 | * 48 | * @param type 49 | * @return 50 | */ 51 | public Fanuc.ODBPOS getPosition(Integer type) { 52 | int[] maxAsis = new int[MAX_ASIS]; 53 | maxAsis[0] = MAX_ASIS; 54 | Fanuc.ODBPOS odbpos = new Fanuc.ODBPOS(); 55 | short res = fanuc.cnc_rdposition(flibHndl[0], type.shortValue(), maxAsis, odbpos); 56 | if (res != Fanuc.EW_OK) { 57 | return null; 58 | } 59 | return odbpos; 60 | } 61 | 62 | /** 63 | * 获取机台进给 64 | * 65 | * @return 66 | */ 67 | public Fanuc.ODBACT getF() { 68 | Fanuc.ODBACT odbact = new Fanuc.ODBACT(); 69 | short res = fanuc.cnc_actf(flibHndl[0], odbact); 70 | if (res != Fanuc.EW_OK) { 71 | return null; 72 | } 73 | return odbact; 74 | } 75 | 76 | /** 77 | * 获取机台转速 78 | * 79 | * @return 80 | */ 81 | public Fanuc.ODBACT getS() { 82 | Fanuc.ODBACT spindle = new Fanuc.ODBACT(); 83 | short res = fanuc.cnc_acts(flibHndl[0], spindle); 84 | if (res != Fanuc.EW_OK) { 85 | return null; 86 | } 87 | return spindle; 88 | } 89 | 90 | /** 91 | * 获取机台指定轴转速 92 | * 93 | * @return 94 | */ 95 | public Fanuc.ODBACT2[] get2S(Integer type) { 96 | Fanuc.ODBACT2 spindle[] = new Fanuc.ODBACT2[8]; 97 | short res = fanuc.cnc_acts2(flibHndl[0], type.shortValue(), spindle); 98 | if (res != Fanuc.EW_OK) { 99 | return null; 100 | } 101 | return spindle; 102 | } 103 | 104 | 105 | /** 106 | * 获取工具使用次数 107 | * @return 108 | */ 109 | public int getToolMachinedPart() { 110 | int count = -1; 111 | Fanuc.ODBUSEGRP odbusegrp = new Fanuc.ODBUSEGRP(); 112 | short ret = fanuc.cnc_rdtlusegrp(flibHndl[0], odbusegrp); 113 | if (ret != Fanuc.EW_OK || odbusegrp.use == 0) { 114 | return -1; 115 | } 116 | 117 | Fanuc.ODBTG toolGroupInfo = new Fanuc.ODBTG(); 118 | ret = fanuc.cnc_rdtoolgrp(flibHndl[0], (short) odbusegrp.use, (short) (20 + 20 * 1), toolGroupInfo); 119 | if (ret == Fanuc.EW_OK) { 120 | System.out.println("正在运行的刀片组号;" + odbusegrp.use + " 寿命:" + toolGroupInfo.life + " 次数:" + toolGroupInfo.count); 121 | count = toolGroupInfo.count; 122 | } 123 | 124 | return count; 125 | } 126 | 127 | /** 128 | * 获取工具使用信息,含寿命、使用次数、当前工具编号等信息 129 | * @return 130 | */ 131 | public JSONObject getToolInfo() { 132 | 133 | Fanuc.ODBUSEGRP toolGroup = new Fanuc.ODBUSEGRP(); 134 | short ret = fanuc.cnc_rdtlusegrp(flibHndl[0], toolGroup); 135 | if (ret != Fanuc.EW_OK || toolGroup.use == 0) { 136 | return null; 137 | } 138 | 139 | Fanuc.ODBTG toolGroupInfo = new Fanuc.ODBTG(); 140 | ret = fanuc.cnc_rdtoolgrp(flibHndl[0], (short) toolGroup.use, (short) (20 + 20 * 4), toolGroupInfo); 141 | if (ret != Fanuc.EW_OK) { 142 | return null; 143 | } 144 | 145 | JSONObject json = new JSONObject(); 146 | json.put("currentTool",toolGroupInfo.data.data1.tool_num); 147 | json.put("life",toolGroupInfo.life); 148 | json.put("count",toolGroupInfo.count); 149 | return json; 150 | } 151 | 152 | /** 153 | * 函数作用待探明 154 | * @return 155 | */ 156 | public double getResistance() { 157 | return 0.0; 158 | } 159 | 160 | 161 | /** 162 | * 获取机台刀号 163 | * TODO: 全是信息打印,作用需要明确 164 | */ 165 | public void getTools() { 166 | Fanuc.ODBTLIFE5 tool = new Fanuc.ODBTLIFE5();//读取刀片组的序号 167 | Fanuc.ODBTLIFE2 tool1 = new Fanuc.ODBTLIFE2();//读取刀片组的全部数量 168 | Fanuc.ODBTLIFE3 tool2 = new Fanuc.ODBTLIFE3();//刀具的数量 169 | Fanuc.ODBTLIFE4 life = new Fanuc.ODBTLIFE4(); 170 | Fanuc.ODBTG btg = new Fanuc.ODBTG(); 171 | Fanuc.ODBUSEGRP grp = new Fanuc.ODBUSEGRP(); 172 | Fanuc.IODBTGI btgi = new Fanuc.IODBTGI(); 173 | 174 | short start = 0;//开始的刀具组 175 | short end = 22;//结束的刀具组 176 | 177 | short length = (short) (8 + 16 * (end - start)); 178 | short ret = fanuc.cnc_rdgrpinfo(flibHndl[0], start, end, length, btgi); 179 | System.out.println("cnc_rdgrpinfo:" + ret); 180 | 181 | if (ret == 0) { 182 | System.out.println(btgi.data.data1.count_value); 183 | } 184 | 185 | int m = 2; 186 | ret = fanuc.cnc_rdgrpid2(flibHndl[0], m, tool); 187 | System.out.println("cnc_rdgrpid2:" + ret); 188 | 189 | ret = fanuc.cnc_rdngrp(flibHndl[0], tool1);//刀片组的全部数量 190 | System.out.println("all:" + ret); 191 | 192 | short group_numer = (short) (tool.data); 193 | 194 | ret = fanuc.cnc_rdntool(flibHndl[0], group_numer, tool2);//刀具的数量 195 | System.out.println("cnc_rdntool:" + ret); 196 | 197 | ret = fanuc.cnc_rdlife(flibHndl[0], group_numer, tool2);//刀具寿命 198 | System.out.println("cnc_rdlife:" + ret); 199 | 200 | ret = fanuc.cnc_rdcount(flibHndl[0], group_numer, tool2);//刀具计时器 201 | System.out.println("cnc_rdcount:" + ret); 202 | 203 | ret = fanuc.cnc_rdtoolgrp(flibHndl[0], (short) 2, (short) (20 + 20 * 1), btg);//根据刀组号读出所有信息,很重要; 204 | System.out.println("cnc_rdtoolgrp:" + ret); 205 | 206 | ret = fanuc.cnc_rdtlusegrp(flibHndl[0], grp);//读出正在使用的到组号; 207 | System.out.println("cnc_rdtlusegrp:" + ret); 208 | 209 | System.out.println("刀片组号:" + group_numer); 210 | System.out.println("type:" + tool.type); 211 | System.out.println("刀片组的全部数量" + btgi.data.data1.count_value); 212 | System.out.println("刀片号:" + tool2.data); 213 | System.out.println("刀片组号;" + group_numer + " 寿命:" + tool2.data); 214 | System.out.println("刀片组号;" + group_numer + " 寿命计时:" + tool2.data); 215 | 216 | /* 217 | Fanuc.ODBUSEGRP toolGroup = new Fanuc.ODBUSEGRP(); 218 | Fanuc.ODBTG toolGroupInfo = new Fanuc.ODBTG(); 219 | 220 | short ret = Fanuc.cnc_rdtlusegrp(flibHndl[0], toolGroup); 221 | 222 | System.out.println("===" + ret); 223 | System.out.println(toolGroup.use); 224 | 225 | Integer usingToolGroupNumber = toolGroup.use; 226 | System.out.println(usingToolGroupNumber); 227 | 228 | short length = 20 + 20 * 4; 229 | 230 | ret = Fanuc.cnc_rdtoolgrp(flibHndl[0], usingToolGroupNumber.shortValue(), length, toolGroupInfo); 231 | 232 | System.out.println("ret:" + ret); 233 | System.out.println(toolGroupInfo.data.data1.tool_num); 234 | System.out.println(toolGroupInfo);*/ 235 | } 236 | 237 | /** 238 | * 获取机台负载 239 | * 240 | * @param type 241 | * @return 242 | */ 243 | public Fanuc.ODBSPN[] getLoad(Integer type) { 244 | Fanuc.ODBSPN[] odbspn = new Fanuc.ODBSPN[8]; 245 | short res = fanuc.cnc_rdspload(flibHndl[0], type.shortValue(), odbspn); 246 | if (res != Fanuc.EW_OK) { 247 | System.out.println("err:" + res); 248 | return null; 249 | } 250 | return odbspn; 251 | } 252 | 253 | 254 | /** 255 | * cnc status infomation 256 | * 获取CNC 状态信息 257 | * 258 | * @return 259 | */ 260 | public Fanuc.ODBST getCncStatus() { 261 | Fanuc.ODBST odbst = new Fanuc.ODBST(); 262 | short res = fanuc.cnc_statinfo(flibHndl[0], odbst); 263 | if (res != Fanuc.EW_OK) { 264 | System.out.println("err:" + res); 265 | return null; 266 | } 267 | return odbst; 268 | } 269 | 270 | /** 271 | * 获取加工工件数量 272 | * TODO: 这个函数好像没有写完 273 | */ 274 | public void getProcessTimes() { 275 | Fanuc.ODBPARANUM odbparanum = new Fanuc.ODBPARANUM(); 276 | 277 | fanuc.cnc_rdparanum(flibHndl[0], odbparanum); 278 | 279 | Fanuc.IODBPSD iodbpsd2s = new Fanuc.IODBPSD(); 280 | 281 | short[] s_number = new short[8]; 282 | s_number[0] = 6712; 283 | 284 | short[] e_number = new short[8]; 285 | e_number[0] = 6712; 286 | 287 | short[] length = new short[8]; 288 | length[0] = 16; 289 | 290 | short res = fanuc.cnc_rdparar(flibHndl[0], s_number, (short) -1, e_number, length, iodbpsd2s); 291 | 292 | System.out.println(); 293 | } 294 | 295 | } 296 | -------------------------------------------------------------------------------- /src/main/java/daiyun/cnc/fanuc/sdk/GetClassAttr.java: -------------------------------------------------------------------------------- 1 | package daiyun.cnc.fanuc.sdk; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * @author daiyun 9 | * @date 2018-07-04 14:39. 10 | */ 11 | public class GetClassAttr { 12 | public static List allAttribute(Class c) { 13 | List list = new ArrayList<>(); 14 | Field[] fields = c.getDeclaredFields(); 15 | for (Field field : fields) { 16 | String name = field.getName(); 17 | if (name.length() > 0) { 18 | list.add(name); 19 | } 20 | } 21 | return list; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/daiyun/cnc/fanuc/sdk/JnaStructure.java: -------------------------------------------------------------------------------- 1 | package daiyun.cnc.fanuc.sdk; 2 | 3 | import com.sun.jna.Structure; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author daiyun 9 | * @date 2018-07-04 14:45. 10 | */ 11 | public class JnaStructure extends Structure { 12 | 13 | @Override 14 | protected List getFieldOrder() { 15 | return GetClassAttr.allAttribute(this.getClass()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/daiyun/cnc/fanuc/sdk/MyStructure.java: -------------------------------------------------------------------------------- 1 | package daiyun.cnc.fanuc.sdk; 2 | 3 | import com.sun.jna.Structure; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author daiyun 9 | * @date 2018-07-04 14:45. 10 | */ 11 | public class MyStructure extends Structure { 12 | 13 | @Override 14 | protected List getFieldOrder() { 15 | return GetClassAttr.allAttribute(this.getClass()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/daiyun/cnc/fanuc/sdk/OutObject.java: -------------------------------------------------------------------------------- 1 | package daiyun.cnc.fanuc.sdk; 2 | 3 | //---------------------------------------------------------------------------------------- 4 | // Copyright © 2007 - 2018 Tangible Software Solutions Inc. 5 | // This class can be used by anyone provided that the copyright notice remains intact. 6 | // 7 | // This class is used to replicate the ability to have 'out' parameters in Java. 8 | //---------------------------------------------------------------------------------------- 9 | public final class OutObject { 10 | public T argValue; 11 | } -------------------------------------------------------------------------------- /src/main/java/daiyun/cnc/fanuc/sdk/RefObject.java: -------------------------------------------------------------------------------- 1 | package daiyun.cnc.fanuc.sdk; 2 | 3 | //---------------------------------------------------------------------------------------- 4 | // Copyright © 2007 - 2018 Tangible Software Solutions Inc. 5 | // This class can be used by anyone provided that the copyright notice remains intact. 6 | // 7 | // This class is used to replicate the ability to pass arguments by reference in Java. 8 | //---------------------------------------------------------------------------------------- 9 | public final class RefObject { 10 | public T argValue; 11 | 12 | public RefObject(T refArg) { 13 | argValue = refArg; 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/daiyun/cnc/fanuc/sdk/StringHelper.java: -------------------------------------------------------------------------------- 1 | package daiyun.cnc.fanuc.sdk; 2 | 3 | //---------------------------------------------------------------------------------------- 4 | // Copyright © 2007 - 2018 Tangible Software Solutions Inc. 5 | // This class can be used by anyone provided that the copyright notice remains intact. 6 | // 7 | // This class is used to replicate some .NET string methods in Java. 8 | //---------------------------------------------------------------------------------------- 9 | public final class StringHelper { 10 | //------------------------------------------------------------------------------------ 11 | // This method replaces the .NET string method 'Substring' when 'start' is a method 12 | // call or calculated value to ensure that 'start' is obtained just once. 13 | //------------------------------------------------------------------------------------ 14 | public static String substring(String string, int start, int length) { 15 | if (length < 0) 16 | throw new IndexOutOfBoundsException("Parameter length cannot be negative."); 17 | 18 | return string.substring(start, start + length); 19 | } 20 | 21 | //------------------------------------------------------------------------------------ 22 | // This method replaces the .NET static string method 'IsNullOrEmpty'. 23 | //------------------------------------------------------------------------------------ 24 | public static boolean isNullOrEmpty(String string) { 25 | return string == null || string.length() == 0; 26 | } 27 | 28 | //------------------------------------------------------------------------------------ 29 | // This method replaces the .NET static string method 'IsNullOrWhiteSpace'. 30 | //------------------------------------------------------------------------------------ 31 | public static boolean isNullOrWhiteSpace(String string) { 32 | if (string == null) 33 | return true; 34 | 35 | for (int index = 0; index < string.length(); index++) { 36 | if (!Character.isWhitespace(string.charAt(index))) 37 | return false; 38 | } 39 | 40 | return true; 41 | } 42 | 43 | //------------------------------------------------------------------------------------ 44 | // This method replaces the .NET static string method 'Join' (2 parameter version). 45 | //------------------------------------------------------------------------------------ 46 | public static String join(String separator, String[] stringArray) { 47 | if (stringArray == null) 48 | return null; 49 | else 50 | return join(separator, stringArray, 0, stringArray.length); 51 | } 52 | 53 | //------------------------------------------------------------------------------------ 54 | // This method replaces the .NET static string method 'Join' (4 parameter version). 55 | //------------------------------------------------------------------------------------ 56 | public static String join(String separator, String[] stringArray, int startIndex, int count) { 57 | String result = ""; 58 | 59 | if (stringArray == null) 60 | return null; 61 | 62 | for (int index = startIndex; index < stringArray.length && index - startIndex < count; index++) { 63 | if (separator != null && index > startIndex) 64 | result += separator; 65 | 66 | if (stringArray[index] != null) 67 | result += stringArray[index]; 68 | } 69 | 70 | return result; 71 | } 72 | 73 | //------------------------------------------------------------------------------------ 74 | // This method replaces the .NET string method 'Remove' (1 parameter version). 75 | //------------------------------------------------------------------------------------ 76 | public static String remove(String string, int start) { 77 | return string.substring(0, start); 78 | } 79 | 80 | //------------------------------------------------------------------------------------ 81 | // This method replaces the .NET string method 'Remove' (2 parameter version). 82 | //------------------------------------------------------------------------------------ 83 | public static String remove(String string, int start, int count) { 84 | return string.substring(0, start) + string.substring(start + count); 85 | } 86 | 87 | //------------------------------------------------------------------------------------ 88 | // This method replaces the .NET string method 'TrimEnd'. 89 | //------------------------------------------------------------------------------------ 90 | public static String trimEnd(String string, Character... charsToTrim) { 91 | if (string == null || charsToTrim == null) 92 | return string; 93 | 94 | int lengthToKeep = string.length(); 95 | for (int index = string.length() - 1; index >= 0; index--) { 96 | boolean removeChar = false; 97 | if (charsToTrim.length == 0) { 98 | if (Character.isWhitespace(string.charAt(index))) { 99 | lengthToKeep = index; 100 | removeChar = true; 101 | } 102 | } else { 103 | for (int trimCharIndex = 0; trimCharIndex < charsToTrim.length; trimCharIndex++) { 104 | if (string.charAt(index) == charsToTrim[trimCharIndex]) { 105 | lengthToKeep = index; 106 | removeChar = true; 107 | break; 108 | } 109 | } 110 | } 111 | if (!removeChar) 112 | break; 113 | } 114 | return string.substring(0, lengthToKeep); 115 | } 116 | 117 | //------------------------------------------------------------------------------------ 118 | // This method replaces the .NET string method 'TrimStart'. 119 | //------------------------------------------------------------------------------------ 120 | public static String trimStart(String string, Character... charsToTrim) { 121 | if (string == null || charsToTrim == null) 122 | return string; 123 | 124 | int startingIndex = 0; 125 | for (int index = 0; index < string.length(); index++) { 126 | boolean removeChar = false; 127 | if (charsToTrim.length == 0) { 128 | if (Character.isWhitespace(string.charAt(index))) { 129 | startingIndex = index + 1; 130 | removeChar = true; 131 | } 132 | } else { 133 | for (int trimCharIndex = 0; trimCharIndex < charsToTrim.length; trimCharIndex++) { 134 | if (string.charAt(index) == charsToTrim[trimCharIndex]) { 135 | startingIndex = index + 1; 136 | removeChar = true; 137 | break; 138 | } 139 | } 140 | } 141 | if (!removeChar) 142 | break; 143 | } 144 | return string.substring(startingIndex); 145 | } 146 | 147 | //------------------------------------------------------------------------------------ 148 | // This method replaces the .NET string method 'Trim' when arguments are used. 149 | //------------------------------------------------------------------------------------ 150 | public static String trim(String string, Character... charsToTrim) { 151 | return trimEnd(trimStart(string, charsToTrim), charsToTrim); 152 | } 153 | 154 | //------------------------------------------------------------------------------------ 155 | // This method is used for string equality comparisons when the option 156 | // 'Use helper 'stringsEqual' method to handle null strings' is selected 157 | // (The Java String 'equals' method can't be called on a null instance). 158 | //------------------------------------------------------------------------------------ 159 | public static boolean stringsEqual(String s1, String s2) { 160 | if (s1 == null && s2 == null) 161 | return true; 162 | else 163 | return s1 != null && s1.equals(s2); 164 | } 165 | 166 | //------------------------------------------------------------------------------------ 167 | // This method replaces the .NET string method 'PadRight' (1 parameter version). 168 | //------------------------------------------------------------------------------------ 169 | public static String padRight(String string, int totalWidth) { 170 | return padRight(string, totalWidth, ' '); 171 | } 172 | 173 | //------------------------------------------------------------------------------------ 174 | // This method replaces the .NET string method 'PadRight' (2 parameter version). 175 | //------------------------------------------------------------------------------------ 176 | public static String padRight(String string, int totalWidth, char paddingChar) { 177 | StringBuilder sb = new StringBuilder(string); 178 | 179 | while (sb.length() < totalWidth) { 180 | sb.append(paddingChar); 181 | } 182 | 183 | return sb.toString(); 184 | } 185 | 186 | //------------------------------------------------------------------------------------ 187 | // This method replaces the .NET string method 'PadLeft' (1 parameter version). 188 | //------------------------------------------------------------------------------------ 189 | public static String padLeft(String string, int totalWidth) { 190 | return padLeft(string, totalWidth, ' '); 191 | } 192 | 193 | //------------------------------------------------------------------------------------ 194 | // This method replaces the .NET string method 'PadLeft' (2 parameter version). 195 | //------------------------------------------------------------------------------------ 196 | public static String padLeft(String string, int totalWidth, char paddingChar) { 197 | StringBuilder sb = new StringBuilder(""); 198 | 199 | while (sb.length() + string.length() < totalWidth) { 200 | sb.append(paddingChar); 201 | } 202 | 203 | sb.append(string); 204 | return sb.toString(); 205 | } 206 | 207 | //------------------------------------------------------------------------------------ 208 | // This method replaces the .NET string constructor which repeats a character. 209 | //------------------------------------------------------------------------------------ 210 | public static String repeatChar(char charToRepeat, int count) { 211 | String newString = ""; 212 | for (int i = 1; i <= count; i++) { 213 | newString += charToRepeat; 214 | } 215 | return newString; 216 | } 217 | 218 | //------------------------------------------------------------------------------------ 219 | // This method replaces the .NET string method 'LastIndexOf' (char version). 220 | //------------------------------------------------------------------------------------ 221 | public static int lastIndexOf(String string, char value, int startIndex, int count) { 222 | int leftMost = startIndex + 1 - count; 223 | int rightMost = startIndex + 1; 224 | String substring = string.substring(leftMost, rightMost); 225 | int lastIndexInSubstring = substring.lastIndexOf(value); 226 | if (lastIndexInSubstring < 0) 227 | return -1; 228 | else 229 | return lastIndexInSubstring + leftMost; 230 | } 231 | 232 | //------------------------------------------------------------------------------------ 233 | // This method replaces the .NET string method 'LastIndexOf' (string version). 234 | //------------------------------------------------------------------------------------ 235 | public static int lastIndexOf(String string, String value, int startIndex, int count) { 236 | int leftMost = startIndex + 1 - count; 237 | int rightMost = startIndex + 1; 238 | String substring = string.substring(leftMost, rightMost); 239 | int lastIndexInSubstring = substring.lastIndexOf(value); 240 | if (lastIndexInSubstring < 0) 241 | return -1; 242 | else 243 | return lastIndexInSubstring + leftMost; 244 | } 245 | 246 | //------------------------------------------------------------------------------------ 247 | // This method replaces the .NET string method 'IndexOfAny' (1 parameter version). 248 | //------------------------------------------------------------------------------------ 249 | public static int indexOfAny(String string, char[] anyOf) { 250 | int lowestIndex = -1; 251 | for (char c : anyOf) { 252 | int index = string.indexOf(c); 253 | if (index > -1) { 254 | if (lowestIndex == -1 || index < lowestIndex) { 255 | lowestIndex = index; 256 | 257 | if (index == 0) 258 | break; 259 | } 260 | } 261 | } 262 | 263 | return lowestIndex; 264 | } 265 | 266 | //------------------------------------------------------------------------------------ 267 | // This method replaces the .NET string method 'IndexOfAny' (2 parameter version). 268 | //------------------------------------------------------------------------------------ 269 | public static int indexOfAny(String string, char[] anyOf, int startIndex) { 270 | int indexInSubstring = indexOfAny(string.substring(startIndex), anyOf); 271 | if (indexInSubstring == -1) 272 | return -1; 273 | else 274 | return indexInSubstring + startIndex; 275 | } 276 | 277 | //------------------------------------------------------------------------------------ 278 | // This method replaces the .NET string method 'IndexOfAny' (3 parameter version). 279 | //------------------------------------------------------------------------------------ 280 | public static int indexOfAny(String string, char[] anyOf, int startIndex, int count) { 281 | int endIndex = startIndex + count; 282 | int indexInSubstring = indexOfAny(string.substring(startIndex, endIndex), anyOf); 283 | if (indexInSubstring == -1) 284 | return -1; 285 | else 286 | return indexInSubstring + startIndex; 287 | } 288 | 289 | //------------------------------------------------------------------------------------ 290 | // This method replaces the .NET string method 'LastIndexOfAny' (1 parameter version). 291 | //------------------------------------------------------------------------------------ 292 | public static int lastIndexOfAny(String string, char[] anyOf) { 293 | int highestIndex = -1; 294 | for (char c : anyOf) { 295 | int index = string.lastIndexOf(c); 296 | if (index > highestIndex) { 297 | highestIndex = index; 298 | 299 | if (index == string.length() - 1) 300 | break; 301 | } 302 | } 303 | 304 | return highestIndex; 305 | } 306 | 307 | //------------------------------------------------------------------------------------ 308 | // This method replaces the .NET string method 'LastIndexOfAny' (2 parameter version). 309 | //------------------------------------------------------------------------------------ 310 | public static int lastIndexOfAny(String string, char[] anyOf, int startIndex) { 311 | String substring = string.substring(0, startIndex + 1); 312 | int lastIndexInSubstring = lastIndexOfAny(substring, anyOf); 313 | if (lastIndexInSubstring < 0) 314 | return -1; 315 | else 316 | return lastIndexInSubstring; 317 | } 318 | 319 | //------------------------------------------------------------------------------------ 320 | // This method replaces the .NET string method 'LastIndexOfAny' (3 parameter version). 321 | //------------------------------------------------------------------------------------ 322 | public static int lastIndexOfAny(String string, char[] anyOf, int startIndex, int count) { 323 | int leftMost = startIndex + 1 - count; 324 | int rightMost = startIndex + 1; 325 | String substring = string.substring(leftMost, rightMost); 326 | int lastIndexInSubstring = lastIndexOfAny(substring, anyOf); 327 | if (lastIndexInSubstring < 0) 328 | return -1; 329 | else 330 | return lastIndexInSubstring + leftMost; 331 | } 332 | 333 | } -------------------------------------------------------------------------------- /src/main/java/daiyun/conf/Configurable.java: -------------------------------------------------------------------------------- 1 | package daiyun.conf; 2 | 3 | public interface Configurable { 4 | 5 | /** 6 | * ser the configuration to be used by this object 7 | * 8 | * @param configuration 9 | */ 10 | public void setConf(Configuration configuration); 11 | 12 | /** 13 | * return the configuration used by this object 14 | * 15 | * @return 16 | */ 17 | Configuration getConf(); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/daiyun/conf/Configuration.java: -------------------------------------------------------------------------------- 1 | package daiyun.conf; 2 | 3 | import com.google.common.base.Strings; 4 | import org.apache.logging.log4j.LogManager; 5 | import org.apache.logging.log4j.Logger; 6 | 7 | import java.io.FileInputStream; 8 | import java.io.IOException; 9 | import java.io.InputStreamReader; 10 | import java.util.Properties; 11 | 12 | public class Configuration { 13 | 14 | private static final Logger logger = LogManager.getLogger(Configuration.class); 15 | 16 | private final Properties props = new Properties(); 17 | 18 | public Configuration() { 19 | } 20 | 21 | /** 22 | * 通过文件添加配置文件. 23 | * 24 | * @param filename 25 | * @throws IOException 26 | */ 27 | public void addConfiguration(String filename) { 28 | try { 29 | props.load(new InputStreamReader(new FileInputStream(filename), "UTF-8")); 30 | } catch (IOException e) { 31 | logger.info("添加配置文件失败"); 32 | } 33 | } 34 | 35 | /** 36 | * 添加resources 下配置文件. 37 | * 38 | * @param filename 39 | */ 40 | public void addRootResources(String filename) { 41 | try { 42 | props.load(getClass().getClassLoader().getResourceAsStream(filename)); 43 | } catch (IOException e) { 44 | logger.error("can't load resources:" + filename); 45 | } 46 | } 47 | 48 | /** 49 | * 通过classpath resource添加配置文件,同一包内的resource文件按相对路径 50 | * ,不同包的按绝对路径. 51 | * addResources("/com/rainmaker/haha.properties") 52 | * addResources("haha.properties") 53 | * 54 | * @param resourcesPath 55 | */ 56 | public void addResources(String resourcesPath) { 57 | try { 58 | props.load(getClass().getResourceAsStream(resourcesPath)); 59 | } catch (IOException e) { 60 | logger.error("can't load resources:" + resourcesPath); 61 | } 62 | } 63 | 64 | 65 | /** 66 | * 清空配置. 67 | */ 68 | public void clear() { 69 | props.clear(); 70 | } 71 | 72 | public Boolean getBoolean(String key, Boolean defaultValue) { 73 | String value = getTrimed(key); 74 | if (!Strings.isNullOrEmpty(value)) { 75 | return Boolean.parseBoolean(value.trim()); 76 | } 77 | return defaultValue; 78 | } 79 | 80 | public Boolean getBoolean(String key) { 81 | return getBoolean(key, null); 82 | } 83 | 84 | public Integer getInteger(String key, Integer defaultValue) { 85 | String value = getTrimed(key); 86 | if (!Strings.isNullOrEmpty(value)) { 87 | return Integer.parseInt(value.trim()); 88 | } 89 | return defaultValue; 90 | } 91 | 92 | public Integer getInteger(String key) { 93 | return getInteger(key, null); 94 | } 95 | 96 | public Long getLong(String key, Long defaultValue) { 97 | String value = getTrimed(key); 98 | if (!Strings.isNullOrEmpty(value)) { 99 | return Long.parseLong(value.trim()); 100 | } 101 | return defaultValue; 102 | } 103 | 104 | public Long getLong(String key) { 105 | return getLong(key, null); 106 | } 107 | 108 | public Float getFloat(String key, Float defaultValue) { 109 | String value = getTrimed(key); 110 | if (!Strings.isNullOrEmpty(value)) { 111 | return Float.parseFloat(value.trim()); 112 | } 113 | return defaultValue; 114 | } 115 | 116 | public Float getFloat(String key) { 117 | return getFloat(key, null); 118 | } 119 | 120 | public String getString(String key, String defaultValue) { 121 | return get(key, defaultValue); 122 | } 123 | 124 | public String getString(String key) { 125 | return get(key); 126 | } 127 | 128 | private String get(String key, String defaultValue) { 129 | String value = getTrimed(key); 130 | if (!Strings.isNullOrEmpty(value)) { 131 | return value; 132 | } 133 | return defaultValue; 134 | } 135 | 136 | private String get(String key) { 137 | return get(key, null); 138 | } 139 | 140 | public Class getClass(String key, Class defaultValue) { 141 | String value = getTrimed(key); 142 | if (!Strings.isNullOrEmpty(value)) { 143 | try { 144 | Class clazz = Class.forName(value); 145 | return clazz; 146 | } catch (ClassNotFoundException e) { 147 | return defaultValue; 148 | } 149 | } 150 | return defaultValue; 151 | } 152 | 153 | public Class getClass(String key) { 154 | return getClass(key, null); 155 | } 156 | 157 | public Properties getProps() { 158 | return props; 159 | } 160 | 161 | private String getTrimed(String key) { 162 | String val = props.getProperty(key); 163 | return val != null ? val.trim() : null; 164 | } 165 | 166 | @Override 167 | public String toString() { 168 | return "Configuration{" + 169 | "props=" + props + 170 | '}'; 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /src/main/java/daiyun/conf/Configured.java: -------------------------------------------------------------------------------- 1 | package daiyun.conf; 2 | 3 | public class Configured implements Configurable { 4 | private Configuration configuration; 5 | 6 | public Configured(Configuration configuration) { 7 | this.configuration = configuration; 8 | } 9 | 10 | @Override 11 | public void setConf(Configuration configuration) { 12 | this.configuration = configuration; 13 | } 14 | 15 | @Override 16 | public Configuration getConf() { 17 | return configuration; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/daiyun/conf/SysConfig.java: -------------------------------------------------------------------------------- 1 | package daiyun.conf; 2 | 3 | import java.io.File; 4 | 5 | /** 6 | * @author daiyun 7 | * @date 2018-06-14 15:44. 8 | */ 9 | public class SysConfig { 10 | 11 | public static Configuration conf = new Configuration(); 12 | 13 | static { 14 | conf.addConfiguration("conf" + File.separator + "conf.properties"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/daiyun/utils/LocalFlieUtil.java: -------------------------------------------------------------------------------- 1 | package daiyun.utils; 2 | 3 | import org.apache.logging.log4j.LogManager; 4 | import org.apache.logging.log4j.Logger; 5 | 6 | import java.io.*; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Set; 11 | 12 | /** 13 | * @author daiyun 14 | * @date 2016-09-12 14:42. 15 | */ 16 | public class LocalFlieUtil { 17 | 18 | private static final Logger LOGGER = LogManager.getLogger(LocalFlieUtil.class.getName()); 19 | 20 | private LocalFlieUtil() { 21 | } 22 | 23 | /** 24 | * 以追加的方式向文件中新写入一行数据. 25 | * 26 | * @param writeFilePath 写入的文件 27 | * @param content 写入的内容 28 | * @return 写入成功标志 29 | */ 30 | public static synchronized boolean appendFile(String writeFilePath, String content) { 31 | try { 32 | FileWriter writer = new FileWriter(writeFilePath, true); 33 | writer.write(content); 34 | writer.write(System.getProperty("line.separator")); 35 | writer.close(); 36 | } catch (IOException e) { 37 | e.printStackTrace(); 38 | } 39 | return true; 40 | } 41 | 42 | public static synchronized boolean appendFile(String filePath, String fileName, String content) { 43 | createFile(filePath, fileName); 44 | try { 45 | FileWriter writer = new FileWriter(filePath + File.separator + fileName, true); 46 | writer.write(content); 47 | writer.write(System.getProperty("line.separator")); 48 | writer.close(); 49 | } catch (IOException e) { 50 | e.printStackTrace(); 51 | } 52 | return true; 53 | } 54 | 55 | /** 56 | * 多行文件写入. 57 | * 58 | * @param fileIndex 59 | * @param fileName 60 | * @param contentList 61 | */ 62 | public static synchronized void saveFile(String fileIndex, String fileName, List contentList) { 63 | createFile(fileIndex, fileName); 64 | String filePath = fileIndex + File.separator + fileName; 65 | File file = new File(filePath); 66 | try { 67 | BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); 68 | for (String lineStr : contentList) { 69 | writer.append(lineStr); 70 | writer.newLine(); 71 | } 72 | writer.flush(); 73 | writer.close(); 74 | } catch (IOException e) { 75 | e.printStackTrace(); 76 | } 77 | } 78 | 79 | public static synchronized void saveFile(String writeFilePath, Map keyValue) { 80 | createFile(writeFilePath); 81 | File file = new File(writeFilePath); 82 | try { 83 | BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); 84 | for (String key : keyValue.keySet()) { 85 | writer.append(key + " => " + keyValue.get(key)); 86 | writer.newLine(); 87 | } 88 | writer.flush(); 89 | writer.close(); 90 | } catch (IOException e) { 91 | e.printStackTrace(); 92 | } 93 | } 94 | 95 | public static synchronized void saveFile(String writeFilePath, String keyValue) { 96 | createFile(writeFilePath); 97 | File file = new File(writeFilePath); 98 | try { 99 | BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); 100 | writer.append(keyValue); 101 | writer.newLine(); 102 | writer.flush(); 103 | writer.close(); 104 | } catch (IOException e) { 105 | e.printStackTrace(); 106 | } 107 | } 108 | 109 | /** 110 | * 多行文件写入. 111 | * 112 | * @param fileIndex 113 | * @param fileName 114 | * @param contentList 115 | */ 116 | public static synchronized void saveFile(String fileIndex, String fileName, Map contentList) { 117 | createFile(fileIndex, fileName); 118 | String filePath = fileIndex + File.separator + fileName; 119 | File file = new File(filePath); 120 | try { 121 | BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); 122 | for (String lineStr : contentList.keySet()) { 123 | writer.append(lineStr); 124 | writer.newLine(); 125 | } 126 | writer.flush(); 127 | writer.close(); 128 | } catch (IOException e) { 129 | e.printStackTrace(); 130 | } 131 | } 132 | 133 | /** 134 | * 多行文件写入. 135 | * 136 | * @param fileIndex 137 | * @param fileName 138 | * @param contentList 139 | */ 140 | static String fileName = "data"; 141 | static String curentFlie = "data"; 142 | static int fileNum = 1; 143 | 144 | public static synchronized void saveFile2(String fileIndex, List contentList) { 145 | createFile(fileIndex, curentFlie); 146 | String filePath = fileIndex + File.separator + curentFlie; 147 | File file = new File(filePath); 148 | if (file.length() > 314572800L) { 149 | curentFlie = fileName + "_" + (fileNum + ""); 150 | createFile(fileIndex, curentFlie); 151 | file = new File(fileIndex + File.separator + curentFlie); 152 | fileNum++; 153 | } 154 | try { 155 | BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); 156 | for (String lineStr : contentList) { 157 | writer.append(lineStr); 158 | writer.newLine(); 159 | } 160 | writer.flush(); 161 | writer.close(); 162 | } catch (IOException e) { 163 | e.printStackTrace(); 164 | } 165 | } 166 | 167 | public static synchronized void saveFile(String file, List contentList) { 168 | LOGGER.info("写入文件:" + file + " 行数:" + contentList.size()); 169 | createFile(file); 170 | File files = new File(file); 171 | try { 172 | BufferedWriter writer = new BufferedWriter(new FileWriter(files, true)); 173 | for (String lineStr : contentList) { 174 | writer.append(lineStr); 175 | writer.newLine(); 176 | } 177 | writer.flush(); 178 | writer.close(); 179 | } catch (IOException e) { 180 | e.printStackTrace(); 181 | } 182 | } 183 | 184 | public static synchronized void saveFile(String file, Set contentList) { 185 | createFile(file); 186 | File files = new File(file); 187 | try { 188 | BufferedWriter writer = new BufferedWriter(new FileWriter(files, true)); 189 | for (String lineStr : contentList) { 190 | writer.append(lineStr); 191 | writer.newLine(); 192 | } 193 | writer.flush(); 194 | writer.close(); 195 | } catch (IOException e) { 196 | e.printStackTrace(); 197 | } 198 | } 199 | 200 | /** 201 | * 创建文件. 202 | * 203 | * @param fileIndex 204 | * @param fileName 205 | */ 206 | public static void createFile(String fileIndex, String fileName) { 207 | try { 208 | File fileDir = new File(fileIndex); 209 | if (!fileDir.exists()) { 210 | fileDir.mkdirs(); 211 | } 212 | String filePath = fileIndex + File.separator + fileName; 213 | File file = new File(filePath); 214 | if (!file.exists()) { 215 | try { 216 | file.createNewFile(); 217 | } catch (IOException e) { 218 | e.printStackTrace(); 219 | } 220 | } 221 | } catch (Exception e) { 222 | e.printStackTrace(); 223 | } 224 | 225 | } 226 | 227 | public static void createFile(String file) { 228 | int pathEnd = file.lastIndexOf(File.separator); 229 | String path = file.substring(0, pathEnd + 1); 230 | String fileName = file.substring(pathEnd + 1); 231 | createFile(path, fileName); 232 | } 233 | 234 | /** 235 | * 安行阅读文件内容. 236 | * 237 | * @param filePath 238 | * @param encoding 239 | * @return 240 | */ 241 | public static List lineContent(String filePath, String encoding) { 242 | List sb = new ArrayList<>(); 243 | try { 244 | File file = new File(filePath); 245 | if (file.isFile() && file.exists()) { 246 | InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); 247 | BufferedReader bufferedReader = new BufferedReader(read); 248 | String lineTxt = bufferedReader.readLine(); 249 | while (lineTxt != null) { 250 | sb.add(lineTxt); 251 | lineTxt = bufferedReader.readLine(); 252 | } 253 | read.close(); 254 | } 255 | } catch (Exception e) { 256 | e.printStackTrace(); 257 | } 258 | return sb; 259 | } 260 | 261 | /** 262 | * 读取文件全部内容(速度比readToString慢). 263 | * 264 | * @param filePath 265 | * @param encoding 266 | * @return 267 | */ 268 | public static String allContent(String filePath, String encoding) { 269 | StringBuffer sb = new StringBuffer(); 270 | try { 271 | File file = new File(filePath); 272 | if (file.isFile() && file.exists()) { 273 | InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); 274 | BufferedReader bufferedReader = new BufferedReader(read); 275 | String lineTxt = bufferedReader.readLine(); 276 | String lineSeparator = System.getProperty("line.separator"); 277 | while (lineTxt != null) { 278 | sb.append(lineTxt); 279 | sb.append(lineSeparator); 280 | lineTxt = bufferedReader.readLine(); 281 | } 282 | read.close(); 283 | } 284 | } catch (Exception e) { 285 | e.printStackTrace(); 286 | } 287 | return sb.toString(); 288 | } 289 | 290 | /** 291 | * 读取文件全部内容(文件太大会出现内存溢出). 292 | * 293 | * @param filePath 294 | * @param encoding 295 | * @return 296 | */ 297 | public static String readToString(String filePath, String encoding) { 298 | File file = new File(filePath); 299 | Long filelength = file.length(); 300 | byte[] filecontent = new byte[filelength.intValue()]; 301 | try { 302 | FileInputStream in = new FileInputStream(file); 303 | in.read(filecontent); 304 | in.close(); 305 | } catch (FileNotFoundException e) { 306 | e.printStackTrace(); 307 | } catch (IOException e) { 308 | e.printStackTrace(); 309 | } 310 | try { 311 | return new String(filecontent, encoding); 312 | } catch (UnsupportedEncodingException e) { 313 | System.err.println("The OS does not support " + encoding); 314 | e.printStackTrace(); 315 | return null; 316 | } 317 | } 318 | 319 | /** 320 | * 读取需要处理的文件路径. 321 | * 322 | * @param filepath 323 | * @return 324 | * @throws FileNotFoundException 325 | * @throws IOException 326 | */ 327 | public static synchronized List pathtTaverse(String filepath, List fileList) throws IOException { 328 | try { 329 | File file = new File(filepath); 330 | if (!file.isDirectory()) { 331 | String handleFilePath = file.getAbsolutePath(); 332 | fileList.add(handleFilePath); 333 | } else if (file.isDirectory()) { 334 | String[] filelist = file.list(); 335 | for (int i = 0; i < filelist.length; i++) { 336 | File readfile = new File(filepath + File.separator + filelist[i]); 337 | if (!readfile.isDirectory()) { 338 | String handleFilePath = readfile.getAbsolutePath(); 339 | fileList.add(handleFilePath); 340 | } else if (readfile.isDirectory()) { 341 | pathtTaverse(filepath + File.separator + filelist[i], fileList); 342 | } 343 | } 344 | } 345 | } catch (FileNotFoundException e) { 346 | System.out.println(e.getMessage()); 347 | } 348 | return fileList; 349 | } 350 | 351 | 352 | /** 353 | * 安行读取文件内容. 354 | * 355 | * @param filePath 356 | */ 357 | public static void getAllFileLine(String filePath, List fileLineList, String encoding) { 358 | try { 359 | File file = new File(filePath); 360 | if (file.isFile() && file.exists()) { 361 | InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); 362 | BufferedReader bufferedReader = new BufferedReader(read); 363 | String lineTxt = bufferedReader.readLine(); 364 | while (lineTxt != null) { 365 | fileLineList.add(lineTxt); 366 | lineTxt = bufferedReader.readLine(); 367 | } 368 | read.close(); 369 | bufferedReader.close(); 370 | } 371 | } catch (Exception e) { 372 | e.printStackTrace(); 373 | } 374 | } 375 | 376 | 377 | } 378 | -------------------------------------------------------------------------------- /src/main/java/daiyun/utils/TimeFormat.java: -------------------------------------------------------------------------------- 1 | package daiyun.utils; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | 7 | /** 8 | * time format. 9 | * 10 | * @author daiyun 11 | * @date 2016-07-28 11:17. 12 | */ 13 | public class TimeFormat { 14 | 15 | private static final int SECONDS = 1000; 16 | private static final int MINUTES = 60; 17 | private static final int HOUR = 60; 18 | private static final int DAY = 24; 19 | private static final int WEEK = 7; 20 | 21 | private TimeFormat() { 22 | } 23 | 24 | /** 25 | * 当前系统化格式化时间,默认格式 2016-07-28 11:17:23. 26 | * 27 | * @return 格式化后时间字符串 28 | */ 29 | public static String currentTimeFormat() { 30 | Date currentTime = new Date(); 31 | SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 32 | return formatter.format(currentTime); 33 | } 34 | 35 | /** 36 | * 根据传入格式格式化当前时间. 37 | * 38 | * @param format 格式化规则 39 | * @return 对应格式化后时间字符串 40 | */ 41 | public static String currentTimeFormat(String format) { 42 | Date currentTime = new Date(); 43 | SimpleDateFormat formatter = new SimpleDateFormat(format); 44 | return formatter.format(currentTime); 45 | } 46 | 47 | /** 48 | * 根据传入格式格式化第二天当前时间. 49 | * 50 | * @param format 格式化规则 51 | * @return 对应格式化后时间字符串 52 | */ 53 | public static String tomorrowTimeFormat(String format) { 54 | Date currentTime = new Date(); 55 | currentTime.setTime(currentTime.getTime() + DAY * HOUR * MINUTES * SECONDS); 56 | SimpleDateFormat formatter = new SimpleDateFormat(format); 57 | return formatter.format(currentTime); 58 | } 59 | 60 | /** 61 | * 格式化几天后当前时间格式. 62 | * 63 | * @param format 格式化规则 64 | * @param day 几天后 65 | * @return 对应格式化后时间字符串 66 | */ 67 | public static String dayAfterFormat(String format, int day) { 68 | Date currentTime = new Date(); 69 | currentTime.setTime(currentTime.getTime() + 1L * DAY * HOUR * MINUTES * SECONDS * day); 70 | SimpleDateFormat formatter = new SimpleDateFormat(format); 71 | return formatter.format(currentTime); 72 | } 73 | 74 | /** 75 | * 格式化传入的时间. 76 | * 77 | * @param format 78 | * @param date 79 | * @return 80 | */ 81 | public static String dateFormat(String format, Date date) { 82 | SimpleDateFormat formatter = new SimpleDateFormat(format); 83 | return formatter.format(date); 84 | } 85 | 86 | 87 | public static String formatChange(String dateOld, String oldFormat, String newFormat) { 88 | String dateStr = ""; 89 | try { 90 | Date date = formatData(dateOld, oldFormat); 91 | dateStr = dateFormat(newFormat, date); 92 | } catch (ParseException e) { 93 | e.printStackTrace(); 94 | } 95 | 96 | return dateStr; 97 | } 98 | 99 | public static String getTime(String user_time) { 100 | String dateStr = ""; 101 | try { 102 | Date date = formatData(user_time, "yyyy年MM月dd日"); 103 | dateStr = dateFormat("yyyy-MM-dd", date); 104 | } catch (ParseException e) { 105 | e.printStackTrace(); 106 | } 107 | return dateStr; 108 | } 109 | 110 | /** 111 | * 将默认格式时间字符串转为data. 112 | * 113 | * @param dataStr 默认格式时间字符串 114 | * @return 字符串对应data 115 | */ 116 | public static Date formatData(String dataStr) { 117 | Date date = null; 118 | try { 119 | date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dataStr); 120 | } catch (ParseException e) { 121 | e.printStackTrace(); 122 | } 123 | return date; 124 | } 125 | 126 | /** 127 | * 根据指定格式将字符串转为data. 128 | * 129 | * @param dataStr 时间字符串 130 | * @param formatStr 时间字符串格式 131 | * @return 字符串对应data 132 | * @throws ParseException 133 | */ 134 | public static Date formatData(String dataStr, String formatStr) throws ParseException { 135 | Date date = null; 136 | try { 137 | date = new SimpleDateFormat(formatStr).parse(dataStr); 138 | } catch (ParseException e) { 139 | e.printStackTrace(); 140 | } 141 | return date; 142 | } 143 | 144 | /** 145 | * 判断是否为当前时间节点前一周时间. 146 | * 147 | * @param time 判断的时间字符串 yyyy-MM-dd 148 | * @return 判断结果 149 | */ 150 | public static boolean isLatestWeek(String time) { 151 | boolean judge = false; 152 | //评论时间 153 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 154 | try { 155 | Date date = sdf.parse(time); 156 | long million1 = date.getTime(); 157 | //当前网络时间 158 | long million2 = System.currentTimeMillis(); 159 | if ((million2 - million1) <= DAY * HOUR * MINUTES * SECONDS * WEEK) { 160 | judge = true; 161 | } else { 162 | 163 | judge = false; 164 | } 165 | } catch (ParseException e) { 166 | e.printStackTrace(); 167 | SimpleDateFormat sdf01 = new SimpleDateFormat("yyyyMMdd"); 168 | try { 169 | Date date = sdf01.parse(time); 170 | long million1 = date.getTime(); 171 | //当前网络时间 172 | long million2 = System.currentTimeMillis(); 173 | if ((million2 - million1) <= DAY * HOUR * MINUTES * SECONDS * WEEK) { 174 | judge = true; 175 | } else { 176 | judge = false; 177 | } 178 | } catch (ParseException e1) { 179 | e1.printStackTrace(); 180 | judge = false; 181 | } 182 | } 183 | return judge; 184 | } 185 | 186 | public static boolean isBeforDate(String time) { 187 | boolean judge = false; 188 | //评论时间 189 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 190 | try { 191 | Date date = sdf.parse(time); 192 | long million1 = date.getTime(); 193 | long million2 = System.currentTimeMillis(); 194 | if (million1 <= million2) { 195 | judge = true; 196 | } 197 | } catch (ParseException e) { 198 | e.printStackTrace(); 199 | 200 | } 201 | return judge; 202 | } 203 | 204 | /** 205 | * 毫秒级时间戳格式化. 206 | * 207 | * @param timestampString 208 | * @param formats 209 | * @return 210 | */ 211 | public static String TimeStamp2Date(String timestampString, String formats) { 212 | long timestamp = Long.parseLong(timestampString); 213 | String date = new SimpleDateFormat(formats).format(new Date(timestamp)); 214 | return date; 215 | } 216 | 217 | /** 218 | * 将时期字符串格式化为时间戳. 219 | * 220 | * @param time 221 | * @param formats 222 | * @return 223 | */ 224 | public static long dateToTimeStamp(String time, String formats) { 225 | SimpleDateFormat format = new SimpleDateFormat(formats); 226 | Date date = null; 227 | try { 228 | date = format.parse(time); 229 | } catch (ParseException e) { 230 | e.printStackTrace(); 231 | } 232 | return date.getTime(); 233 | } 234 | 235 | public static void main(String[] args) { 236 | } 237 | 238 | 239 | } 240 | --------------------------------------------------------------------------------