phones.java

来自「java的书上例子」· Java 代码 · 共 51 行

JAVA
51
字号
import java.io.*; 
class Phones        //生成数据库的程序
{
	static FileOutputStream fos;
	public static final int lineLength = 81;
	public static void main(String args[]) throws IOException
	{
		byte[] phone = new byte[lineLength];
		byte[] name = new byte[lineLength];
		int I;
		try 
		{
			fos = new FileOutputStream("phone.numbers");
		}
		catch(FileNotFoundException e)
		{}
		while (true)
		{
			System.err.println("Enter a name (enter 'done' to quit)");
			readLine(name);
			if ("done".equalsIgnoreCase(new String(name,0,4)))
			{
				break; 
			}
			System.err.println("Enter the phone number"); 
			readLine(phone);
			for (int i=0;phone[i]!= 0;i++) 
			{
				fos.write(phone[i]);
			}
			fos.write(',');
			for (int i=0;name[i]!= 0;i++)
			{
				fos.write(name[i]); 
			}
			fos.write('\n');
		}
		fos.close();
	} 

	private static void readLine(byte line[]) throws IOException
	{ 
		int i=0,b=0; 
		while ((i<lineLength-1)&&((b=System.in.read())!='\n'))
		{
			line[i++] = (byte)b;
		} 
		line[i]=(byte) 0;
	} 
}

⌨️ 快捷键说明

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