通讯录:
importjava.io.Serializable;
public class Phone implementsSerializable{
privateString name;
privateString number;
publicString getName() {
returnname;
}
publicvoid setName(String name) {
this.name= name;
}
publicString getNumber() {
returnnumber;
}
publicvoid setNumber(String number) {
this.number= number;
}
}
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.util.Vector;
public class Main{
publicstatic String getLine() throws Exception {
byte[]b = new byte[100];
System.in.read(b);
returnnew String(b).trim();
}
publicstatic void hy(){
System.out.println("-----------------------------------");
System.out.println("\t通讯录 管理");
System.out.println("1.添加");
System.out.println("2.查看");
System.out.println("3.修改");
System.out.println("4.删除");
System.out.println("-----------------------------------");
}
privatestatic Vector<Phone> phones = new Vector();
publicstatic void save() throwsException {
FileOutputStream out = newFileOutputStream("./obj");
ObjectOutputStream outt = new ObjectOutputStream(out);
outt.writeObject(phones);
outt.close();
}
publicstatic void open() throwsException {
if(!new File("./obj").exists()){
return;
}
FileInputStream in = newFileInputStream("./obj");
ObjectInputStream oin = new ObjectInputStream(in);
phones= (Vector) oin.readObject();
oin.close();
}
publicstatic void main(String[] args) throwsException {
open();
while(true) {
hy();
System.out.print("请选择:");
String cmd = getLine();
if (cmd.equals("1")) {
Phone p = new Phone();
System.out.println("---------------------------");
System.out.print("请输入姓名:");
cmd = getLine();
p.setName(cmd);
System.out.print("请输入电话号码:");
cmd = getLine();
p.setNumber(cmd);
phones.add(p);
System.out.println("ok");
save();
} else if(cmd.equals("2")) {
System.out.println("---------------------------");
System.out.println("\t姓名\t电话");
for (Phone p : phones){
System.out.print("\t" + p.getName());
System.out.println("\t" + p.getNumber());
}
} else if(cmd.equals("3")) {
int row=0;
System.out.println("---------------------------");
System.out.println("编号\t姓名\t电话");
for (Phone p : phones){
System.out.print(row++);
System.out.print("\t" + p.getName());
System.out.println("\t" + p.getNumber());
}
System.out.println("请输入编号:");
cmd = getLine();
try{
Phone p=phones.get(Integer.parseInt(cmd));
System.out.println("1.姓名\t2.电话\t3.全部\t4.退出");
cmd = getLine();
if(cmd.equals("1")){
System.out.print("请输入姓名:");
cmd = getLine();
p.setName(cmd);
System.out.println("修改成功");
}else if(cmd.equals("2")){
System.out.print("请输入电话:");
cmd = getLine();
p.setNumber(cmd);
System.out.println("修改成功");
}else if(cmd.equals("3")){
System.out.print("请输入姓名:");
cmd = getLine();
p.setName(cmd);
System.out.print("请输入电话:");
cmd = getLine();
p.setNumber(cmd);
System.out.println("修改成功");
}
}catch(Exception e){
System.out.println("修改失败");
}
}else if(cmd.equals("4")) {
int row=0;
System.out.println("---------------------------");
System.out.println("编号\t姓名\t电话");
for (Phone p : phones){
System.out.print(row++);
System.out.print("\t" + p.getName());
System.out.println("\t" + p.getNumber());
}
System.out.print("请输入编号:");
cmd = getLine();
try{
phones.remove(Integer.parseInt(cmd));
System.out.print("删除成功!");
}catch(Exception e){
System.out.print("删除失败!");
}
}else if(cmd.equals("886")) {
System.exit(0);
}
}
}
}
1.垃圾回收:Protected void finalize() throws throwable{ }
是不定时的督促系统进行垃圾回收。
2.属性文件加载类 : Properties
Properties p=new Properties();先加载后使用
p.load(newFileInputStream(“d:\\a.txt”));
System.out.print(“yyyy”);
3.16位流: 主要解决字符,只操控字符,是一种国际化文本。8位不能做国际化文本,因为不支持多国语言。
16位流有 FileReader BufferedReader PrintWriter
FileWriter BufferedWriter
4.添加:FileWriter w=new FileWriter(“d:\\练习\\c.txt”);
w.writer(“fsagdhsr”);
w.close();
5.写入:PrintWriter out=new PrintWriter(“./a.txt”);
out.print(“fshiieawifwea”);
out.close();
6.8位转16位是允许的(用装饰可以实现),但16位不能转化为8位
例:将8位转化为16位,再读进一行
// 写入操作,并转化为16位
File f=new File(“./16.txt”);
FileOutputStreamout=new FileOutputStream(f);
BufferedOutputStreambout=new BufferedOutputStream(out);
BufferedWriter w=newBufferedWriter(new OutputStreamWriter(bout));
w.writer(“sssssssssssss”);
w.newLine();
w.close();
// 读操作,并转化为16位
File ff=new File(“./16.txt”);
FileInputStreamin=new FileInputStream(ff);
BufferedInputStreambin=new BufferedInputStream(in);
BufferedWriter r=newBufferedWriter(new InputStreamWriter(bin));
String line=null;
While((line=r.readLine())!=null){
System.out.println(line);
}
r.close();
7.代码转换器
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Date;
public class CodeRowCount {
public static voidgetFile(File f) {
if (f.isFile()) {
if(f.getName().toUpperCase().endsWith(".JAVA")) {
try {
fileCount++;
System.out.println("正在统计:"+f.getPath());
count(f);
System.out.println("统计完毕:"+f.getPath());
} catch(Exception e) {
}
}
} else {
File[] fs =f.listFiles();
if (fs !=null) {
for (Filefile : fs) {
getFile(file);
}
}
}
}
static int rowCount =0;
static int rowCount1 =0;
static intfileCount=0;
public static voidcount(File codePath) throws Exception {
BufferedReader red= new BufferedReader(new FileReader(codePath));
String line ="";
while ((line =red.readLine()) != null) {
if(line.trim().length() != 0) {
if(line.trim().indexOf("//") != 0) {
if(line.trim().indexOf("/*") == 0) {
rowCount1++;
while((line = red.readLine()) != null) {
rowCount1++;
if(line.indexOf("*/") >= 0) {
if(line.indexOf("*/") + 2 != line.length()) {
rowCount++;
}
break;
}
}
} elseif (line.trim().indexOf("/*") > 0) {
rowCount++;
rowCount1++;
while((line = red.readLine()) != null) {
if(line.indexOf("*/") >= 0) {
if(line.indexOf("*/") + 2 != line.length()) {
rowCount++;
}
break;
}
}
} else{
rowCount++;
}
} else {
rowCount1++;
}
}
}
red.close();
}
public static voidmain(String[] args) throws Exception {
long time=newDate().getTime();
getFile(newFile(args[0]));
long endTime=newDate().getTime();
javax.swing.JOptionPane.showMessageDialog(null,"\n共计代码行数:"+rowCount+"行\n"
+"共计注释行数:"+rowCount1+"行\n"+
"统计文件:"+fileCount+"个\n"+
"耗时:"+((endTime-time)/1000)+"秒\n"); }
}