您的位置:首页 > 职场人生

求职这几天编写的TEST程序

2016-03-04 16:53 405 查看
离职求职是一个煎熬的过程,不得不重新拾起那些个“旧物”。复习怎么少得了hello world?废话不多说代码贴上!

public class CopyFile {
public static void main(String[] args) throws IOException {
File f2 = new File("d:\\","copy.txt");
File f1 = new File("d:\\a.txt");
//if(!f2.isDirectory())f2.mkdir();
if(!f2.exists())f2.createNewFile();
if(!f1.exists())f1.mkdirs();
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] b = new byte[1000];
while (in.read(b)!=-1){
out.write(b);
}
out.close();
in.close();
}
}
/**
* 证明了前面的异常输出了就不会输出后面的了
*/
public class ExceptionTest {
public static void main(String[] args) {
try {
throw new TestException();
} catch (TestException e) {
System.out.println("test异常");
}catch (Exception e1) {
System.out.println("总异常");
}
}
}
class TestException extends Exception {
}
/**
* 冒泡排序
* @author liuyizhi
*/
public class maopao {
public static void main(String[] args) {
int a[]={1,9,4,3,5,6,7,8,2};
for(int i=0; i<a.length;i++){
for(int j=0;j<a.length-i-1;j++){
if(a[j]>a[j+1]){
int k = a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}
for(int i:a){
System.out.print(i);
}
}
}
/**
* 线程取票的
* */
public class MyThread implements Runnable{
public Integer ticket = 10;
public static void main(String[] args) {
MyThread my = new MyThread();
new Thread(my,"张三").start();
new Thread(my,"李四").start();
}
@Override
public void run() {
while(ticket>0){
synchronized(this){
this.ticket = ticket -1;
System.out.println(Thread.currentThread().getName()+"取了票,还剩"+ticket+"张余票");
}
}
}
}

下面是日期类的代码,也不知道从哪拷过来的,记得当初开始工作不问工具类就自已造轮子,结果挨P,贴上纪念吧。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MyCalendar {
public static void main(String[] args) throws ParseException {
System.out.println(MyCalendar.getMonthSpace("1982-11-8", "2012-12-12"));

}

public static int getMonthSpace(String date1, String date2)
throws ParseException {

int result = 0;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();

c1.setTime(sdf.parse(date1));
c2.setTime(sdf.parse(date2));

//设置时间为0时
c1.set(java.util.Calendar.HOUR_OF_DAY, 0);
c1.set(java.util.Calendar.MINUTE, 0);
c1.set(java.util.Calendar.SECOND, 0);
c2.set(java.util.Calendar.HOUR_OF_DAY, 0);
c2.set(java.util.Calendar.MINUTE, 0);
c2.set(java.util.Calendar.SECOND, 0);
//得到两个日期相差的天数
int days = ((int) (c2.getTime().getTime() / 1000) - (int) (c1
.getTime().getTime() / 1000)) / 3600 / 24;
// result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);

// return result == 0 ? 1 : Math.abs(result);
return days;
}

下面是测试对象池代码,后来用来测试路径了。

public class ObjectPoolFactory {

private Map<String,Object> objectPool = new HashMap<String,Object>();
private Object createObject(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
Class<?> clazz = Class.forName(className);
return clazz.newInstance();
}
public void initPool(String fileName)throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
//FileInputStream fis = null;
InputStream is = null;
try{
//fis = new FileInputStream(fileName);
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
Properties prop = new Properties();
prop.load(is);
for(String name:prop.stringPropertyNames())
{
objectPool.put(name, createObject(prop.getProperty(name)));
//System.out.println(name+"="+prop.getProperty(name));
}
}
catch(IOException e){
System.out.println("error1");
}
finally{
try{
if(is!=null)
{
is.close();
}
}
catch(IOException e){
System.out.println("error2");
}
}

}
public Object getObject(String name)
{
return objectPool.get(name);
}
public static void main(String[] args) throws Exception {

//ObjectPoolFactory pf = new ObjectPoolFactory();
//pf.initPool("./src/test/obj.txt");
//String s=ObjectPoolFactory.class.getResource("").getFile();与getPath一样,可得绝对路径
//pf.initPool(ObjectPoolFactory.class.getResource("/").getPath()+"resource/obj.txt");
//pf.initPool("resource/obj.txt");
//System.out.println(pf.getObject("a"));
//System.out.println(((User) pf.getObject("b")).getUser());
//以下得到的是URL,要转为path
System.out.println(ObjectPoolFactory.class.getResource("")+"obj.txt");
System.out.println(ObjectPoolFactory.class.getResource("/")+"resource/obj.txt");
System.out.println(ObjectPoolFactory.class.getClassLoader().getResource("") +"obj.txt");
System.out.println(System.getProperty("user.dir")+"/obj.txt");
System.out.println(ClassLoader.getSystemResource("") +"obj.txt");
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath() +"obj.txt");//推荐
String s=ObjectPoolFactory.class.getResource("")+"obj.txt";

}

}


还有一段是用来通过反射注解对比持久类的数据库字段值是否一样的代码,找了半天没找到……略有失落的说。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java