[转帖]几个有用的java程序片段_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2705 | 回复: 0   主题: [转帖]几个有用的java程序片段        下一篇 
只是很无聊
注册用户
等级:中尉
经验:440
发帖:33
精华:0
注册:2013-6-18
状态:离线
发送短消息息给只是很无聊 加好友    发送短消息息给只是很无聊 发消息
发表于: IP:您无权察看 2013-6-18 14:57:40 | [全部帖] [楼主帖] 楼主

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

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

2. 向文件末尾添加内容

  1. BufferedWriter out = null; 
  2. try { 
  3.        out = new BufferedWriter(new FileWriter(”filename”, true)); 
  4.        out.write(”aString”); 
  5. } catch (IOException e) { 
  6.        // error processing code 
  7. } finally { 
  8.  if (out != null) { 
  9.        out.close(); 
  10.  } 

3. 得到当前方法的名字

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

4. 转字符串到日期

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

或者是:

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

5. 使用JDBC链接Oracle

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

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

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

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

  1. public static void fileCopy( File in, File out ) 
  2.  throws IOException 
  3.  { 
  4.        FileChannel inChannel = new FileInputStream( in ).getChannel(); 
  5.        FileChannel outChannel = new FileOutputStream( out ).getChannel(); 
  6.        try 
  7.        { 
  8.             // inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows 
  9.             
  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(); 
  28.              } 
  29.        } 
  30.  } 




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论