顯示具有 JAVA 程式設計 標籤的文章。 顯示所有文章
顯示具有 JAVA 程式設計 標籤的文章。 顯示所有文章

2019年10月31日 星期四

JAVA String.split() 的誤區

JAVA String.split() 的誤區

碼農們
String.split 這個方法很好用
但是
人生就是有這個但是

以下字串
"A|B|C|D"
會切出 ["A","B","C","D"]

"A||C|D"
會切出 ["A", ,"C","D"]

"A||C|"
會  java.lang.ArrayIndexOutOfBoundsException: 3


處理方法請參考

String Rstr= "A||C|";
StringBuffer sb = new StringBuffer();
List<String> strArr = new ArrayList<String>();
for (int i=0;i<Rstr.length();i++){
    if(Rstr.charAt(i) == '|') {
        //System.out.print("\n");
        strArr.add(sb.toString());
        //sb = new StringBuffer();
        sb.delete(0, sb.length());
    } else {
        //System.out.print(Rstr.charAt(i));
        sb.append(Rstr.charAt(i));
    }
}
strArr.add(sb.toString());
sb.delete(0, sb.length());

String[] arrayStr = new String[strArr.size()];        
arrayStr = strArr.toArray(arrayStr);
System.out.println(arrayStr[0]);
System.out.println(arrayStr[1]);
System.out.println(arrayStr[2]);
System.out.println(arrayStr[3]);

2017年12月21日 星期四

ODF Toolkit 範例

ODF範例
public  void main2(String[] args) throws Exception   {
 TextDocument textdoc=(TextDocument)TextDocument.loadDocument("D:/HelloWorld.odt");
 TextNavigation search1;
 search1=new TextNavigation("What",textdoc);
 while (search1.hasNext()) {
  TextSelection item1 = (TextSelection) search1.nextSelection();
  System.out.println(item1);
 }      
 TextNavigation search2 = null;
 search2 = new TextNavigation("What", textdoc);
 while (search2.hasNext()) {
 TextSelection item= (TextSelection) search2.nextSelection();
  item.replaceWith("replacedest");
 }      
 textdoc.save("D:/HelloWorld2.odt");
}

ODS範例
public static void main(String[] args) throws Exception   {
 System.out.println("START");
 SpreadsheetDocument ods = SpreadsheetDocument.newSpreadsheetDocument();
 Table table = Table.newTable(ods, 4000, 20, 0, 0);
 table.setTableName("foo");
 //create style
 OdfOfficeAutomaticStyles astyles = ods.getContentDom().getOrCreateAutomaticStyles();
 StyleStyleElement ele = astyles.newStyleStyleElement(OdfStyleFamily.TableCell.getName(), "myss");
 StyleTableCellPropertiesElement styleTableCellPropertiesElement = ele.newStyleTableCellPropertiesElement();
 styleTableCellPropertiesElement.setFoBackgroundColorAttribute("#A5A5A5");
 styleTableCellPropertiesElement.setFoBorderAttribute("1.0pt solid #000000");
 ele.newStyleParagraphPropertiesElement().setFoTextAlignAttribute(HorizontalAlignmentType.CENTER.toString());
 StyleTextPropertiesElement styleTextPropertiesElement = ele.newStyleTextPropertiesElement(null);
 styleTextPropertiesElement.setStyleFontNameAttribute("Arial");
 styleTextPropertiesElement.setFoFontSizeAttribute("7.0pt");
 styleTextPropertiesElement.setFoColorAttribute(Color.BLACK.toString());
 styleTextPropertiesElement.setFoFontWeightAttribute("bold");

 List rows = table.getRowList();
 for (Row r : rows) {
  for (int a = 0; a < 10; a++) {
   Cell cell = r.getCellByIndex(a);
   cell.setStringValue("Foo " + a);
   cell.setCellStyleName("myss");
  }
 }
 
 ods.save("D:/foo2.ods");
 System.out.println("END");
}

讀檔
SpreadsheetDocument workbook = null;
Cell cell = null;
File tmpFile = new File("tmp.ods")
try {
    workbook = SpreadsheetDocument.loadDocument(tmpFile);
    Table sheet = workbook.getSheetByIndex(0);
    int rows = sheet.getRowCount();
    for (int row = 1;row < rows;row++) {
        String rowStr1 = sheet.getCellByPosition(0, row).getStringValue();
    }
    workbook.close();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (workbook != null) {
        workbook.close();
    }
}

2016年10月10日 星期一

Project Lombok

討厭寫那寫該死又臭又長的pojo了嗎
lombok 是你唯一的救贖!

https://projectlombok.org/

Eclipse 安裝教學
http://jnb.ociweb.com/jnb/jnbJan2010.html
-startup
plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.2.500.v20220509-0833
-product
org.springframework.boot.ide.branding.sts4
--launcher.defaultAction
openFile
-vm
plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.3.v20220515-1416/jre/bin
-vmargs
-javaagent:D:\sts-4.15.1.RELEASE\lombok.jar
--add-opens=java.base/java.io=ALL-UNNAMED
--add-opens=java.base/sun.nio.ch=ALL-UNNAMED
--add-opens=java.base/java.net=ALL-UNNAMED
--add-opens=java.base/sun.security.ssl=ALL-UNNAMED
-Dosgi.requiredJavaVersion=11
-Dosgi.dataAreaRequiresExplicitInit=true
-Xms256m
-Xmx2048m
--illegal-access=permit
--add-modules=ALL-SYSTEM

2014年2月5日 星期三

A server 需要 B server 類別
A server 不須預先 載入 B server 的 jar檔
A server 透過 socket 動態 載入 B server  jar檔
使用 B server 類別

2014年1月22日 星期三

取得JVM 的 類型 32 bit or 64 bit

2013年10月16日 星期三

SWT Win32 Extension

SWT Win32 Extension

http://feeling.sourceforge.net/

SWT 火力加強版,很邪惡的,可以直接呼叫 win 底層元件使用

2013年8月19日 星期一

JXL 轉換 儲存格位置 成為 EXCEL 格式

給JXL 轉換 儲存格位置 成為 EXCEL 格式

2012年1月18日 星期三

coupling cohesion 的解釋

http://www.cs.nchu.edu.tw/~fileman/notepad/sa07.htm

【藕合力 ( Coupling ) 】
 
兩模組間的相依 ( Dependence ) 程度稱之為藕合力 ( Coupling ) 或聯接力 ( Connection )。即欲了解其中一個模
組時,必須在了解令一個模組。如此將導致將來測試、除錯及維護等工作的成本。一般而言,藕合力分
為相依度高的緊密藕合與相依度低的鬆散藕合及無任何相依度的無直接藕合。


內聚力 ( Cohesion ) 】
 
內聚力是模組化程度的另一種評估標準,重點在於評估模組內部每一元素所完成功能間的相關程度 ( Ass-
ocation ) ,相關性越強表示模組的功能獨立性越高,所提供的功能也越單純。


所以 ...

  • Looser coupling >>> 降低相依性
  • Higher cohesion>>>提高模組化