Kotlin总结3
来源:互联网 发布:网络技术服务工程师 编辑:程序博客网 时间:2024/06/05 14:12
Date
这个是日常用的比较多的类,在kotlin中用传统的方法,IDEA会提示语法警告,有更好的方法,就是下面的
传统的方法
fun formatDate(date: Date, pattern: String): String = SimpleDateFormat(pattern).format(date)
kotlin推荐方法
fun formatDate(date: Date, dateFormat: DateFormat): String = dateFormat.format(date)
提示如下
To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale. US for ASCII dates.
获得本地格式化请使用getDateInstance(),getDateTimeInstance(),或者getTimeInstance(),或者使用new SimpleDateFormat(String template, Locale locale)
,例如Locale.US
为ASCII日期。
- 但是它只提供了常用的,这些之外的,还是自定义吧
静态常量和静态方法共存
- 工具类
object DateUtils { const val SFStr = "yyyyMMdd" fun formatDate(date: Date, pattern: String): String = SimpleDateFormat(pattern).format(date) }
- 引用
//javaDateUtils.INSTANCE.formatDate(new Date(),DateUtils.DF_YYYYMMDDHHMMSS);//kotlinDateUtils.formatDate(Date(),DateUtils.DF_YYYYMMDDHHMMSS)
java的引用,看起来是个单例,但是我的习惯是像java的静态方法一样调用
- class可以做到,如果把工具类改成class,那么静态常量就没法用了
- 伴生对象也可以做到,但object又不允许有伴生对象
- 所以,加个注解就搞定了@JvmStatic
- 加了注解不影响kotlin调用,只是简化了java调用
//工具类方法@JvmStaticfun formatDate(date: Date, pattern: String): String = SimpleDateFormat(pattern).format(date)//java调用DateUtils.formatDate(new Date(),DateUtils.DF_YYYYMMDDHHMMSS);
Kotlin读写流操作
写文件在java中是这么操作的
public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { InputStream in = new ByteArrayInputStream(bytes); File destFile = new File(filePath); if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } destFile.createNewFile(); OutputStream out = new FileOutputStream(destFile); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } out.close(); in.close(); }
转成kotlin后,是不允许在while中写赋值表达式的,弄好好久,发现应该是这样的
@Throws(Exception::class) fun byteArrayToFile(bytes: ByteArray, filePath: String) { val inStream = ByteArrayInputStream(bytes) val destFile = File(filePath) if (!destFile.parentFile.exists()) { destFile.parentFile.mkdirs() } destFile.createNewFile() val out = FileOutputStream(destFile) val cache = ByteArray(CACHE_SIZE) var nRead = inStream.read(cache) while (nRead != -1) { out.write(cache, 0, nRead) nRead = inStream.read(cache) } inStream.copyTo(out) out.close() inStream.close() }
然后Slient大神发了一个扩展方法InputStream.copyTo
于是就变成这样了
@Throws(Exception::class)fun byteArrayToFile(bytes: ByteArray, filePath: String) { val inStream = ByteArrayInputStream(bytes) val destFile = File(filePath) if (!destFile.parentFile.exists()) destFile.parentFile.mkdirs() destFile.createNewFile() val out = FileOutputStream(destFile) inStream.copyTo(out,MemoryUtils.KB) out.close() inStream.close()}
卧槽,感觉好多语法糖,上次忘了一个什么方法,写了半天,也是Slient大神给了个语法糖
- Kotlin总结3
- Kotlin学习总结文档
- kotlin项目开发总结
- Android Kotlin问题总结
- Kotlin一周总结
- Kotlin总结2
- Kotlin学习总结文档
- Kotlin总结4(完结)
- Kotlin优点总结
- Kotlin入门学习总结
- Kotlin使用简单总结
- Kotlin常用语法总结
- Kotlin运算符重载总结
- kotlin学习小点总结1
- Kotlin 入坑半年总结
- Kotlin学习总结(1)——Kotlin快速入门
- Kotlin学习(3)
- Kotlin基础 3
- RTC
- ToString() 拓展
- CentOs 7.2 防火墙配置--开启端口
- 关于QT中遇到的问题
- 定时删除edas多余日志
- Kotlin总结3
- 实现RecyclerView类似3D效果的消失
- [Error Code]:RequestTimeTooSkewed, [Message]:The difference between the request time and the current
- HDU 1241(DFS)
- OpenGL学习: 光照系列1-光照基础(phong模型)
- Linux bash 之declare
- POJ-2139 Six Degrees of Cowvin Bacon
- 57
- 西游记思维导图大全