常用的Java基本代码汇总
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. 列出文件和目录
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter; public class Test {
public static void main(String[] args) {
File dir = new File("F:\\韩顺平java");
String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i < children.length; i++) {
// Get filename of file or directory
String filename = children[i];
System.out.println(i+" ---"+filename);
}
}
// It is also possible to filter the list of returned files.
// This example does not return any files that start with '.'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
children = dir.list(filter);
// The list of files can also be retrieved as File objects
File[] files = dir.listFiles(); // This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter);
} }
9. 发送代数据的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();
}
常用的Java基本代码汇总的更多相关文章
- 常用的Java代码汇总
1. 字符串有整型的相互转换 Java 1 2 <strong>Stringa=String.valueOf(2); //integer to numeric ...
- Java知识汇总——思维导图
转载:https://www.cnblogs.com/java1024/p/8757952.html Java知识点汇总,从基础到常用的API.还有常用的集合类,总结的很详细.图片是从论坛里面找到的, ...
- Linux常用到的指令汇总
Linux常用到的指令汇总 根据鸟哥linux私房菜上定义的:一定要先學會的指令:ls, more, cd, pwd, rpm, ifconfig, find 登入與登出(開機與關機):telnet, ...
- jquery常用函数与方法汇总
1.delay(duration,[queueName]) 设置一个延时来推迟执行队列中之后的项目. jQuery1.4新增.用于将队列中的函数延时执行.他既可以推迟动画队列的执行,也可以用于自定义队 ...
- mysql copy表或表数据常用的语句整理汇总
mysql copy表或表数据常用的语句整理汇总. 假如我们有以下这样一个表: id username password ----------------------------------- 1 a ...
- Vue常用经典开源项目汇总参考-海量
Vue常用经典开源项目汇总参考-海量 Vue是什么? Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的 ...
- (转)JAVA排序汇总
JAVA排序汇总 package com.softeem.jbs.lesson4; import java.util.Random; /** * 排序测试类 * * 排序算法的分类如下: * 1.插入 ...
- Java设计模式汇总
Java设计模式汇总 设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式. ...
- 常用的Java转义字符
1.常用的Java转义字符 \n : 回车 \t : 水平制表符 \r : 换行 \f : 换页 \' : 单引号 \'' : 双引号 ...
随机推荐
- python基础一 day14 生成器函数进阶(1)
- python_95_类变量的作用及析构函数
参考:http://www.cnblogs.com/alex3714/articles/5188179.html #类变量的用途:大家共有的属性,节省内存 class Person(): cn='Ch ...
- 【转】JavaScript 节点操作 以及DOMDocument属性和方法
最近发现DOMDocument对象很重要,还有XMLHTTP也很重要 注意大小写一定不能弄错. 属性: 1Attributes 存储节点的属性列表(只读) 2childNodes 存储节点的子节点列表 ...
- javaweb基础(16)_jsp指令
一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...
- Bootstrap历练实例:默认的面板(Panels)
Bootstrap 面板(Panels) 本章将讲解 Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素 ...
- C#数组简介
一.数组的定义 数组:是一种包含若干个变量的数据结构,这些变量可以通过索引进行访问. 数组的元素:数组中的变量就称为数组的元素. 元素类型:数组中的元素具有相同的数据类型,该数据类型就称为数组的元素类 ...
- Linux下如何通过命令检查网卡是否插上网线
How To:Linux下如何通过命令检查网卡是否插上网线 主要工具为ethtool来检查,主要关注的字段为"Link detected",注意如下的输出,其中em4实际物理上 ...
- 无需上传附件到服务器,Servlet读取Excel(二)
package com.str; import java.io.File;import java.io.FileInputStream;import java.io.IOException; impo ...
- centos 7 安装WordPress的参考博文
安装方法: https://www.cnblogs.com/flankershen/p/7476415.html 安装完,测试不成功的解决办法: https://blog.csdn.net/u0104 ...
- python getopt模块使用方法
python中 getopt 模块,是专门用来处理命令行参数的 getop标准格式: 函数getopt(args, shortopts, longopts = []) shortopts 是短参数 ...