《Java就业培训教程》 作者:张孝祥 书中源码
《Java就业培训教程》P218源码
程序清单:ReadLine.java
public class ReadLine
{
public static void main(String [] args)
{
byte buf[]=new byte[1024];
String strInfo=null;
int pos=0;
int ch=0;
System.out.println("please enter info, input bye for exit:");
while(true)
{
try
{
ch=System.in.read();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
switch(ch)
{
case '/r':
break;
case '/n':
strInfo= new String(buf,0,pos);
if(strInfo == "bye")
return ;
else
System.out.println(strInfo);
pos=0;
break;
default:
buf[pos++]=(byte)ch;
}
}
}
}
《Java就业培训教程》P221源码
程序清单:TestInteger.java
class TestInteger
{
public static void main(String[] args)
{
int w = Integer.parseInt(args[0]); //第一种方法
int h = new Integer(args[1]).intValue(); //第二种方法
//int h = Integer.valueOf(args[1]).intValue(); //第三种方法
for(int i=0;i<h;i++)
{
StringBuffer sb=new StringBuffer();
for(int j=0 ;j<w;j++)
{
sb.append('*');
}
System.out.println(sb.toString());
}
}
}
《Java就业培训教程》P223源码
程序清单:TestVector.java
import java.util.*; //下面用到的Vector类和Enumeration接口都在此包中
public class TestVector
{
public static void main(String [] args)
{
int b=0;
Vector v=new Vector();
System.out.println("Please Enter Number:");
while(true)
{
try
{
b= System.in.read();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
if(b=='/r' || b== '/n')
break;
else
{
int num=b-'0';
v.addElement(new Integer(num));
}
}
int sum=0;
Enumeration e=v.elements();
while(e.hasMoreElements())
{
Integer intObj=(Integer)e.nextElement();
sum += intObj.intValue();
}
System.out.println(sum);
}
}
《Java就业培训教程》P225源码
程序清单:TestCollection.java
import java.util.*; //ArrayList类和Iterator接口都在此包中
public class TestCollection
{
public static void main(String [] args)
{
int b=0;
ArrayList al=new ArrayList();
System.out.println("Please Enter Number:");
while(true)
{
try
{
b= System.in.read();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
if(b=='/r' | b== '/n')
break;
else
{
int num=b-'0';
al.add(new Integer(num));
}
}
int sum=0;
Iterator itr=al.iterator();
while(itr.hasNext())
{
Integer intObj=(Integer)itr.next();
sum += intObj.intValue();
}
System.out.println(sum);
}
}
《Java就业培训教程》P227源码
程序清单:TestSort.java
import java.util.*;
public class TestSort
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Integer(1));
al.add(new Integer(3));
al.add(new Integer(2));
System.out.println(al.toString()); //排序前
Collections.sort(al);
System.out.println(al.toString()); //排序后
}
}
《Java就业培训教程》P228源码
class MyKey
{
private String name;
private int age;
public MyKey(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return new String(name + "," + age);
}
public boolean equals(Object obj)
{
if(obj instanceof MyKey)
{
MyKey objTemp = (MyKey)obj;
if(name.equals(objTemp.name) && age==objTemp.age)
{
return true;
}
else
{
return false;
}
}
//假如obj不是MyKey类的实例对象,它就不可能与当前对象相等了
else
{
return false;
}
}
public int hashCode()
{
return name.hashCode() + age;
}
}
《Java就业培训教程》P229源码
程序清单:HashtableTest.java
import java.util.*;
public class HashtableTest
{
public static void main(String[] args)
{
Hashtable numbers=new Hashtable();
numbers.put(new MyKey("zhangsan",18),new Integer(1));
numbers.put(new MyKey("lisi",15),new Integer(2));
numbers.put(new MyKey("wangwu",20),new Integer(3));
Enumeration e=numbers.keys();
while(e.hasMoreElements())
{
MyKey key=(MyKey)e.nextElement();
System.out.print(key.toString()+"=");
System.out.println(numbers.get(key).toString());
}
}
}
《Java就业培训教程》P231源码
程序清单:PropertiesFile.java
import java.util.*;
import java.io.*;
class PropertiesFile
{
public static void main(String[] args)
{
Properties settings=new Properties();
try
{
settings.load(new FileInputStream("c://count.txt"));
}
catch(Exception e)
{
settings.setProperty("Count",new Integer(0).toString());
}
int c=Integer.parseInt(settings.getProperty("Count"))+1;
System.out.println("这是本程序第"+c+"次被使用");
settings.put("Count",new Integer(c).toString());
try
{
settings.store(new FileOutputStream("c://count.txt"),
"This Program is used:");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
《Java就业培训教程》P233源码
程序清单:TestProperties
import java.util.*;
public class SystemInfo
{
public static void main(String[] args)
{
Properties sp=System.getProperties();
Enumeration e=sp.propertyNames();
while(e.hasMoreElements())
{
String key=(String)e.nextElement();
System.out.println(key+" = "+sp.getProperty(key));
}
}
}
《Java就业培训教程》P234源码
程序清单:TestRuntime.java
public class TestRuntime
{
public static void main(String[] args)
{
Process p=null;
try
{
p=Runtime.getRuntime().exec("notepad.exe TestRuntime.java");
Thread.sleep(5000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
p.destroy();
}
}
《Java就业培训教程》P236源码
程序清单:TestDateFormat.java
import java.util.*;
import java.text.*;
public class TestDateFormat
{
public static void main(String[] args)
{
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy年MM月dd日");
try
{
Date d=sdf1.parse("2003-03-15");
System.out.println(sdf2.format(d));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}