文章詳情頁
java 文件的縮進(jìn)排版
瀏覽:113日期:2024-06-11 18:21:18
內(nèi)容: //以下是我做的java 文件的縮進(jìn)排版,算法有缺點(diǎn).但能用,//不管怎么說,排java文件時(shí)比UltralEdit的排版強(qiáng)。import java.io.File;import java.io.PrintStream;import java.util.ArrayList;import lang9.Str9;import lang9.out;import util9.Load9;class DentJavaCode{ public static boolean dent(File file, File file1) { String as[] = new String[1]; if(file == null || !file.exists()) { return false; } else { as = Load9.copy(file, as); ArrayList arraylist = new ArrayList(); dent(as, arraylist, 0); as = (String[])arraylist.toArray(as); out.b('s.len=' + as.length); Load9.copy(as, file1); return true; } } public static int dent(ArrayList arraylist, ArrayList arraylist1, int i) { if(arraylist == null && arraylist.size() == 0) { System.out.println('dent frm list1,but list1 isnull'); return i; } if(arraylist1 == null) { System.out.println('dent to list2,but list2 is null'); return i; } else { String as[] = new String[1]; as = (String[])arraylist.toArray(as); return dent(as, arraylist1, i); } } public static String[] dent(String as[], String as1[], int i) { ArrayList arraylist = new ArrayList(); dent(as, arraylist, i); if(as1 == null) as1 = new String[1]; return (String[])arraylist.toArray(as1); } public static int dent(String as[], ArrayList arraylist, int i) { int j = as.length; boolean flag = false; if(as == null || as.length == 0) { System.out.println('dent from s[],but s[] is null'); return i; } if(arraylist == null) { System.out.println('dent to list,but list is null'); return i; } for(int l = 0; l < as.length; l++) { if(as[l] == null) continue; as[l] = as[l].trim(); int k = as[l].length(); if(k == 0) continue; int i1 = Str9.indexOf_outQuotes(special, as[l]); if(i1 < 0) { if(flag) arraylist.add(space(2) + as[l]); else arraylist.add(space(i) + as[l]); continue; } String s = as[l].substring(i1); if(s.indexOf('{') == 0) s = '{'; else if(s.indexOf('}') == 0) s = '}'; else if(s.indexOf('//') == 0) s = '//'; else if(s.indexOf('*/') == 0) s = '*/'; else if(s.indexOf('/*') == 0) s = '/*'; if(s.equals('//')) { if(i1> 0) { arraylist.add(space(i) + as[l].substring(0, i1)); arraylist.add(space(i) + as[l].substring(i1)); } else { arraylist.add(space(i) + as[l]); } continue; } if(s.equals('*/')) { flag = false; if(i1> 0) arraylist.add(space(2) + as[l].substring(0, i1)); arraylist.add('*/'); i1 += 2; if(k> i1) { as[l] = as[l].substring(i1); l--; } continue; } if(s.equals('/*')) { flag = true; if(i1> 0) { arraylist.add(space(i) + as[l].substring(0, i1)); as[l] = as[l].substring(i1); } if((i1 = Str9.indexOf_outQuotes('*/', as[l], i1 + 2)) != -1) { flag = false; i1 += 2; arraylist.add(as[l].substring(0, i1)); if(as[l].length()> i1) { as[l] = as[l].substring(i1); l--; } continue; } if(as[l].length() 0) arraylist.add(space(2) + as[l]); continue; } if(s.equals('{')) { i1++; if(as[l].indexOf('//') == i1) { arraylist.add(space(i) + as[l]); i += increment; continue; } arraylist.add(space(i) + as[l].substring(0, i1)); i += increment; if(k> i1) { as[l] = as[l].substring(i1); l--; } continue; } if(s.equals('}')) { arraylist.add(space(i) + as[l].substring(0, i1)); i -= increment; i1++; if(k> i1 && as[l].charAt(i1) == ';') { arraylist.add(space(i) + '};'); i1++; if(k> i1) { as[l] = as[l].substring(i1); l--; } continue; } if(as[l].indexOf('}//') == 0) { System.out.println('2s[i]=' + as[l] + ',indent=' + i); arraylist.add(space(i) + as[l]); continue; } arraylist.add(space(i) + '}'); if(k> i1) { as[l] = as[l].substring(i1); l--; } } else { arraylist.add(space(i) + as[l]); } } return i; } static String space(int i) { return Str9.repeat(' ', i); } public DentJavaCode(File file) { String as[] = null; as = Load9.copy(file, as); ArrayList arraylist = new ArrayList(); dent(as, arraylist, 0); String s = file.getName(); int i = 0; boolean flag = false; File file1; file1 = file1 = new File(s + 'dent0'); i = 1; do { if(i>= 5) break; file1 = new File(s + 'dent' + i); if(!file1.exists()) break; i++; } while(true); if(file1.exists()) file1.delete(); if(file.renameTo(file1)) { String as1[] = (String[])arraylist.toArray(as); Load9.copy(as1, file); System.out.println('ok'); } else { System.out.println('rename:false'); } } public static void main(String args[]) { String args1[] = new String[1]; File file = new File(args[0]); args1 = Load9.copy(file, args1); ArrayList arraylist = new ArrayList(); dent(args1, arraylist, 0); File file1 = new File(args[0] + 'dent0'); int i = 1; do { if(i>= 5) break; file1 = new File(args[0] + 'dent' + i); if(!file1.exists()) break; i++; } while(true); if(file1.exists()) file1.delete(); String args2[] = (String[])arraylist.toArray(args1); if(file.renameTo(file1)) { Load9.copy(args2, file); System.out.println('ok'); } else { System.out.println('rename:false'); } } static int increment = 3; public static String special[] = { '{', '}', '//', '/*', '*/' };} Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd //以下?
標(biāo)簽:
Java
相關(guān)文章:
1. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容2. JavaScript檢測(cè)是否開啟了控制臺(tái)(F12調(diào)試工具)3. jsp文件下載功能實(shí)現(xiàn)代碼4. Android App啟動(dòng)圖啟動(dòng)界面(Splash)的簡(jiǎn)單實(shí)現(xiàn)代碼5. ajax post下載flask文件流以及中文文件名問題6. python實(shí)現(xiàn)TCP文件接收發(fā)送7. ASP中if語句、select 、while循環(huán)的使用方法8. IntelliJ IDEA恢復(fù)刪除文件的方法9. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享10. ASP常用日期格式化函數(shù) FormatDate()
排行榜
