java IO之Print、Scanner和对象序列化

输出

java提供了字节打印流和字符打印流,以增强输出能力,这里分析字节输出流。

PrintStream

public PrintStream(File file)throws FileNotFoundEXCeption        通过file实例一个PrintStream对象

public PrintStream(OutputStream out)                                             类似于上

public PrintStream printf(String,objs...)                                            根据本地环境格式化输出(和C语言一样)

和C一样的输出方式

public class PrintStreamDemo02 {

	public static void main(String[] args) throws Exception {
		File file = new File("d:" + File.separator + "demo.txt");
		PrintStream out = new PrintStream(new FileOutputStream(file));
		String name = "李兴华";
		int age = 3;
		float score = 99.9f;
		char sex = 'M';
		out.printf("姓名:%s;年龄:%d;成绩:%5.2f;性别:%c。", name, age, score, sex);
		out.close();
	}

}


输入
引入Scannner以 强化java输入能力

方法:

public Scanner(InputStream source)                                                                         从指定的字节输入流中接收内容

public boolean hasNext(Pattern pattern)                                                                  判断输入的数据是否符合指定的正则标准

public boolean hasNext()                                                                                            判断有无输入内容

public boolean hasNextXxx()                                                                                      判断输入的是否为指定的数据类型

public String next()                                                                                                       接收内容

public String next(Pattern pattern)                                                                             接收内容进行正则验证   

public int nextXxx()                                                                                                       接收指定的输入类型

public Scanner use delimiter(String pattern)                                                            设置读取的分隔符

示例:

public class ScannerDemo02 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String str = null ;
		if (scan.hasNext()) {
			str = scan.next();
		}
		System.out.println("str = " + str);
	}
}

public class ScannerDemo03 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String str = null ;
		if (scan.hasNext("\\d{4}-\\d{2}-\\d{2}")) {
			str = scan.next();
		}
		System.out.println("str = " + str);
	}
}

public class ScannerDemo04 {

	public static void main(String[] args) throws Exception {
		File file = new File("D:" + File.separator + "demo.txt");
		Scanner scan = new Scanner(new FileInputStream(file));
		StringBuffer buf = new StringBuffer();
		scan.useDelimiter("\n") ;
		while (scan.hasNext()) {
			buf.append(scan.next()).append("\n");
		}
		System.out.println("str = " + buf);
	}
}

对象序列化
序列化操作的主要目的是为保证对象可以以二进制数据的方式传输,如果要想实现对象序列化对象所在的类必须实现java.io.Serializable接口。默认情况下一个对象的所有属性都会被序列化下来,也可以使用transient关键自定义不被序列化的属性
程序----ObjectOutputStream--------序列化对象---------ObjectInputStream---------程序
        序列化                                         反序列化


示例:

public class ObjectOutputStreamDemo {


	public static void main(String[] args) throws Exception {
		File file = new File("d:" + File.separator + "person.ser");
		ObjectOutputStream oos = null;
		oos = new ObjectOutputStream(new FileOutputStream(file));
		Person per = new Person("张三", 30);
		oos.writeObject(per) ;
		oos.close() ;
	}


}

public class ObjectInputStreamDemo {


	public static void main(String[] args) throws Exception {
		File file = new File("d:" + File.separator + "person.ser");
		ObjectInputStream ois = null;
		ois = new ObjectInputStream(new FileInputStream(file));
		Object obj = ois.readObject();
		Person per = (Person) obj;
		System.out.println(per);
	}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值