开发者社区> 问答> 正文

20非常有用的Java程序片段 热:报错

下面是20个非常有用的Java程序片段,希望能对你有用。

1. 字符串有整型的相互转换

1  
2String a = String.valueOf(2);   //integer to numeric string  
3int i = Integer.parseInt(a); //numeric string to an int 


2. 向文件末尾添加内容

01  
02BufferedWriter out = null;  
03try {  
04    out = new BufferedWriter(new FileWriter(”filename”, true));  
05    out.write(”aString”);  
06} catch (IOException e) {  
07    // error processing code  
08} finally {  
09    if (out != null) {  
10        out.close();  
11    }  
12

3. 得到当前方法的名字

1String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); 

4. 转字符串到日期

1  
2java.util.Date = java.text.DateFormat.getDateInstance().parse(date String); 

或者是:

1  
2SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" );  
3Date date = format.parse( myString ); 

5. 使用JDBC链接Oracle

01public class OracleJdbcTest  
02{  
03    String driverClass = "oracle.jdbc.driver.OracleDriver";  
04  
05    Connection con;  
06  
07    public void init(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException  
08    {  
09        Properties props = new Properties();  
10        props.load(fs);  
11        String url = props.getProperty("db.url");  
12        String userName = props.getProperty("db.user");  
13        String password = props.getProperty("db.password");  
14        Class.forName(driverClass);  
15  
16        con=DriverManager.getConnection(url, userName, password);  
17    }  
18  
19    public void fetch() throws SQLException, IOException  
20    {  
21        PreparedStatement ps = con.prepareStatement("select SYSDATE from dual");  
22        ResultSet rs = ps.executeQuery();  
23  
24        while (rs.next())  
25        {  
26            // do the thing you do  
27        }  
28        rs.close();  
29        ps.close();  
30    }  
31  
32    public static void main(String[] args)  
33    {  
34        OracleJdbcTest test = new OracleJdbcTest();  
35        test.init();  
36        test.fetch();  
37    }  
38

6. 把 Java util.Date 转成 sql.Date

1java.util.Date utilDate = new java.util.Date();  
2java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); 

7. 使用NIO进行快速的文件拷贝

01public static void fileCopy( File in, File out )  
02            throws IOException  
03    {  
04        FileChannel inChannel = new FileInputStream( in ).getChannel();  
05        FileChannel outChannel = new FileOutputStream( out ).getChannel();  
06        try 
07        {  
08//          inChannel.transferTo(0, inChannel.size(), outChannel);      // original -- apparently has trouble copying large files on Windows  
09  
10            // magic number for Windows, 64Mb - 32Kb)  
11            int maxCount = (64 * 1024 * 1024) - (32 * 1024);  
12            long size = inChannel.size();  
13            long position = 0;  
14            while ( position < size )  
15            {  
16               position += inChannel.transferTo( position, maxCount, outChannel );  
17            }  
18        }  
19        finally 
20        {  
21            if ( inChannel != null )  
22            {  
23               inChannel.close();  
24            }  
25            if ( outChannel != null )  
26            {  
27                outChannel.close();  
2

展开
收起
kun坤 2020-06-06 21:49:11 521 0
1 条回答
写回答
取消 提交回答
  • 好东东,很实用

    ######

    還行,可以參考學習使用

    ######

    很实用的东东,希望楼主以后能分享更多实用的代码,让我们学习,顶一个!

    ######

    我崇拜你,好东西

    ######

    代码全是乱的!。。。

    ######

    不错不错,,不过代码有点乱

    ######

    代码怎么不换行啊?红薯看看噻

    ######

    港滴波, 看的眼花缭乱, 只能看黑体知道是做什么用的

    顺便问下, NIO 真的比IO的效率高吗?

    ######

    引用来自#9楼“Shny”的帖子

    港滴波, 看的眼花缭乱, 只能看黑体知道是做什么用的

    顺便问下, NIO 真的比IO的效率高吗?

    非也,简单的说,只有多并发连接多的时候才能体现出 NIO 的优势。

    ######

    引用来自#10楼“红薯”的帖子

    引用来自#9楼“Shny”的帖子

    港滴波, 看的眼花缭乱, 只能看黑体知道是做什么用的

    顺便问下, NIO 真的比IO的效率高吗?

    非也,简单的说,只有多并发连接多的时候才能体现出 NIO 的优势。

     靠!有啥你不懂的啊?

    2020-06-08 11:19:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载