---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
//21-1
import java.io.*;
class ObjectStreamDemo
{
public static void writeObj() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.object"));
oos.writeObject(new Person("person1",19,"kr"));
oos.close();
}
public static void readObj() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.object"));
Person p = (Person)ois.readObject();
System.out.println(p);
ois.close();
}
public static void main(String[] args)throws Exception
{
writeObj();
readObj();
System.out.println("Hello World!");
}
}
//import java.io.*;
class Person implements Serializable//用于类的标记
{
public static final long serialVersionUID = 42L;//用于类的标记。
String name;
transient int age;//transient 关键字,用于变量不序列化。
static String country = "cn"; //static 的数据不能序列化。
Person(String name, int age,String country){
this.name = name;
this.age = age;
this.country = country;
}
public String toString(){
return name+":"+age+":"+country;
}
}
//21-2
/*
PipedInputStream: 从管道中读取
PipedOutputStream: 写入管道
*/
import java.io.*;
class Read implements Runnable
{
private PipedInputStream in;
Read(PipedInputStream in){
this.in = in;
}
public void run (){
try
{
byte[] buf = new byte[1024];
System.out.println("读取前");
int len = in.read(buf);
System.out.println("读取后");
String s = new String(buf,0,len);
System.out.println(s);
in.close();
}
catch (IOException e)
{
throw new RuntimeException("管道读取流失败");
}
}
}
class Write implements Runnable
{
private PipedOutputStream out;
Write(PipedOutputStream out){
this.out = out;
}
public void run(){
try
{
System.out.println("等待3秒");
Thread.sleep(3000);//static 方法
out.write("piped".getBytes());//
out.close();
}
catch (Exception e)
{
throw new RuntimeException("管道读取失败");
}
}
}
class PipedStreamDemo
{
public static void main(String[] args) throws IOException
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
Read r = new Read(in);
Write w = new Write(out);
new Thread(r).start();//
new Thread(w).start();
System.out.println("Hello World!");
}
}
/*21-3
RandomAccessFile:
该类不是IO体系的子类。
而是直接继承自Object。
但是它是IO包中的成员,因为它具备读和写功能。
内部封装了一个数组,而且通过指针对数组的元素进行操作。
可以通过getFilePointer获取指针位置。
同时通过seek改变指针的位置。
其实完成读写的原理就是内部封装了字节输入流和输出流。
通过构造方法可以看出,该类只能操作文件。
而且访问文件还有模式。"r","rw","rws","rwd"
如果模式为只读,不会创建文件,会去读取一个已存在文件,如果该文件,如果该文件不存在,
则会出现异常。如果模式为"rw",操作的文件不存在,会自动创建,如果存在不会覆盖。
该类可以实现多线程下载
只能操作文件
而且该对象的构造函数要操作的文件不存在,会自动创建,
且不覆盖已存在的文件。
*/
import java.io.*;
class RandomAccessFileDemo
{
public static void main(String[] args) throws IOException
{
writeFile();
readFile();
System.out.println("Hello World!");
}
public static void readFile() throws IOException{
RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
raf.seek(8);
//raf.skipBytes(8);
byte[] b = new byte[4];
raf.read(b);
String name = new String(b);
int age = raf.readInt();
System.out.println("name:" +name);
System.out.println("age:" +age);
}
public static void writeFile() throws IOException{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
raf.write("人名".getBytes());
//raf.write(97);
//raf.write("97".getBytes());
raf.writeInt(97);
raf.write("随机".getBytes());
raf.writeInt(255);
raf.close();
}
}
/*21-4
可以用于操作基本数据类型
*/
import java.io.*;
class DataStreamDemo
{
public static void main(String[] args) throws IOException
{
readData();
System.out.println("Hello World!");
}
public static void writeUTFDemo()throws IOException{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("utfdata.txt"));
dos.writeUTF("你好");//修改的utf-8编码
dos.close();
}
public static void readUTFDemo()throws IOException{
DataInputStream dis = new DataInputStream(new FileInputStream("utfdata.txt"));
String s = dis.readUTF();//
System.out.println(s);
dis.close();
}
public static void readData() throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
boolean b = dis.readBoolean();
double d = dis.readDouble();
System.out.println(num);
System.out.println(b);
System.out.println(d);
}
public static void writeData() throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(255);
dos.writeBoolean(true);
dos.writeDouble(9999.555);
dos.close();
}
}
/*21-5
关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,
而不会产生任何 IOException。
ByteArrayInputStream: 在构造的时候,需要接收数据源,而且数据是一个字节数组。
ByteArrayOutputStream: 在构造的时候,不需要接收目的,
因为该对象中已经封装了可变长度的内部数组。这就是数组目的地。
因为这两个流对象都操作数组,并没有使用系统资源,
所以不用 close() 关闭。
用流的读写思想来操作数组
*/
//writeTo(OutputStream o)
import java.io.*;
class ByteArrayStream
{
public static void main(String[] args)
{
ByteArrayInputStream bis = new ByteArrayInputStream("abcdefg".getBytes());//
ByteArrayOutputStream bos = new ByteArrayOutputStream();//
int len = 0;
while ((len = bis.read())!=-1)
{
bos.write(len);
}
System.out.println(bos.size());//
}
}
/*
设备:键盘System.in/System.out 硬盘FileStream 内存ArrayStream
*/
//21-6
import java.io.*;
class EncodeStream
{
public static void main(String[] args) throws IOException
{
readText();
System.out.println("Hello World!");
}
public static void readText() throws IOException{
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"), "utf-8");//"gbk"
char[] buf = new char[10];
int len = isr.read(buf);
String str = new String(buf,0,len);//
System.out.println(str);
isr.close();
}
public static void writeText() throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"utf-8");
osw.write("你好");
osw.close();
}
}
/*21-7
编码:字符串变字节
解码:字节变字符串
String -->byte[]; str.getBytes(charsetName);
byte[] -->String; new String(byte[],charsetName);
Arrays.toString(b1)
*/
import java.util.*;
class EncodeDemo
{
public static void main(String[] args) throws Exception
{
String s = "你好";
byte[] b1 = s.getBytes("utf-8");
String s1 = new String (b1,"iso8859-1");
byte[] b2 = s1.getBytes("iso8859-1");
String s2 = new String (b2,"utf-8");
System.out.println(s1);
System.out.println(Arrays.toString(b1));
System.out.println(s2);
}
}
/*21-9
有5个学生,每个学生有3门课成绩:
从键盘输入以上数据(包括姓名,三门课成绩),输入的格式:如: zhangsan,30,40,60计算总成绩。
并把学生的信息和计算出来的总分数由高到底的顺序保存到"stu.txt"中。
*/
import java.io.*;
import java.util.*;
class StudentInfoTool
{
public static void main(String[] args) throws IOException
{
Comparator<Student> cmp = Collections.reverseOrder();//Collections
Set<Student> stus = StudentInfoTool.getStudents(cmp);
StudentInfoTool.write2File(stus);
System.out.println("Hello World!");
}
public static Set<Student> getStudents()throws IOException{
return getStudents(null);
}
public static Set<Student> getStudents(Comparator<Student> cmp)throws IOException{
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
String line = null;
Set<Student> stus = null;
if (null==cmp)
stus = new TreeSet<Student>();
else
stus = new TreeSet<Student>(cmp);
while ((line=bufr.readLine())!=null)//
{
if("over".equals(line))
break;
String[] info = line.split(",");
Student stu = new Student(info[0],
Integer.parseInt(info[1]),
Integer.parseInt(info[2]),
Integer.parseInt(info[3]));
stus.add(stu);
}
bufr.close();
return stus;
}
public static void write2File(Set<Student> stus)throws IOException{
BufferedWriter bufw = new BufferedWriter(new FileWriter("stuinfo.txt"));
for(Student stu: stus){
bufw.write(stu.toString()+"\t");
bufw.write(stu.getSum()+"");
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}
class Student implements Comparable<Student>//Comparable 接口
{
private String name;
private int ma,cn,en;
private int sum;
Student(String name,int ma,int cn,int en){
this.name = name;
this.ma = ma;
this.cn = cn;
this.en = en;
sum = ma + cn +en;
}
public int compareTo(Student s){
int num = new Integer(this.sum).compareTo(new Integer(s.sum));
if(0==num)
return this.name.compareTo(s.name);
return num;
}
public String getName(){
return name;
}
public int getSum(){
return sum;
}
public int hashCode(){//?????????????
return name.hashCode() + sum*78;
}
public boolean equals(Object obj){//?????????????
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student)obj;
return this.name.equals(s.name)&&this.sum==s.sum;
}
public String toString(){
return "student{"+name+","+ma+","+cn+","+en+"}";
}
}
/*
必须自己重新写一次
*/
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------