⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sourcecode.txt

📁 张孝祥的《Java就业培训教程》书中源代码
💻 TXT
📖 第 1 页 / 共 5 页
字号:
		q.sex="男";
	}
	else
	{
q.name="陈琼";
		q.sex="女";
	}
	i=(i+1)%2;
}
}
}
class Q
{
	String name="陈琼";
	String sex="女";
}
class Consumer implements Runnable
{
	Q q=null;
	public Consumer(Q q)
	{
		this.q=q;
	}
	public void run()
{
	while(true)
{
	System.out.println(q.name + "---->" + q.sex);
}
}
} 
public class ThreadCommunation
{
	public static void main(String [] args)
	{
		Q q=new Q();
		new Thread(new Producer(q)).start();
		new Thread(new Consumer(q)).start();
	}
}
 
   
--------------------------------------------------------------------------------
 
《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());
		}
	}
}
 
   
--------------------------------------------------------------------------------
 
《Java就业培训教程》 作者:张孝祥 书中源码 
《Java就业培训教程》P239源码
程序清单:FileTest.java
import java.io.*;
public class FileTest
{
	public static void main(String[] args)
	{
		File f=new File("c:\\1.txt");
		if(f.exists())
			f.delete();
		else
			try
			{
				f.createNewFile();
			}
			catch(Exception e)
			{
				System.out.println(e.getMessage());
			}
		System.out.println("File name:"+f.getName());
		System.out.println("File path:"+f.getPath());
		System.out.println("Abs path:"+f.getAbsolutePath());
		System.out.println("Parent:"+f.getParent());
		System.out.println(f.exists()?"exists":"does not exist");
		System.out.println(f.canWrite()?"is writeable":"is not writeable");
		System.out.println(f.canRead()?"is readable":"is not readable");
		System.out.println(f.isDirectory()?"is ":"is not"+" a directory");
		System.out.println(f.isFile()?"is normal file":"might be a named pipe");
		System.out.println(f.isAbsolute()?"is absolute":"is not absolute");
		System.out.println("File last modified:"+f.lastModified());
		System.out.println("File size:"+f.length()+" Bytes");
	}
}
《Java就业培训教程》P242源码
程序清单:RandomFileTest.java
import java.io.*;
public class RandomFileTest
{
	public static void main(String [] args) throws Exception 
	{
		Employee e1 = new Employee("zhangsan",23);
		Employee e2 = new Employee("Lisi",24);
		Employee e3 = new Employee("Wangwu",25);
		RandomAccessFile ra=new RandomAccessFile("c:\\1.txt","rw");
		ra.write(e1.name.getBytes());
		ra.writeInt(e1.age);
		ra.write(e2.name.getBytes());
		ra.writeInt(e2.age);
		ra.write(e3.name.getBytes());
		ra.writeInt(e3.age);
		ra.close();
		RandomAccessFile raf=new RandomAccessFile("c:\\1.txt","r");
		int len=8;
		raf.skipBytes(12); //跳过第一个员工的信息,其中姓名8字节,年龄4字节
		System.out.println("第二个员工信息:");
		String str="";
		for(int i=0;i<len;i++)
			str=str+(char)raf.readByte();
		System.out.println("name:"+str);
		System.out.println("age:"+raf.readInt());
		
		System.out.println("第一个员工的信息:");
		raf.seek(0); //将文件指针移动到文件开始位置
		str="";
		for(int i=0;i<len;i++)
			str=str+(char)raf.readByte();
		System.out.println("name:"+str);
		System.out.println("age:"+raf.readInt());
		
		System.out.println("第三个员工的信息:");
		raf.skipBytes(12);  //跳过第二个员工信息
		str="";
		for(int i=0;i<len;i++)
			str=str+(char)raf.readByte();
		System.out.println("name:"+str.trim());
		System.out.println("age:"+raf.readInt());
		
		raf.close();
	}
}
class Employee
{		
		String name;
		int age;
		final static int LEN=8;
		public Employee(String name,int age)
		{
			if(name.length()>LEN)
			{
				name = name.substring(0,8);
			}
			else
			{
				while(name.length()<LEN)
					name=name+"\u0000";
			}
			this.name=name;
			this.age=age;			
		}
}
《Java就业培训教程》P247源码
程序清单:FileStream.java
import java.io.*;
public class FileStream
{
	public static void main(String[] args)
	{
		File f = new File("hello.txt"); 
		try
		{
			FileOutputStream out = new FileOutputStream(f);
			byte buf[]="www.it315.org".getBytes();
			out.write(buf);
			out.close();
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		
		try
		{
			FileInputStream in = new FileInputStream(f);
			byte [] buf = new byte[1024];
			int len = in.read(buf);
			System.out.println(new String(buf,0,len));
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}
《Java就业培训教程》P250源码
程序清单:PipeStreamTest.java
import java.io.*;
public class PipeStreamTest
{
    public static void main(String args[])
    {
        try
        {
            Sender t1=new Sender();
            Receiver t2=new Receiver();
            PipedOutputStream out = t1.getOutputStream();
            PipedInputStream in = t2.getInputStream();
            out.connect(in);
            t1.start();
            t2.start();
        }
        catch(IOException e)
        {
        	System.out.println(e.getMessage());
        }
    }
}
class Sender extends Thread
{
    private PipedOutputStream out=new PipedOutputStream();
    public PipedOutputStream getOutputStream()
    {
        return out;
    }
    public void run()
    {
        String s=new String("hello,receiver,how are you!");
        try
        {
            out.write(s.getBytes());
            out.close();
        }
        catch(IOException e)
        {
        	System.out.println(e.getMessage());
        }
    }
}
class Receiver extends Thread
{
    private PipedInputStream in=new PipedInputStream();
    public PipedInputStream getInputStream()
    {
        return in;
    }
    public void run()
    {
        String s=null;
	   byte [] buf = new byte[1024];
        try
        {
            int len =in.read(buf);
		   s = new String(buf,0,len);
            System.out.println(
"the following message comes from sender:\n"+s);
            in.close();
        }
        catch(IOException e)
        {
        	System.out.println(e.getMessage());
        }
    }
}
《Java就业培训教程》P253源码
程序清单:ByteArrayTest.java
import java.io.*;
public class ByteArrayTest
{
	public static void main(String[] args) throws Exception
	{
		String tmp="abcdefghijklmnopqrstuvwxyz";
		byte [] src =tmp.getBytes();//src为转换前的内存块
		ByteArrayInputStream input = new ByteArrayInputStream(src);
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		new ByteArrayTest().transform(input,output);
		byte [] result = output.toByteArray();//result为转换后的内存块
		System.out.println(new String(result));
	}
	public void transform(InputStream in,OutputStream out)
	{
		int c=0;
		try
		{
			while((c=in.read())!=-1)//read在读到流的结尾处返回-1
			{
				int C = (int)Character.toUpperCase((char)c);
				out.write(C);
			
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -