// 负责显示菜单
class Menu{
public Menu(){
while(true){
this.showMenu() ;
}
}
public void showMenu(){
System.out.println(" ================== 菜单程序 ================== ") ;
System.out.println("[1]、输入数据") ;
System.out.println("[2]、查看数据") ;
System.out.println("[3]、修改数据") ;
System.out.println("[4]、退出系统") ;
InputData input = new InputData() ;
Operate o = new Operate() ;
switch(input.getInt("请选择:")){
case 1:
{
o.add() ;
break ;
}
case 2:
{
o.show() ;
break ;
}
case 3:
{
o.update() ;
break ;
}
case 4:
{
System.out.println("byebye!") ;
System.exit(1) ;
break ;
}
case 5:
{
System.out.println("无效的选项,请重新选择!") ;
break ;
}
}
}
}
public class IODemo33{
public static void main(String args[]) throws Exception{
new Menu() ;
}
}
3.7、字符编码(了解)
在各个平台上都是有语言的支持的,那么一般对于文字来说有以下几种常见的编码方式:
GBK:包含了简体中文和繁体中文的编码集
GB2312:只包含了简体中文
ISO8859-1:是一个国际的通用编码
例如:取得本机的编码方式
public class IODemo34{
public static void main(String args[]) throws Exception{
System.getProperties().list(System.out) ;
}
}
显示结果:
任何编码都不写的情况下,肯定是GBK码,那么如果现在要保存一个文件的内容,但是文件的内容使用了ISO8859-1编码,问能正确解码吗?不可以
例如:以下代码对输出的内容进行重新编码
import java.io.* ;
public class IODemo35{
public static void main(String args[]) throws Exception{
OutputStream out = new FileOutputStream(new File("e:\\test.txt")) ;
out.write("世界,你好".getBytes("ISO8859-1")) ;
out.close() ;
}
}
因为程序本身与本地环境中的编码方式不一样,所以无法正确的进行解码。
程序造成乱码的根本原因:
编码不一致所造成
3.8、对象序列化(重点)
对象序列化:将一个对象进行IO操作,输入/输出。
如果要想实现对象序列化,则对象所在的类必须实现一个序列化接口 —— Serializable,但是此接口中没有任何的定义,所以此接口只是一个标识接口。
常见的标识接口:
Cloneable,表示可以被克隆
Serializable:标识可以被序列化
那么下面的代码以向文件中保存为例。
如果现在要想实现对象的输出和输入,则必须使用以下两个类:
ObjectOutputStream:对象输出流
构造方法:public ObjectOutputStream(OutputStream out) throws IOException
写对象:public final void writeObject(Object obj) throws IOException
ObjectInputStream:对象输入流,反序列化
构造方法:public ObjectInputStream(InputStream in) throws IOException
读对象:public final Object readObject() throws IOException,ClassNotFoundException
说明:
对象可以向任何地方保存。
例如:向文件之中写入一个对象
import java.io.* ;
class Person implements Serializable{
private String name;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
}
public class IODemo36{
public static void main(String args[]) throws Exception{
Person per = new Person("zhangsan",30) ;
ser(per) ;
}
public static void ser(Person per) throws Exception{
ObjectOutputStream oos = null ;
oos = new ObjectOutputStream(new FileOutputStream(new File("e:\\person.ser"))) ;
// 写对象
oos.writeObject(per) ;
oos.close() ;
}
}
例如:从文件之中读取出对象
import java.io.* ;
class Person implements Serializable{
private String name;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
}
public class IODemo37{
public static void main(String args[]) throws Exception{
Person per = new Person("zhangsan",30) ;
// ser(per) ;
System.out.println(dser()) ;
}
public static void ser(Person per) throws Exception{
ObjectOutputStream oos = null ;
oos = new ObjectOutputStream(new FileOutputStream(new File("e:\\person.ser"))) ;
// 写对象
oos.writeObject(per) ;
oos.close() ;
}
public static Person dser() throws Exception{
Person per = null ;
ObjectInputStream ois = null ;
ois = new ObjectInputStream(new FileInputStream(new File("e:\\person.ser"))) ;
per = (Person)ois.readObject() ;
ois.close() ;
return per ;
}
}
例如:现在不希望Person类中的name属性被序列化,那么次时对于不希望被序列化的属性就可以使用transient关键字声明。
import java.io.* ;
class Person implements Serializable{
private transient String name;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
}
public class IODemo37{
public static void main(String args[]) throws Exception{
Person per = new Person("zhangsan",30) ;
ser(per) ;
System.out.println(dser()) ;
}
public static void ser(Person per) throws Exception{
ObjectOutputStream oos = null ;
oos = new ObjectOutputStream(new FileOutputStream(new File("e:\\person.ser"))) ;
// 写对象
oos.writeObject(per) ;
oos.close() ;
}
public static Person dser() throws Exception{
Person per = null ;
ObjectInputStream ois = null ;
ois = new ObjectInputStream(new FileInputStream(new File("e:\\person.ser"))) ;
per = (Person)ois.readObject() ;
ois.close() ;
return per ;
}
}
4、总结
整个IO包中,实际上就五个类,一个接口、一个关键字
五个类:File、OutputStream、InputStream、Reader、Writer
一个接口:Serializable
一个关键字:transient
一切的操作以父类为准,但是里面有若干个子类很有用处:
OutputStream:
FileOutputStream:文件操作类
ByteArrayOutputStream:内存操作类
ObjectOutputStream:对象输出流
PrintStream:打印流
InputStream:
FileInputStream
ObjectInputStream
ByteArrayInputStream
Writer:
OutputStreamWriter:字符-字节的转换类
FileWriter
PrintWriter
Reader:
InputStreamReader
FileReader
BufferedReader
用户输入数据的标准格式
字符编码:GBK、GB2312、ISO8859-1