首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

20 個實用 Java 代碼片段:字符串轉換、文件操作、JSON、PDF 生成等

     編輯:Java 綜合教學

高頻 Java 代碼模板集錦

  1. 字符串與整型互轉
    String a = String.valueOf(2);
    int i = Integer.parseInt(a);
    
  2. 文件追加寫入
    BufferedWriter out = new BufferedWriter(new FileWriter("file", true));
    out.write("content"); out.close();
    
  3. 獲取當前方法名
    String method = Thread.currentThread().getStackTrace()[1].getMethodName();
    
  4. 字符串轉日期
    Date date = new SimpleDateFormat("dd.MM.yyyy").parse(str);
    
  5. JDBC 連接 Oracle(含屬性文件配置)
  6. util.Date 轉 sql.Date
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    
  7. NIO 大文件高效拷貝
    FileChannel in = new FileInputStream(inFile).getChannel();
    FileChannel out = new FileOutputStream(outFile).getChannel();
    in.transferTo(0, in.size(), out);
    
  8. 生成圖片縮略圖(等比縮放、質量控制)
  9. 創建 JSON 數據
    JSONObject json = new JSONObject();
    json.put("city", "Mumbai");
    String output = json.toString();
    
  10. iText 生成 PDF
  11. HTTP 代理設置
    System.getProperties().put("http.proxyHost", "proxy");
    
  12. 單例模式(雙重檢查/枚舉)
  13. 屏幕截圖 (Robot.createScreenCapture)
  14. 列出文件/目錄(支持過濾)
  15. 創建 ZIP/JAR 文件
  16. DOM 解析 XML
  17. 數組轉 Map (ArrayUtils.toMap)
  18. 發送郵件 (JavaMail API)
  19. HTTP GET 請求讀取響應
  20. 動態調整數組大小 (反射 Array.newInstance + System.arraycopy)

學習交流群:279558494

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved