org.apache.commons.lang.StringUtils 常用方法
| <dependency> |
| <groupId>org.apache.commons</groupId> |
| <artifactId>commons-lang3</artifactId> |
| <version>3.4</version> |
| </dependency> |
示例:
| import org.apache.commons.lang.StringUtils; |
| |
| public class Test{ |
| @SuppressWarnings({ "deprecation", "unused" }) |
| public static void test(String[] args) { |
| |
| |
| |
| boolean isEmpty = StringUtils.isEmpty(""); |
| boolean isEmpty1 = StringUtils.isEmpty(" "); |
| boolean isEmptyNull = StringUtils.isEmpty(null); |
| |
| boolean isBlack = StringUtils.isBlank(""); |
| boolean isBlack1 = StringUtils.isBlank(" "); |
| boolean isBlankNull = StringUtils.isBlank(null); |
| |
| |
| |
| String difference = StringUtils.difference("s123", "s13"); |
| System.out.println(difference); |
| |
| |
| boolean equals = StringUtils.equals("s1", "s1"); |
| System.out.println(equals); |
| |
| |
| boolean equalsIgnoreCase = StringUtils.equalsIgnoreCase("abc", "ABc"); |
| System.out.println(equalsIgnoreCase); |
| |
| |
| boolean b2 = StringUtils.contains("asd", "as"); |
| System.out.println(b2); |
| |
| |
| String concatStr = StringUtils.join(new String[]{"dog", "cat", "monkey"},":"); |
| System.out.println(concatStr); |
| |
| |
| String[] split = StringUtils.split("apple|xiaomi|dell|lenovo", "|"); |
| for (String s1 : split) { |
| System.out.print(s1 + "、"); |
| } |
| System.out.println(); |
| |
| |
| String capitaliseAllWords = StringUtils.capitaliseAllWords("today i will go to china"); |
| System.out.println(capitaliseAllWords); |
| |
| |
| int matchCount = StringUtils.countMatches("Happy Birthday to you", "o"); |
| System.out.println(matchCount); |
| |
| |
| String leftPad = StringUtils.leftPad("54", 8, "0"); |
| System.out.println(leftPad); |
| |
| |
| String rightPad = StringUtils.rightPad("54", 8, "0"); |
| System.out.println(rightPad); |
| |
| |
| boolean startsWith = StringUtils.startsWith("GoodMorning", "go"); |
| System.out.println(startsWith); |
| |
| |
| boolean endsWith = StringUtils.endsWith("GoodMorning", "ing"); |
| System.out.println(endsWith); |
| |
| |
| StringUtils.trim(" 222 "); |
| |
| |
| StringUtils.trimToNull(""); |
| |
| |
| StringUtils.trimToEmpty(null); |
| |
| |
| StringUtils.defaultIfEmpty(null, "sos"); |
| StringUtils.defaultIfEmpty("", "sos"); |
| StringUtils.defaultIfEmpty("111", "sos"); |
| |
| |
| StringUtils.strip("fsfsdf", "f"); |
| StringUtils.strip("fsfsdfa", "f"); |
| |
| StringUtils.stripStart("ddsuuud", "d"); |
| |
| StringUtils.stripEnd("ddsuuud", "d"); |
| |
| |
| |
| |
| |
| String[] strip = StringUtils.stripAll(new String[]{" java ", "c++ ", "python script"}); |
| for(String aa : strip){ |
| System.out.println(aa); |
| } |
| |
| |
| |
| |
| |
| String[] strip1 = StringUtils.stripAll(new String[]{" java ", "php ", "python script"},"p"); |
| for(String aa : strip){ |
| System.out.println(aa); |
| } |
| |
| |
| |
| StringUtils.indexOf("bbbb", "a"); |
| StringUtils.indexOf(null, "a"); |
| StringUtils.indexOf("aaaa", "a"); |
| StringUtils.indexOf("aaaa", "A"); |
| |
| |
| StringUtils.indexOf("bbbb", "A"); |
| StringUtils.indexOf(null, "A"); |
| StringUtils.indexOf("aaaa", "A"); |
| |
| |
| } |
| |
| public static void main(String[] args) { |
| System.out.println(StringUtils.indexOfIgnoreCase("abbb", "A")); |
| } |
| |
| } |
@Data(Lombok实现)
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| <version>1.18.22</version> |
| <optional>true</optional> |
| </dependency> |
示例
| package com.example.lombok; |
| |
| import lombok.Data; |
| |
| @Data |
| public class Student { |
| private Integer id; |
| private String name; |
| private Integer gender; |
| private Integer classId; |
| } |
| |
可选配置 如果想要在 Maven 打包的时候,Lombok 不被打包,可使用如下配置。
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-maven-plugin</artifactId> |
| <configuration> |
| <excludes> |
| <exclude> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| </exclude> |
| </excludes> |
| </configuration> |
| </plugin> |
| </plugins> |
| </build> |
java利用POI解析Excel及图片
| <dependency> |
| <groupId>org.apache.poi</groupId> |
| <artifactId>poi</artifactId> |
| <version>3.17</version> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.poi</groupId> |
| <artifactId>poi-ooxml</artifactId> |
| <version>3.17</version> |
| </dependency> |
| package com.betawoo.admin.test.base; |
| import com.betawoo.admin.commons.utils.QiNiuUtils; |
| import org.apache.poi.POIXMLDocumentPart; |
| import org.apache.poi.hssf.usermodel.*; |
| import org.apache.poi.ss.usermodel.*; |
| import org.apache.poi.xssf.usermodel.*; |
| import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| |
| |
| public class POIExcel { |
| public static void getDataFromExcel(String filePath) throws IOException |
| { |
| |
| if(!filePath.endsWith(".xls")&&!filePath.endsWith(".xlsx")) |
| { |
| System.out.println("文件不是excel类型"); |
| } |
| FileInputStream fis =null; |
| Workbook wookbook = null; |
| Sheet sheet =null; |
| try |
| { |
| |
| fis = new FileInputStream(filePath); |
| |
| |
| |
| } |
| catch(Exception e) |
| { |
| e.printStackTrace(); |
| } |
| try |
| { |
| |
| wookbook = new HSSFWorkbook(fis); |
| } |
| catch (Exception ex) |
| { |
| |
| try |
| { |
| |
| fis = new FileInputStream(filePath); |
| wookbook = new XSSFWorkbook(fis); |
| } catch (IOException e) |
| { |
| |
| e.printStackTrace(); |
| } |
| } |
| Map<String, PictureData> maplist=null; |
| sheet = wookbook.getSheetAt(0); |
| |
| if (filePath.endsWith(".xls")) { |
| maplist = getPictures1((HSSFSheet) sheet); |
| } else if(filePath.endsWith(".xlsx")){ |
| maplist = getPictures2((XSSFSheet) sheet); |
| } |
| try { |
| printImg(maplist); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| }finally { |
| |
| if (maplist != null){ |
| maplist = null; |
| } |
| } |
| |
| |
| Row rowHead = sheet.getRow(0); |
| |
| |
| int totalRowNum = sheet.getLastRowNum(); |
| |
| String proName=""; |
| String space=""; |
| String size=""; |
| String brand=""; |
| String unit=""; |
| Integer num=null; |
| Double unitPrice=null; |
| Double total=null; |
| String material=""; |
| String remark=""; |
| String pic=""; |
| |
| System.out.println("产品名称\t\t空间\t\t规格/尺寸\t\t品牌\t\t单位\t\t数量\t\t单价\t\t金额\t\t材质\t\t备注"); |
| for(int i = 1 ; i < totalRowNum ; i++) |
| { |
| |
| Row row = sheet.getRow(i); |
| |
| |
| Cell cell = row.getCell(0); |
| if (cell == null){ |
| break; |
| } |
| cell.setCellType(CellType.STRING); |
| space =cell.getStringCellValue().toString(); |
| if (StringUtils.isBlank(space)){ |
| break; |
| } |
| |
| |
| cell = row.getCell(1); |
| if (cell != null){ |
| cell.setCellType(CellType.STRING); |
| proName = cell.getStringCellValue(); |
| } |
| |
| cell = row.getCell(3); |
| if (cell != null){ |
| cell.setCellType(CellType.STRING); |
| size =cell.getStringCellValue()+""; |
| } |
| |
| cell = row.getCell(4); |
| if (cell != null){ |
| cell.setCellType(CellType.STRING); |
| brand =cell.getStringCellValue()+""; |
| } |
| |
| cell = row.getCell(5); |
| if (cell != null){ |
| cell.setCellType(CellType.STRING); |
| unit =cell.getStringCellValue()+""; |
| } |
| |
| cell = row.getCell(6); |
| if (cell != null){ |
| num =(int)cell.getNumericCellValue(); |
| } |
| |
| cell = row.getCell(7); |
| if (cell != null){ |
| unitPrice =cell.getNumericCellValue(); |
| } |
| |
| cell = row.getCell(8); |
| if (cell != null){ |
| total =cell.getNumericCellValue(); |
| } |
| |
| cell = row.getCell(9); |
| if (cell != null){ |
| cell.setCellType(CellType.STRING); |
| material =cell.getStringCellValue()+""; |
| } |
| |
| cell = row.getCell(10); |
| if (cell != null){ |
| cell.setCellType(CellType.STRING); |
| remark =cell.getStringCellValue()+""; |
| } |
| System.out.println(proName+"\t\t"+space+"\t\t"+size+"\t\t"+brand+"\t\t"+unit+"\t\t"+num+"\t\t" |
| +unitPrice+"\t\t"+total+"\t\t"+material+"\t\t"+remark); |
| } |
| for (Map.Entry<String, PictureData> entry : maplist.entrySet()) { |
| System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); |
| } |
| |
| wookbook.close(); |
| if (fis != null){ |
| fis.close(); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| public static Map<String, PictureData> getPictures1 (HSSFSheet sheet) throws IOException { |
| Map<String, PictureData> map = new HashMap<String, PictureData>(); |
| List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren(); |
| for (HSSFShape shape : list) { |
| if (shape instanceof HSSFPicture) { |
| HSSFPicture picture = (HSSFPicture) shape; |
| HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor(); |
| PictureData pdata = picture.getPictureData(); |
| String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); |
| map.put(key, pdata); |
| } |
| } |
| return map; |
| } |
| |
| |
| |
| |
| |
| |
| public static Map<String, PictureData> getPictures2 (XSSFSheet sheet) throws IOException { |
| Map<String, PictureData> map = new HashMap<String, PictureData>(); |
| List<POIXMLDocumentPart> list = sheet.getRelations(); |
| for (POIXMLDocumentPart part : list) { |
| if (part instanceof XSSFDrawing) { |
| XSSFDrawing drawing = (XSSFDrawing) part; |
| List<XSSFShape> shapes = drawing.getShapes(); |
| for (XSSFShape shape : shapes) { |
| XSSFPicture picture = (XSSFPicture) shape; |
| XSSFClientAnchor anchor = picture.getPreferredSize(); |
| CTMarker marker = anchor.getFrom(); |
| String key = marker.getRow() + "-" + marker.getCol(); |
| map.put(key, picture.getPictureData()); |
| } |
| } |
| } |
| return map; |
| } |
| |
| public static void printImg(Map<String, PictureData> sheetList) throws Exception { |
| Object key[] = sheetList.keySet().toArray(); |
| String filePath = ""; |
| for (int i = 0; i < sheetList.size(); i++) { |
| |
| PictureData pic = sheetList.get(key[i]); |
| |
| String picName = key[i].toString(); |
| |
| String ext = pic.suggestFileExtension(); |
| byte[] data = pic.getData(); |
| |
| |
| |
| filePath = "D:\\img\\pic" + picName + "." + ext; |
| System.out.println(filePath); |
| FileOutputStream out = new FileOutputStream(filePath); |
| out.write(data); |
| out.close(); |
| } |
| } |
| public static void main(String[] args) throws Exception { |
| getDataFromExcel("D:"+ File.separator +"test.xlsx"); |
| } |
| } |
java csv文件读取包
| <dependency> |
| <groupId>org.apache.commons</groupId> |
| <artifactId>commons-csv</artifactId> |
| <version>1.8</version> |
| </dependency> |
示例
| 在Java中,可以使用不同的库来读取CSV文件,其中最常用的是Apache Commons CSV和OpenCSV。 |
| |
| 下面是使用Apache Commons CSV读取CSV文件的示例代码: |
| |
| java |
| import org.apache.commons.csv.*; |
| |
| import java.io.FileReader; |
| import java.io.IOException; |
| import java.util.List; |
| |
| public class CSVReader { |
| public static void main(String[] args) { |
| try { |
| FileReader reader = new FileReader("data.csv"); |
| Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(reader); |
| for (CSVRecord record : records) { |
| String name = record.get("Name"); |
| int age = Integer.parseInt(record.get("Age")); |
| System.out.println("Name: " + name + ", Age: " + age); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| java |
| import com.opencsv.CSVReader; |
| import java.io.FileReader; |
| import java.io.IOException; |
| |
| public class CSVReader { |
| public static void main(String[] args) { |
| try { |
| CSVReader reader = new CSVReader(new FileReader("data.csv")); |
| String[] nextLine; |
| while ((nextLine = reader.readNext()) != null) { |
| String name = nextLine[0]; |
| int age = Integer.parseInt(nextLine[1]); |
| System.out.println("Name: " + name + ", Age: " + age); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } catch (NumberFormatException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2022-12-19 mac m1 phpstudy 升级php版本方法
2022-12-19 Using ${var} in strings is deprecated, use {$var} instead