2020-08-27

第十七次课

package com.hpe.bean;

/**

  • 类描述: 电话本 实体类

  • @author Administrator

  • @see v1.0

  • @date 2020-8-24
    */
    public class Telephone {

    private String name; /* 姓名 */

    private int age; /* 年龄 */

    private String phone; /* 电话 */

    private String addr ; /* 地址 */

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }

    public String getPhone() {
    return phone;
    }

    public void setPhone(String phone) {
    this.phone = phone;
    }

    public String getAddr() {
    return addr;
    }

    public void setAddr(String addr) {
    this.addr = addr;
    }

    @Override
    public String toString() {
    return “Telephone [name=” + this.name + “, age=” +
    this.age + “,phone=” + this.phone + “,addr=” + this.addr + “]”;
    }

}

package com.hpe.dao;

import java.util.ArrayList;
import java.util.List;

import com.hpe.bean.Telephone;

/**
*

  • @author Administrator

/
public interface TelephoneDao {
int A = 123; // 公有的静态的。
/
*
* 方法描述: 初始化电话本信息 ,读取文件的过程。
* @param path 处理的文件路径
* @return
*/
List init(String path);

/**
 * 添加一行电话记录(姓名,年龄,电话,地址)
 * @param 电话对象
 * @Return 1 成功  0 失败  -1 已经有此人记录 
 */
int addTel(Telephone tel);

/**
 * 方法描述 :根据名删除
 * @param 用户名
 * @return fasle 失败   true 成功
 */
boolean delTel(String name);
boolean delTel(Telephone name);

/**
 * 方法描述:根据名称修改信息, (名称不可以修改;只能修改 年龄,号码,地址)。
 * @parma 电话对象
 * @return false 失败  true 成功
 * 
 */
boolean updateTel(Telephone tel);

/**
 * 根据名称查询,获取电话位置索引。
 * @param 姓名
 * @return 位置   int   -1 
 */
int getIndexByName(String name);

/**
 * 保存文件
 * @param path
 */
void save(String path);

/**
 * 获取容器方法
 * @return
 */
List<Telephone> getT();

}

package com.hpe.dao.impl;

import java.io.BufferedReader;
import java.io.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.hpe.bean.Telephone;
import com.hpe.dao.TelephoneDao;

/**

  • 电话本底层实现类
  • @author Administrator

*/
public class TelephoneDaoImpl implements TelephoneDao{
// 选择一个临时容器
public static List telList = new ArrayList();

@Override
public List<Telephone> init(String path) {
	// io操作 读取telephone.txt,把每一行转化成 Telephone对象,再放到telList容器。
	List<Telephone> list =new ArrayList<Telephone>();
	BufferedReader br = null;
	try {
		// 针对当前工程的 绝对路径   “D:\1-workspace\Demo15_1”
		File file = new File("telephone.txt");
		if(!file.exists()) {
			file.createNewFile();
		}
		
		br = new BufferedReader(new FileReader(file));
		String line = null;
		while((line = br.readLine()) != null) {
			// name,age,phone,addr
			Telephone tmp = getTelBean(line);
			list.add(tmp);
		};
	
	}catch (Exception e) {
		e.printStackTrace();
	}finally {
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	telList = list;  // 为了提升效率的 小处理。
	return list;
}

/*
 * 把字符串对象转化成 Telepthon对象。
 */
private Telephone getTelBean(String line){
	String[] strs = line.split(",");
	Telephone tmp = new Telephone();
	tmp.setName(strs[0]);
	tmp.setAge(Integer.parseInt(strs[1]));
	tmp.setPhone(strs[2]);
	tmp.setAddr(strs[3]);
	return tmp;
}
private String  getStrByTel(Telephone tel) {		
	return tel.getName() + "," +
			tel.getAge() + "," +
			tel.getPhone() + "," +
			tel.getAddr();
}


//1 成功  0 失败  -1 已经有此人记录 
@Override
public int addTel(Telephone tel) {
	// telList 容器,
	// 先判断 是否存在tel  tel中名称判断
	// 如果存在用户
	if(getIndexByName(tel.getName()) >= 0 ) {
		return -1;
	}
	
	// telList容器添加
	boolean fl = telList.add(tel);
	
	return fl == true ? 1 : 0;
}

@Override
public boolean delTel(String name) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean delTel(Telephone name) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean updateTel(Telephone tel) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public int getIndexByName(String name) {
	for(int i = 0;i < telList.size();i ++) {
		Telephone tel = telList.get(i);
		if(tel.getName().equals(name)){
			// 下标。
			return i;
		}
	}
	return  -1;
}

@Override
public void save(String path) {
	
	BufferedWriter br = null;
	try {
		br = new BufferedWriter(new FileWriter(new File(path)));
		// 把当前小容器telList 里面的元素记录到文件。
		for (Telephone telephone : telList) {
			// 全替换记录。
			br.write(getStrByTel(telephone));	
			br.newLine();
			br.flush();
		}
		
	}catch (Exception e) {
		e.printStackTrace();
	}finally {
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

@Override
public List getT() {
	return telList;
}

}

package com.hpe.server;

import java.util.List;

import com.hpe.bean.Telephone;

/**

  • 电话本业务接口类
  • @author Administrator

*/
public interface TelServer {

void add(Telephone tel);

void del(String name);

void update(Telephone tel);

/**
 * 根据名称查询电话信息
 * @param name
 * @return
 */
Telephone get(String name);

/**
 * 获取所有对电话信息
 * @return
 */
List<Telephone>  getList();

/**
 * 保存记事本
 */
void save(String path);

List<Telephone> init(String path);

List getT();

}
五.
package com.hpe.server.impl;

import java.util.ArrayList;
import java.util.List;

import com.hpe.bean.Telephone;
import com.hpe.dao.TelephoneDao;
import com.hpe.dao.impl.TelephoneDaoImpl;
import com.hpe.server.TelServer;

/**

  • 电话本 业务实现类
  • @author Administrator

*/
public class TelServerImpl implements TelServer{

TelephoneDao telDao = new TelephoneDaoImpl();

// 非接口编程思想
//TelephoneDaoImpl telDaoImpl = new TelephoneDaoImpl();

@Override
public void add(Telephone tel) {
	telDao.addTel(tel);		
}

@Override
public void del(String name) {
	// TODO Auto-generated method stub
	telDao.delTel(name);
}

@Override
public void update(Telephone tel) {
	// TODO Auto-generated method stub
	telDao.updateTel(tel);
}

@Override
public Telephone get(String name) {
	// TODO Auto-generated method stub
	 int i = telDao.getIndexByName(name);
	 return (Telephone)telDao.getT().get(i);
}

@Override
public List<Telephone> getList() {
	return telDao.getT();
}

@Override
public void save(String path) {
	// TODO Auto-generated method stub
	telDao.save(path);
}

@Override
public List<Telephone> init(String path) {
	return telDao.init(path);
}

@Override
public List getT() {
	return telDao.getT();
}

}
六.
package com.hpe.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

/**

  • 装饰者模式 ()
  • 1、节点流 outputStream inputStream FileOutputStream FileInputStream
  • 2\ 过滤流(缓冲流) BufferedInputStram BufferedoutputStream
  •           字符BufferedReader BudderedWriter
    
  • 操作步骤:
  • 1、创建文件对象(File类型)
  • 2、创建节点流 (文本 =字符流; 视频、音频=字节流;) 通过流管道实现数据传输。
  • 3、封装过滤流。 节点流作为参数传到其他流的构造函数中。 一个节点流可以封装到多个过滤流。
  •      缓存流。
    
  • 4、读 、写数据。 read write ; readLine
  • 5、关闭流 ,通常关闭外层流,内曾流会自动关闭。
  • @author Administrator

*/
public class Demo1 {

@Test
public void testReaderOrWriter() {
	// 文本的复制
	File file1 = new File("c:\\hello.txt");
	File file2 = new File ("c:\\hello2.txt");
	
	BufferedReader br = null;
	BufferedWriter wr = null;
	// FileReader   FileWrie 字符流处理
	try {
		FileReader reader = new FileReader(file1);//  往内存写
		FileWriter wriete = new FileWriter(file2);
		
		// 缓存流
		br = new BufferedReader(reader);
		wr = new BufferedWriter(wriete);
		
		String str = null;
		while( (str = br.readLine()) != null) {
			System.out.println(str);
			wr.write(str);
			wr.newLine();
			wr.flush();  //  刷新
		};
				
		
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

}
七.
package com.hpe.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**

  • RandomAccessFile 针对文本编辑,提供更强大的支持。
  • 选择读取的行,选择写入的行。选择打开方式(只读,可写)
  • @author Administrator

*/
public class Demo2 {
public static void main(String[] args) {
File file1 = new File(“c:\hello.txt”);
// File file2 = new File(“c:\hello3.txt”);
RandomAccessFile raf = null;
try {
// 处理流 (实现读写)
raf = new RandomAccessFile(file1,“rw”);
long l = raf.getFilePointer(); // 0
System.out.println(“首次文件光标:” + l);

		// 指定光标位置(指针)
		raf.seek(2);
		System.out.println("移动:" + raf.getFilePointer());
		// 读取 指针 2的文件数据
		byte[] b = new byte[1024];
		
		int i = 0;
		while((i = raf.read(b))  != -1) {
			System.out.println(new String(b,0,i));
		};
		
		
		raf.seek(2);
		
		
		System.out.println("移动:" + raf.getFilePointer());
		
		// 光标到最后一行
		raf.seek(raf.length());
		// 把文件打开方式修改为 new RandomAccessFile(Object,"rw") 可写。
		raf.writeChars("\n \t");
		raf.writeLong(234242345L);
		raf.writeChars("124141515616");
		
		
		
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

}
八.
package com.hpe.ui;

import java.util.List;
import java.util.Scanner;

import com.hpe.bean.Telephone;
import com.hpe.server.impl.TelServerImpl;

public class TelMain {

static TelServerImpl telServer = new TelServerImpl();

static List<Telephone> list=null;

public static void main(String[] args) {

	Scanner s = new Scanner(System.in);
	System.out.println("启动成功。。。。。。");
	telServer.init("telephone.txt");
	
	list = telServer.getT();
	System.out.println("初始化成功。。。");
	System.out.println("0 展示所有信息       1 添加     2  删除   3  查询    4 保存   5  退出");
	while(true) {
		System.out.println("请输入:");
		String next = s.nextLine();
		if(next.equals("0")) {
			show(s,list);
			continue;
		}
		//  用户选择了1   添加电话记录
		if(next.equals("1")) {
			add(s,list);
			continue;
		}
		if(next.equals("4")) {
			telServer.save("telephone.txt");
			continue;
		}
		if(next.equals("5")) {
			break;
		}
		//tel.add();
		//2 、删除
		//tel.del();
	}
}

public static void show(Scanner s ,List<Telephone> list) {
	for (Telephone t : list) {
		System.out.println(t);
	}
}
public static void add(Scanner s,List<Telephone> list) {
	System.out.println("输入添加的信息:格式【姓名,年龄,号码,地址】");
	String str = s.nextLine();
	String[] st = str.split(",");
	Telephone temptel = new Telephone();
	temptel.setAddr(st[3]);
	telServer.add(temptel);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值