需求:string[]转list
List<String> strsToList1= Arrays.asList(strs);
需求:去掉空格
str.trim(); //去掉首尾空格
str.replace(" ",""); //去除所有空格,包括首尾、中间
str.replaceAll(" ", ""); //去掉所有空格,包括首尾、中间
str.replaceAll(" +",""); //去掉所有空格,包括首尾、中间
str.replaceAll("\\s*", ""); //可以替换大部分空白字符, 不限于空格 ;
需求:合并字符串
s.concat(String);
需求:list操作
ist中添加,获取,删除元素;
添加方法是:.add(e);
修改:set
获取方法是:.get(index);
删除方法是:
.remove(index);
按照索引删除;
.remove(Object o);
按照元素内容删除;
需求:containsAll学习
ArrayList<String> als = new ArrayList<String>();
als.add("a");
als.add("b");
ArrayList<String> alss = new ArrayList<String>();
alss.add("a");
alss.add("c");
System.out.println(als.containsAll(alss));
需求:list初始化
List<String> name = Arrays.asList("xxx","yyy","zzz");
需求:以空格分割字符串
String[] splited = str.split("\\s+");//这样写就可以了
需求:转换时间格式
public String tranTime12To24(String ch) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy hh:mm:ss a", Locale.ENGLISH);
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = df.parse(ch);
String ret = df2.format(date);
return ret;
}
需求:java Pattern和Matcher详解
链接 看链接
需求:截取
String str="1419459573@qq.com";
String str1=str.substring(0, str.indexOf("@"));//截取@之前的字符串
需求:java转换大小写
s.toUpperCase()
s.toLowerCase()
需求:bytes转文件工具类
package com.ls.fostation.utils;
import java.io.*;
/**
* @ClassName => ByteToFileUtil
* @Description => TODO
* @Author => yangjianzhi
* @Date => 2019/8/3 15:35
* @Version => 1.0
**/
public class ByteToFileUtil {
/**
* 获得指定文件的byte数组
*/
private byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"\\"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
需求:java获取项目路径
String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
x:/xxxx/xxxx/xxxxxxx/target/xxxxxxxx/WEB-INF/classes/
String webappRoot = classpath.replaceAll("WEB-INF/classes/", "");
x:/xxxx/xxxx/xxxxxxx/target/xxxxxxxx
需求:时间格式
"yyyy-MM-dd hh:mm:ss
//获取当前时间
public static String getNowDate(){
SimpleDateFormat time = new SimpleDateFormat("yyyyMMddhhmmss");
String date=time.format(new Date());
return date;
}
需求:获取uuid
/**
* @Description: 获取uuid
* @Param: []
* @return: java.lang.String
* @about=>yangjianzhi-version1.0-2019/4/15
*/
public static String getUUID(){
return UUID.randomUUID().toString().replaceAll("-", "");
}
需求:将MultipartFile 文件存入磁盘
public static boolean saveFile(String localUrl, MultipartFile file) throws IOException {
File realFile = new File(localUrl);
OutputStream outputstream = null;
InputStream inputstream = null;
try {
inputstream = file.getInputStream();
outputstream = new FileOutputStream(realFile);
byte[] buffer = new byte[1024 * 5];
int byteRead = -1;
while ((byteRead = (inputstream.read(buffer))) != -1) {
outputstream.write(buffer, 0, byteRead);
}
realFile.setExecutable(false);
return true;
} catch (Exception e) {
return false;
} finally {
if (outputstream != null) {
outputstream.flush();
outputstream.close();
}
if (inputstream != null) {
inputstream.close();
}
}
}
需求:遍历map
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
需求:Dictionary类已经过时了。在实际开发中,你可以实现Map接口来获取键/值的存储功能。