标签:pipedoutputstream 程序员 android 应用
——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——
终于把io流全部看完,写完了。
1:对象的序列化
ObjectInputStream ObjectOutputStream 对象的持久化储存
2:管道流的应用:PipedOutputStream PipedInputStream connect()方法
相关联到的技术 多线程(Runnable)
3:随机访问文件流:RandomAccessFile(“”)
“r” 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
“rw” 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。
“rws” 打开以便读取和写入,对于 “rw”,还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。
“rwd” 打开以便读取和写入,对于 “rw”,还要求对文件内容的每个更新都同步写入到底层存储设备。
4:数据流:DataInputStream DataOutputStream 操作数据的流
5:编码之转换流的应用:
5.1:编码:把字符串转成byte数组 string.getBytrs();
5.2:解码:把byte数组转成字符串 new String(byte[]by);
6:关于对象的排序
6.1:强行反转排序
Comparator com= Collections.reverseOrder();
树杈集合
Set set=new TreeSet(com);
package com.theima.www;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import com.itheima.entity.Employee;
/**
* @author myluo
*
*/
public class IOObject {
public static void main(String[] args) throws IOException {
//对象的写入
// writeObj();
//对象的读取
// readobj();
//管道流的应用
// pipedMethod();
//随机访问文件的读取和写入
//randomMethod();
//操作基本数据类型的流
// dataMethod();
//io中内存为源的一些操作
//arrayMethod();
//转换流中的编码 gb2312 gbk utf-8 如果中文件乱码的结果是? 那么一定是 gbk编的码 用utf-8解的码
//如果是中文件乱码 , 那麽一定是 utf-8编得码 gbk解的码
// unicodeMethod();
//编码:就是把字符串变成byte数组 解码: 把byte数组变成字符串
// str.getBytes(); String str=new String(bt,0,len);
//unicodeGBK();
//编码之联通
//unicodeUTF();
//键盘录入学生姓名,语数外成绩,排完序写入文件保存
studentMethod();
}
//键盘录入学生姓名,语数外成绩,排完序写入文件保存
private static void studentMethod() throws IOException {
// TODO Auto-generated method stub
//强行反转排序(比较器)
Comparator<Student> com=Collections.reverseOrder();
//获取排完序的集合
Set<Student> set= utilMethod(com);
//把集合中的对象写入文件中
writeSet(set);
}
//把集合中的对象写入文件中
private static void writeSet(Set<Student> set) throws IOException {
// TODO Auto-generated method stub
BufferedWriter bw=new BufferedWriter(new FileWriter(new File("e:/abc/6.txt")));
for(Student ss:set){
bw.write(ss.toString());
bw.newLine();
bw.flush();
}
}
//获取排完序的集合
private static Set<Student> utilMethod(Comparator<Student>com) throws IOException {
// TODO Auto-generated method stub
//键盘录入
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//保存到集合(内存中),Set <Student> set=new TreeSet<Student>();
//反向比较器 Set <Student> set=new TreeSet<Student>(com); 带上比较器的集合
Set <Student> set=new TreeSet<Student>(com);
String len=null;
while((len=br.readLine())!=null){
if("over".equals(len)){
break;
}
String [] str= len.split(",");
//把录入的数据封装到对象中
Student stu=new Student(str[0],Integer.parseInt( str[1]), Integer.parseInt( str[1]), Integer.parseInt( str[1]));
//把对象添加到集合
set.add(stu);
}
if(br!=null){
br.close();
}
return set;
}
//解释“联通”两字乱码问题,记事本默认gbk编的码,但是联通两字符合utf-8的编码格式,所以没查到
/* 11000001
10101010
11001101
10101000*/
private static void unicodeUTF() {
// TODO Auto-generated method stub
String str="联通";
//编码
try {
byte [] by=str.getBytes("gbk");
System.out.println(Arrays.toString(by));
System.out.println(new String(by,"gbk"));
for(int i=0;i<by.length;i++){
System.out.println(Integer.toBinaryString(by[i]&255));
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//编码与解码问题
private static void unicodeGBK() {
// TODO Auto-generated method stub
String str="好人";
//开始编码,默认是gbk编码
try {
byte [] bt=str.getBytes("utf-8");
//打印编码
System.out.println(Arrays.toString(bt));
//开始解码
String str2=new String(bt,"iso8859-1");
System.out.println(str2);
//如果解码错误,再编一次,在解码
byte [] bt2=str2.getBytes("iso8859-1");
System.out.println(Arrays.toString(bt2));
//对bt2进行解码
String str3=new String(bt2,"utf-8");
System.out.println(str3);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//转换流之编码问题
private static void unicodeMethod() {
// TODO Auto-generated method stub
// outMethod();
inMethod();
}
//转换流读取,gbk是两个字节一个字,utf-8是三个字节一个字
private static void inMethod() {
// TODO Auto-generated method stub
InputStreamReader isr=null;
try {
isr=new InputStreamReader(new FileInputStream("e:/abc/4.4.txt"),"gbk");
char [] bt=new char[1024];
try {
int len=isr.read(bt);
System.out.println(new String(bt,0,len));
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//转换流写入
private static void outMethod() {
// TODO Auto-generated method stub
OutputStreamWriter osw=null;
try {
try {
osw=new OutputStreamWriter(new FileOutputStream("e:/abc/4.4.txt"),"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
osw.write("你好");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//操作字节数组的流 ByteArrayInputStream ByteArrayOutputStream ,此流不需要close
private static void arrayMethod() {
// TODO Auto-generated method stub
//字节数组读取流
ByteArrayInputStream bals=new ByteArrayInputStream("中国人".getBytes());
//字节数组写入流
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len=0;
while((len=bals.read())!=-1){
baos.write(len);
}
System.out.println("size=="+baos.size());
System.out.println("baos=="+baos.toString());
}
//DataInputStream DataOutputStream()
private static void dataMethod() {
// TODO Auto-generated method stub
// wMethod();
rMethod();
}
private static void rMethod() {
// TODO Auto-generated method stub
DataInputStream dls=null;
try {
dls=new DataInputStream(new FileInputStream("e:/abc/3.txt"));
try {
int num=dls.readInt();
double dou=dls.readDouble();
boolean boo=dls.readBoolean();
byte [] bt=new byte[1024];
int len =dls.read(bt);
String str=new String(bt,0,len);
System.out.println(num+":"+dou+":"+boo+":"+str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void wMethod() {
// TODO Auto-generated method stub
DataOutputStream dos=null;
try {
dos=new DataOutputStream(new FileOutputStream("e:/abc/3.txt"));
try {
dos.writeInt(100);
dos.writeDouble(100.235);
dos.writeBoolean(false);
dos.write("翟哦".getBytes());
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//随机访问文件的读取和写入
private static void randomMethod() {
// TODO Auto-generated method stub
readMethod();
// writeMethod();
}
//随机访问文件的写入
private static void writeMethod() {
// TODO Auto-generated method stub
/**
* 前一参数:File 对象
* 后一参数:模式
* 值
含意
"r" 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
"rw" 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。
"rws" 打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。
"rwd" 打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。
*
*
*/
RandomAccessFile raf=null;
try {
raf=new RandomAccessFile("e:/abc/1.txt","rw");
try {
raf.write("你好 ".getBytes());
raf.writeInt(18);
raf.write("我好 ".getBytes());
raf.writeInt(20);
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//随机访问文件的读取
private static void readMethod() {
// TODO Auto-generated method stub
RandomAccessFile raf=null;
try {
raf=new RandomAccessFile(new File("e:/abc/1.txt"),"r");
try {
raf.seek(8*1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte [] bt=new byte[4];
try {
int len=raf.read(bt);
int num=raf.readInt();
System.out.println("值"+new String(bt,0,len));
System.out.println("age="+num);
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//管道流的应用
private static void pipedMethod() {
// TODO Auto-generated method stub
//管道流对象
PipedInputStream pls=new PipedInputStream();
PipedOutputStream pos=new PipedOutputStream();
//连接管道流
try {
pls.connect(pos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Read ren=new Read(pls);
Write wri=new Write(pos);
//开启线程
new Thread(ren).start();
new Thread(wri).start();
}
// 读取持久化对象 ObjectInputStream 与 ObjectOutputStream 都是成对存在的
private static void readobj() {
// TODO Auto-generated method stub
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream("e:/abc/xx.txt"));
try {
Employee emp=(Employee)ois.readObject();
System.out.println(emp);
ois.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//对象的持久化存储
private static void writeObj() {
// TODO Auto-generated method stub
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream("e:/abc/xx.txt"));
oos.writeObject(new Employee("jack", "123"));
oos.close();
System.out.println(oos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//多线程与io相结合
class Read implements Runnable{
//管道读取流
private PipedInputStream pls;
public Read(PipedInputStream pls) {
super();
this.pls = pls;
}
//必修实现run方法
public void run() {
// TODO Auto-generated method stub
byte [] bt=new byte[1024];
int len=-1;
try {
len=pls.read(bt);
System.out.println("读取到:"+new String(bt,0,len));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(pls!=null){
try {
pls.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Write implements Runnable{
//管道写入流
private PipedOutputStream pos;
public Write(PipedOutputStream pos) {
super();
this.pos = pos;
}
public void run() {
// TODO Auto-generated method stub
try {
pos.write("我的是管道流写入的".getBytes());
pos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//学生类
class Student implements Comparable<Student>
{
private String name; //名字
private int ma,cn,en; //语数外
private int sum; //总分
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMa() {
return ma;
}
public void setMa(int ma) {
this.ma = ma;
}
public int getCn() {
return cn;
}
public void setCn(int cn) {
this.cn = cn;
}
public int getEn() {
return en;
}
public void setEn(int en) {
this.en = en;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public String getName(String name){
return this.name=name;
}
public Student(String name, int ma, int cn, int en) {
super();
this.name = name;
this.ma = ma;
this.cn = cn;
this.en = en;
this.sum = ma+cn+en;
}
@Override
public String toString() {
return "Student [name=" + name + ", ma=" + ma + ", cn=" + cn + ", en="
+ en + ", sum=" + sum + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sum;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sum != other.sum)
return false;
return true;
}
//比较的方法
public int compareTo(Student arg0) {
// TODO Auto-generated method stub
//用对象总分和名字比较,如果总分相等
int num= new Integer(this.sum).compareTo(new Integer(arg0.sum));
//如果
if(num==0){
return this.name.compareTo(arg0.name);
}
return num;
}
}
我命由我,不由天。
标签:pipedoutputstream 程序员 android 应用
原文地址:http://blog.csdn.net/myadmini/article/details/45920697