常用的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 : 换页 \' : 单引号 \'' : 双引号 ...
随机推荐
- JSONPath - XPath for JSON
http://goessner.net/articles/JsonPath/ [edit] [comment] [remove] |2007-02-21| e1 # JSONPath - XPath ...
- kubernetes监控-prometheus(十六)
监控方案 cAdvisor+Heapster+InfluxDB+Grafana Y 简单 容器监控 cAdvisor/exporter+Prometheus+Grafana Y 扩展性好 容器,应用, ...
- iOSAES加密的实现
+(NSData *)AES256ParmEncryptWithKey:(NSString *)key Encrypttext:(NSData *)text //加密 { char keyPtr[k ...
- Unity学习之路——主要类
学习https://blog.csdn.net/VRunSoftYanlz/article/details/78881752 1.Component类gameObject:组件附加的游戏对象.组件总是 ...
- BZOJ-3679(数位DP)
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a,b; int k[20]; ll dp[2 ...
- centos7无法切换startx
centos光盘安装后,显示命令行模式,通过startx无法进入图形界面? 解决方法:1.使用yum grouplist查看,根据显示的结果采用不同的界面格式,我用的是 yum groupinstal ...
- SpringMVC 项目中引用其他 Module 中的方法
1. 将要引用的Module 引入项目中 2. 在主Module中添加依赖, 3. 被引用的类必须放在 Module 中/src/下的某个package中,否则引用不到(重要)
- 使用powershell批量更新git仓库
Get-ChildItem D:\GitHub\NetCore | ForEach-Object -Process{ cd $_.name; git pull; cd ../ }
- Python json和simplejson的使用
在Python中,json数据和字符串的转换可以使用json模块或simplejson模块. json从Python2.6开始内置到了Python标准库中,我们不需要安装即可直接使用. simplej ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...