熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

讓你知道:java 文件的縮進排版

2022-06-13   來源: JSP教程 

  //以下是我做的java 文件的縮進排版算法有缺點但能用
  //不管怎麼說排java文件時比UltralEdit的排版強
  
  import javaioFile;
  import javaioPrintStream;
  import javautilArrayList;
  import langStr;
  import langout;
  import utilLoad;
  
  class DentJavaCode
  {
  
  public static boolean dent(File file File file)
  {
  String as[] = new String[];
  if(file == null || !fileexists())
  {
  return false;
  } else
  {
  as = py(file as);
  ArrayList arraylist = new ArrayList();
  dent(as arraylist );
  as = (String[])arraylisttoArray(as);
  outb(slen= + aslength);
  py(as file);
  return true;
  }
  }
  
  public static int dent(ArrayList arraylist ArrayList arraylist int i)
  {
  if(arraylist == null && arraylistsize() == )
  {
  Systemoutprintln(dent frm listbut list isnull);
  return i;
  }
  if(arraylist == null)
  {
  Systemoutprintln(dent to listbut list is null);
  return i;
  } else
  {
  String as[] = new String[];
  as = (String[])arraylisttoArray(as);
  return dent(as arraylist i);
  }
  }
  
  public static String[] dent(String as[] String as[] int i)
  {
  ArrayList arraylist = new ArrayList();
  dent(as arraylist i);
  if(as == null)
  as = new String[];
  return (String[])arraylisttoArray(as);
  }
  
  public static int dent(String as[] ArrayList arraylist int i)
  {
  int j = aslength;
  boolean flag = false;
  if(as == null || aslength == )
  {
  Systemoutprintln(dent from s[]but s[] is null);
  return i;
  }
  if(arraylist == null)
  {
  Systemoutprintln(dent to listbut list is null);
  return i;
  }
  for(int l = ; 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 > )
  arraylistadd(space() + as[l]substring( i));
  arraylistadd(*/);
  i += ;
  if(k > i)
  {
  as[l] = as[l]substring(i);
  l;
  }
  continue;
  }
  if(sequals(/*))
  {
  flag = true;
  if(i > )
  {
  arraylistadd(space(i) + as[l]substring( i));
  as[l] = as[l]substring(i);
  }
  if((i = StrindexOf_outQuotes(*/ as[l] i + )) != )
  {
  flag = false;
  i += ;
  arraylistadd(as[l]substring( i));
  if(as[l]length() > i)
  {
  as[l] = as[l]substring(i);
  l;
  }
  continue;
  }
  if(as[l]length() <= )
  continue;
  arraylistadd(/*);
  as[l] = as[l]substring();
  if(as[l]length() > )
  arraylistadd(space() + as[l]);
  continue;
  }
  if(sequals({))
  {
  i++;
  if(as[l]indexOf(//) == i)
  {
  arraylistadd(space(i) + as[l]);
  i += increment;
  continue;
  }
  arraylistadd(space(i) + as[l]substring( i));
  i += increment;
  if(k > i)
  {
  as[l] = as[l]substring(i);
  l;
  }
  continue;
  }
  if(sequals(}))
  {
  arraylistadd(space(i) + as[l]substring( i));
  i = increment;
  i++;
  if(k > i && as[l]charAt(i) == ;)
  {
  arraylistadd(space(i) + };);
  i++;
  if(k > i)
  {
  as[l] = as[l]substring(i);
  l;
  }
  continue;
  }
  if(as[l]indexOf(}//) == )
  {
  Systemoutprintln(s[i]= + as[l] + indent= + i);
  arraylistadd(space(i) + as[l]);
  continue;
  }
  arraylistadd(space(i) + });
  if(k > i)
  {
  as[l] = as[l]substring(i);
  l;
  }
  } else
  {
  arraylistadd(space(i) + as[l]);
  }
  }
  
  return i;
  }
  
  static String space(int i)
  {
  return Strrepeat( i);
  }
  
  public DentJavaCode(File file)
  {
  String as[] = null;
  as = py(file as);
  ArrayList arraylist = new ArrayList();
  dent(as arraylist );
  String s = filegetName();
  int i = ;
  boolean flag = false;
  File file;
  file = file = new File(s + dent);
  i = ;
  do
  {
  if(i >= )
  break;
  file = new File(s + dent + i);
  if(!fileexists())
  break;
  i++;
  } while(true);
  if(fileexists())
  filedelete();
  if(filerenameTo(file))
  {
  String as[] = (String[])arraylisttoArray(as);
  py(as file);
  Systemoutprintln(ok);
  } else
  {
  Systemoutprintln(rename:false);
  }
  }
  
  public static void main(String args[])
  {
  String args[] = new String[];
  File file = new File(args[]);
  args = py(file args);
  ArrayList arraylist = new ArrayList();
  dent(args arraylist );
  File file = new File(args[] + dent);
  int i = ;
  do
  {
  if(i >= )
  break;
  file = new File(args[] + dent + i);
  if(!fileexists())
  break;
  i++;
  } while(true);
  if(fileexists())
  filedelete();
  String args[] = (String[])arraylisttoArray(args);
  if(filerenameTo(file))
  {
  py(args file);
  Systemoutprintln(ok);
  } else
  {
  Systemoutprintln(rename:false);
  }
  }
  
  static int increment = ;
  public static String special[] = {
  { } // /* */
  };
  
  }
  

From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19308.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.