各种类型转换

    肯定还有很多其它的方式进行转换,我这里只列出了开放过程中碰到的。

转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

  1. 数组转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);

转对象

  1. json转对象
// fastJson这个包
// json转有内部类的对象
JSON.parseObject(str, new TypeReference<bean<bean>>(){});
  1. 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();
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值