肯定还有很多其它的方式进行转换,我这里只列出了开放过程中碰到的。
转String
- byte转String
String str = new String(byte[],"UTF-8")
- List转String
StringUtils.join(applyNameList, "")
- file转String
-- jdk1.8
Files.lines(Paths.get("C:\\Users\\Administrator\\Desktop\\Cda_Template.view.xml"), StandardCharsets.UTF_8)
.collect(Collectors.joining());
转数组
- List转数组
阿里巴巴规范推荐使用带参数的toArray
List<String> list = new ArrayList<>();
String[] str = new String[list.size()];
str = list.toArray(str);
-
inputStream转byte[]
InputStream inputStream =
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
IOUtils.copy(inputStream,bytestream);
byte imgdata[]=bytestream.toByteArray();
转list
- 数组转list
最常用的方式,转之后的列表只能查询,add/remove会报错
String[] arrs = new String[10];
List<String> list1 = Arrays.asList(arrs );
用Collections转之后的列表可以正常操作
String[] arrs = new String[10];
List<String> newList = new ArrayList<>();
Collections.addAll(newList, arrs);
转对象
- json转对象
// fastJson这个包
// json转有内部类的对象
JSON.parseObject(str, new TypeReference<bean<bean>>(){});
- map转对象
// commons-beanutils这个包
BeanUtils.populate(bean, map);
四舍五入
-
double d = 10.27; String result = String.format("%.1f", d); System.out.println("result:" + result);
DecimalFormat df = new DecimalFormat("#.0"); String format = df.format(new BigDecimal(10.29)); System.out.println(); #直接截取需要加 df.setRoundingMode(RoundingMode.DOWN);
转inputStream
- blob转inputStream
blob.getBinaryStream()
- file转inputStream
#jdk1.8
InputStream inputStream = Files.newInputStream(Paths.get(""));
转file
- inputStream转输出到文件
try {
InputStream input = retreatingService.getExcelByMap();
int index;
byte[] bytes = new byte[1024];
FileOutputStream downloadFile = new FileOutputStream("d://aa1.xlsx");
while ((index = input.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
downloadFile.close();
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}