常用的Java基本代码汇总

常用的Java基本代码汇总1.字符串和整型的相互转换Stringa=String.valueOf(2);inti=Integer.parseInt(a);2.向文件末尾添加内容BufferedWriterout=null;try{out=newBufferedWriter(newFileWriter(“filename”,true));out.write(“iam

大家好,又见面了,我是你们的朋友全栈君。

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

String a= String.valueOf(2);
int i = Integer.parseInt(a);

2. 向文件末尾添加内容

BufferedWriter out=null;
try {
    out=new BufferedWriter(new FileWriter("filename",true));
    out.write("i am stringbuffer!");
} catch (IOException e) {
    e.printStackTrace();
}

3. 得到当前方法的名字

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

4. 转字符串到日期与日期到字符串

//日期到字符串
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy年MM月dd日 ");
String str = sdf.format(new Date());
System.out.println(str);
//字符串到日期
Date date = sdf.parse(str);
System.out.println(date);

5. 使用JDBC链接Oracle

public class OracleJdbcTest {
    String driverClass = "oracle.jdbc.driver.OracleDriver"; 
    Connection con; 
    public void init(FileInputStream fs)
            throws ClassNotFoundException, SQLException, FileNotFoundException, IOException { 
        Properties props = new Properties(); 
        props.load(fs); 
        String url = props.getProperty("db.url"); 
        String userName = props.getProperty("db.user"); 
        String password = props.getProperty("db.password"); 
        Class.forName(driverClass); 
        con=DriverManager.getConnection(url, userName, password); 
    } 
   
    public void fetch() throws SQLException, IOException{ 
        PreparedStatement ps = con.prepareStatement("select SYSDATE from dual"); 
        ResultSet rs = ps.executeQuery(); 
   
        while (rs.next()){ 
            // do the thing you do 
        } 
        rs.close(); 
        ps.close(); 
    } 
   
    public static void main(String[] args){ 
        OracleJdbcTest test = new OracleJdbcTest(); 
        test.init(); 
        test.fetch(); 
    }
}

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

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

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

public static void fileCopy( File in, File out ) 
        throws IOException  { 
    FileChannel inChannel = new FileInputStream( in ).getChannel(); 
    FileChannel outChannel = new FileOutputStream( out ).getChannel(); 
    try{ 
        // inChannel.transferTo(0, inChannel.size(), outChannel); 
        // original -- apparently has trouble copying large files on Windows 
        // magic number for Windows, 64Mb - 32Kb) 
        int maxCount = (64 * 1024 * 1024) - (32 * 1024); 
        long size = inChannel.size(); 
        long position = 0; 
        while ( position < size ){ 
           position += inChannel.transferTo(position, maxCount, outChannel ); 
        } 
    }finally{ 
        if (inChannel != null){ 
           inChannel.close(); 
        } 
        if (outChannel != null){ 
            outChannel.close(); 
        } 
    } 
}


8. 发送代数据的HTTP 请求

try { 
    URL my_url = new URL("链接地址"); 
    BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream())); 
    String strTemp = ""; 
    while(null != (strTemp = br.readLine())){ 
    System.out.println(strTemp); 
} 
} catch (Exception ex) { 
    ex.printStackTrace(); 
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157035.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Github搭建个人博客(2019最新版,亲测)

    Github搭建个人博客(2019最新版,亲测)版权声明:本文为徐代龙原创文章,未经徐代龙允许不得转载。https://blog.csdn.net/xudailong_blog/article/details/78762262(一)前言:建议:慢慢看,也就这一篇用心了点写说来话长,一把辛酸泪,可算是弄好了。1起因:在很早很早,大一的时候,估计快记不得日子了,那时候来到PC吧创业团队,一个大一级的学长通过…

    2022年5月27日
    34
  • STM32CubeMX学习笔记(26)——SDIO接口使用(读写SD卡)

    STM32CubeMX学习笔记(26)——SDIO接口使用(读写SD卡)一、SDIO简介SD卡(SecureDigitalMemoryCard)在我们生活中已经非常普遍了,控制器对SD卡进行读写通信操作一般有两种通信接口可选,一种是SPI接口,另外一种就是SDIO接口。SDIO全称是安全数字输入/输出接口,多媒体卡(MMC)、SD卡、SDI/O卡都有SDIO接口。MMC卡可以说是SD卡的前身,现阶段已经用得很少。STM32F10x系列控制器有一个SDIO主机接口,它可以与MMC卡、SD卡、SDI/O卡以及CE-A

    2022年10月3日
    6
  • android 从相册选择图片_android调用相册并显示图片

    android 从相册选择图片_android调用相册并显示图片publicclassMainActivityextendsAppCompatActivity{intREQUEST_CODE=0;privateNineGridImageViewngiv;privateArrayListphotos;privateDialogdialog;privatecn.com.jwtimes.www.jwtimes.mAdaptermAdapter1…

    2022年9月22日
    5
  • MySQL增删改查_sql where case when

    MySQL增删改查_sql where case whensqlserver数据库中raiserror函数的用法server数据库中raiserror的作用就和asp.NET中的thrownewException一样,用于抛出一个异常或错误。这个错误可以被程序捕捉到。raiserror的常用格式如下:raiserror(‘错误的描述’,错误的严重级别代码,错误的标识,错误的描述中的参数的值(这个可以是多个),一些其它参数),在官方上的格式描述如下:…

    2025年6月17日
    4
  • 航姿基准系统_航姿系统原理

    航姿基准系统_航姿系统原理AHRS称为航姿参考系统包括多个轴向传感器,能够为飞行器提供航向,横滚和侧翻信息,这类系统用来为飞行器提供准确可靠的姿态与航行信息。航姿参考系统包括基于MEMS的三轴陀螺仪,加速度计和磁强计。航姿参考

    2022年8月5日
    10
  • python中griddata的外插值_SciPyTutorial-多元插值griddata

    python中griddata的外插值_SciPyTutorial-多元插值griddata11.ScipyTutorial-多维插值griddatascipy.interpolate模块下的griddata函数可以处理多元(维)函数的插值,以二元函数$f(x,y)$为例说明一下griddata的使用。与之前的一元函数插值interp1d相区别,interp1d是通过已知的点集$P={(x_i,y_i)|x_i\inR,y_i\inR}$通过interp1d可以找…

    2022年5月26日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号