常用的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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 基于STM32的嵌入式语音识别模块设计实现「建议收藏」

    基于STM32的嵌入式语音识别模块设计实现「建议收藏」介绍了一种以ARM为核心的嵌入式语音识别模块的设计与实现。模块的核心处理单元选用ST公司的基于ARMCortex-M3内核的32位处理器STM32F103C8T6。本模块以对话管理单元为中心,通过以LD3320芯片为核心的硬件单元实现语音识别功能,采用嵌入式操作系统μC/OS-II来实现统一的任务调度和外围设备管理。经过大量的实验数据验证,本文设计的语音识别模块具有高实时性、高识别率、高稳定性的…

    2022年6月26日
    31
  • jquery拼音转汉字搜索[通俗易懂]

    jquery拼音转汉字搜索[通俗易懂]HTML:1DOCTYPEhtmlPUBLIC”-//W3C//DTDXHTML1.0Transitional//EN””http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>2htmlxmlns=”http://www.w3.org/1999/xhtml”>34head>5

    2022年7月24日
    12
  • altas(ajax)控件(二十一):判断密码的强弱程度的控件PasswordStrength

    altas(ajax)控件(二十一):判断密码的强弱程度的控件PasswordStrength

    2021年7月25日
    50
  • python zipfile_python zipfile模块学习笔记(一)

    python zipfile_python zipfile模块学习笔记(一)ZIP文件格式是一种常见的存档和压缩标准,这个zipfile模块提供了工具来创建、读取、写入、附加和列出一个ZIP文件。使用ZIP64扩展(即压缩文件大小超过4G),它能解压加密的ZIP文件,解密过程很慢。1、测试是否为ZIP文件is_zipfile()函数会返回一个布尔值来表示是否为ZIP文件,代码如下:#!/usr/bin/pythonimportzipfileforfilenamein…

    2022年9月17日
    4
  • 郑州java面试难吗_java开发人员培训机构

    郑州java面试难吗_java开发人员培训机构前言周末花了2天时间学习了额RabbitMQ,总结了最核心的知识点,带大家快速掌握RabbitMQ,整理不易希望帮忙点赞,转发,分享下,谢谢目录进入SpringBoot世界讲述Sping、SpringBoot和SpringCloud之间的关系,还重点讲述了如何利用开发工具(如IDEA)来实现开发,如何通过API文档来寻找类对象方法,告诉我们在开发过程中如何学习、发现和解决问题需要免费领取这份Alibaba架构师耗时一年整理的《SpringBoot实战,让你的开发像搭积木一样简单

    2022年9月28日
    2
  • java二维数组随机赋值_java 二维数组随机赋值

    java二维数组随机赋值_java 二维数组随机赋值java二维数组随机赋值[2021-01-3100:08:55]简介:目的:使用二维数组打印一个10行杨辉三角。(视频教程推荐:java课程)思路:1.第一行有1个元素,第n行有n个元素;2.每一行的第一个元素和最后一个元素都是1;3.从第三行开始php修改二维数组中值的方法:1、通过【for($i=0;$i<count(Array());++…

    2022年6月4日
    63

发表回复

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

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