Java学习笔记(二)不定时更新
Java语言画图
package cn.witksy.dev; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; /** * Author: Alfred * Created: 2015/5/7 */ public class Main { public void run() { BufferedImage bufferedImage = new BufferedImage(100,50,BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bufferedImage.createGraphics(); g2.setColor(Color.red); g2.fillRect(0,0,5,5); g2.setColor(Color.blue); g2.fillRect(10,20,5,5); File outputFile = new File("D:\\dev\\java\\workspace\\draw2dtest\\test.png"); try { ImageIO.write(bufferedImage, "png", outputFile); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { new Main().run(); } }
不循环,直接打印List元素
[12.3, 1.3, 10.3]
<span style="font-size:14px;">打印一个数组中的所有的元素: 1、使用for循环打印 2、将数组转化为有序的List打印出来 package com.inc.test; import java.util.Arrays; public class Test { public static void main(String[] args) { String names[] = { "Georgianna", "Tenn", "Simon", "Tom" }; System.out.println(Arrays.asList(names)); } } </span>
1.首先新建Test.java
2.javac Test.java
3.jar cvf test.jar Test.class
//更新manifest.mf 将自己写的manifest.mf 放到Test.class目录
内容为 Main-Class: Test注意回车
4.jar umf MANIFEST.MF test.jar
5.java -jar test.jar 你的参数
- public class Test
- {
- public static void main(String[] args) {
- String str = args[0];
- System.out.println(str);
- }
- }
2.javac Test.java
3.jar cvf test.jar Test.class
//更新manifest.mf 将自己写的manifest.mf 放到Test.class目录
内容为 Main-Class: Test注意回车
4.jar umf MANIFEST.MF test.jar
5.java -jar test.jar 你的参数
下载net.sf.fjep.fatjar_0.0.31 http://pan.baidu.com/s/1sj0uawX
或者参考
在编译和运行时我们也要加上依赖的jar包,需要注意的是,使用 java -cp 有额外的jar的时候:在Linux下面ClassPath前面是一个点号加一个冒号;在Windows下面ClassPath前面是一个点号加一个分号
http://blog.csdn.net/czw698/article/details/44353453
Java控制台输出信息console重定向到文件,后恢复到console
1 import java.io.FileNotFoundException; 2 import java.io.PrintStream; 3 4 public class Test { 5 6 public static void main(String[] args){ 7 PrintStream o = System.out; 8 try { 9 System.setOut(new PrintStream("1.txt")); //重定向到文件1.txt 10 } catch (FileNotFoundException e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace(); 13 } 14 System.out.println("line1"); 15 System.setOut(o); //重定向恢复到控制台 16 System.out.println("line2"); 17 } 18 19 }
得到指定时间和指定周期的详细日期时间
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * 得到指定时间和指定周期的详细日期时间 * @author DBClient * */ public class TestTime { private static final SimpleDateFormat df1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); private static final SimpleDateFormat df2 = new SimpleDateFormat( "yyyy-MM-dd"); public static void main(String[] args) throws Exception { // String timeTmp = "00:00"; // Calendar tmpCal = Calendar.getInstance(); // tmpCal.setTime(df2.parse(timeTmp)); // tmpCal.add(Calendar.MINUTE, -5); // // //System.out.println(df2.format(tmpCal.getTime())); // int j = 1; // while(j <= 2){ // System.out.println(Math.pow(-1, j)); // j++; // } String test = "2016-01-23"; // new TestTime().calcTime(test); new TestTime().getMonth(); new TestTime().getWeek(); new TestTime().getQuarter(); new TestTime().getYear(); } public void calcTime(String testTime) { try { // 周 Date times = df2.parse(testTime); Calendar cal = Calendar.getInstance(); cal.setTime(times); cal.add(Calendar.DATE, -7); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(df2.format(cal.getTime())); cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY); System.out.println(df2.format(cal.getTime())); // 月 cal.add(Calendar.MONTH, -1); cal.set(Calendar.MONTH, 0); System.out.println(df2.format(cal.getTime())); System.out.println(cal.get(Calendar.MONTH) + 1); // 季度 // 年 // cal.add(Calendar.WEEK_OF_MONTH, -1); // System.out.println(df2.format(cal.getTime())); // //cal.add(Calendar.MONTH, -1); // System.out.println(df2.format(cal.getTime())); // System.out.println(cal.get(Calendar.WEEK_OF_YEAR)); } catch (Exception e) { e.printStackTrace(); } } /** * 月 * @throws Exception */ public void getMonth() throws Exception { String test = "2015-02-23"; List<String> ret = new ArrayList<String>(); Date times = df2.parse(test); Calendar cal = Calendar.getInstance(); cal.setTime(times); cal.add(Calendar.MONTH, -1); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); cal.set(year, month, 1); while(cal.get(Calendar.MONTH) == month){ ret.add(df2.format(cal.getTime())); cal.add(Calendar.DATE, 1); } System.out.println(ret); } /** * 周 * @throws Exception */ public void getWeek() throws Exception { String test = "2016-01-09"; List<String> ret = new ArrayList<String>(); Date times = df2.parse(test); Calendar cal = Calendar.getInstance(); cal.setTime(times); cal.add(Calendar.DATE, -7); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); while(ret.size() < 7){ ret.add(df2.format(cal.getTime())); cal.add(Calendar.DATE, 1); } System.out.println(ret); } public void getQuarter() throws Exception { String test = "2016-03-01"; List<String> ret = new ArrayList<String>(); Date times = df2.parse(test); Calendar cal = Calendar.getInstance(); cal.setTime(times); cal.add(Calendar.MONTH, -3); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); if(month >= 0&& month < 3){ month = 0; }else if(month >= 3 && month < 6){ month = 3; }else if(month >= 6 && month < 9){ month = 6; }else if(month >= 9 && month < 12){ month = 9; } cal.set(year, month, 1); while(cal.get(Calendar.MONTH) <= month + 2 && cal.get(Calendar.YEAR) == year){ ret.add(df2.format(cal.getTime())); cal.add(Calendar.DATE, 1); } System.out.println(ret); } public void getYear() throws Exception { String test = "2016-05-09"; List<String> ret = new ArrayList<String>(); Date times = df2.parse(test); Calendar cal = Calendar.getInstance(); cal.setTime(times); cal.add(Calendar.YEAR, -1); int year = cal.get(Calendar.YEAR); cal.set(year, 0, 1); while(cal.get(Calendar.YEAR) == year){ ret.add(df2.format(cal.getTime())); cal.add(Calendar.DATE, 1); } System.out.println(ret); } }
Java 依赖包相关运行技巧
有时候不能向服务器上传jar包,或者交涉过于繁琐;只有复制粘贴的权限 可采取: 1 上传jar包的解压文件,可用zip解压 2 把自己的源码复制粘贴到解压文件的目录下,即可找到依赖包 或者把jar包添加到java的LibClass path目录下
导出乱码: 1、可能是linux缺少中文 命令:locale 命令:fc-list :lang=zh 2、可能是jdk缺少中文simsun.ttc 放:$JAVA_HOME/jreb/fonts/fallback
simsun.ttc下载: http://pan.baidu.com/s/1jHy5Uaq
JAVA多态原理解释
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//定义超类superA class superA { int i = 100; void fun() { System.out.println(“This is superA”); } } //定义superA的子类subB class subB extends superA { int m = 1; void fun() { System.out.println(“This is subB”); } } //定义superA的子类subC class subC extends superA { int n = 1; void fun() { System.out.println(“This is subC”); } } class Test { public static void main(String[] args) { superA a; subB b = new subB(); subC c = new subC(); a=b; a.fun(); (1) a=c; a.fun(); (2) } } /* 运行结果为: This is subB This is subC 上述代码中subB和subC是超类superA的子类,我们在类Test中声明了3个引用变量a, b, c,通过将子类对象引用赋值给超类对象引用变量来实现动态方法调用。也许有人会问:“为什么(1)和(2)不输出:This is superA”。java 的这种机制遵循一个原则:当超类对象引用变量引用子类对象时,被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法,但是这个被调用的方法必须是在超类中定义过的,也就是说被子类覆盖的方法。 所以,不要被上例中(1)和(2)所迷惑,虽然写成a.fun(),但是由于(1)中的a被b赋值,指向了子类subB的一个实例,因而(1)所调用的fun()实际上是子类subB的成员方法fun(),它覆盖了超类superA的成员方法fun();同样(2)调用的是子类subC的成员方法fun()。 另外,如果子类继承的超类是一个抽象类,虽然抽象类不能通过new操作符实例化,但是可以创建抽象类的对象引用指向子类对象,以实现运行时多态性。具体的实现方法同上例。 不过,抽象类的子类必须覆盖实现超类中的所有的抽象方法,否则子类必须被abstract修饰符修饰,当然也就不能被实例化了。 */ http://developer.51cto.com/art/200909/153887.htm
JAVA多线程样例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Test implements Runnable { private String name; private static int flag = 0; public Test(String name) { this.name = name; } @Override public void run() { while (flag < 100) { flag++; System.out.println(name + ": " + flag); } } public static void main(String[] args) { long startTime = System.currentTimeMillis(); for (int i = 0; i < 2; i++) { new Thread(new Test("thread-" + (i + 1))).start(); } long endTime = System.currentTimeMillis(); System.out.println("spend times: " + (endTime - startTime) / 1000 + " s"); } }