博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Java] Frequently used method or solutions for issues
阅读量:4709 次
发布时间:2019-06-10

本文共 2128 字,大约阅读时间需要 7 分钟。

模板:

 

  • Split string into parts based on new line in java

    Solution:   Reference is . 

        1)

 

  • get out of the first part of a string until meet "<"

    Solution:   Reference is . 

        1)  s = "boo:and:foo";     String[] result = s.split(":") ; => {"boo", "and", "foo"}

        2)  s = "boo:and:foo";     String[] result = s.split(":", 2) ; => {"boo", "and:foo"}

        3)  s = "boo.and.foo";     String[] result = s.split("\\.", 2) ; => {"boo", "and.foo"}

 

 

  • Split string into parts based on new line in java

    Solution:   Reference is . 

        1) 

String lines[] = string.split("\\r?\\n");

 

  • Convert Long in int in java

    Solution:    

        1) 

int result = (int) longVar;

 

  • Copy file to another folder in java

    Solution:    Reference is . copy(Path source, Path target)

        1) 

Path source = Paths.get("/Users/apple/Desktop/test.rtf");Path destination = Paths.get("/Users/apple/Desktop/copied.rtf");Files.copy(source, destination);

 

  • File into a String in java

    Solution:    Reference is . Files.readAllBytes(Path source)

        1) 

String filePath = "c:/temp/data.txt";String content = new String ( Files.readAllBytes( Paths.get(filePath) ) );

 

  • Path String to File in java

    Solution:   

        1)

String filePath = "c:/temp/data.txt";File file = new File (filePath );

 

  • Join path in java

    Solution:   Reference is . 

        1) Use Paths.get(String parentPath, args)

import java.nio.file.Path; import java.nio.file.Paths; Path currentPath = Paths.get(System.getProperty("user.dir"));Path filePath = Paths.get(currentPath.toString(), "data", "foo.txt");

output is "

/Users/user/coding/data/foo.txt

"

        2) Use File(String parentPath, fileName)

import java.io.FileFile file = new File(parentPath, fileName);System.out.println(file.getAbsolutePath());

 

  • Get epoch time in java (present a unique Id by numbers if the time is not in the same ms)

    Solution:   Reference is . 

        1)

long epoch = System.currentTimeMillis();String Id = Long.toString(epoch);

 

  • Create a path from String in java

    Solution:   Reference is . 

        1)

import java.nio.file.Paths;Path path = Paths.get(textPath);

 

转载于:https://www.cnblogs.com/Johnsonxiong/p/9968554.html

你可能感兴趣的文章
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>