文件模式JAVASE----16----IO流_3
在写这篇文章之前,xxx已经写过了几篇关于改文件模式主题的文章,想要了解的朋友可以去翻一下之前的文章
一,操纵象对
package IO2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectStreamDemo {
/**
* @param args
*/
public static void main (String[] args) throws Exception{
//writeObj();
readObj();
}
public static void readObj() throws Exception {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("obj.txt"));
Person p=(Person)ois.readObject();
System.out.println(p);
ois.close();
}
public static void writeObj() throws Exception {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person("lisi",339,"kr"));
oos.close();
}
}
package IO2;
import java.io.Serializable;
//序列化已现实象对的持久化
public class Person implements Serializable{
private String name;
transient int age; //此属性不会被序列化
static String country="cn"; //静态也不能被序列化
public Person(String name, int age, String country) {
super();
this.name = name;
this.age = age;
this.country=country;
}
@Override
public String toString() {
return name+":"+age+":"+country;
}
}
二,IO包中的其他类
1,管道流:
package IO2;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStreamDemo {
/**
* @param args
* @throws IOException
*/
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();
}
}
class Read implements Runnable
{
private PipedInputStream in;
Read( PipedInputStream in)
{
this.in=in;
}
@Override
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) {
e.printStackTrace();
}
}
}
class Write implements Runnable
{
private PipedOutputStream out;
Write( PipedOutputStream out)
{
this.out=out;
}
@Override
public void run() {
try {
System.out.println("开始入写数据,等待5秒后");
Thread.sleep(5000);
out.write("piped come ".getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2,RandomAccessFile
该类不算是IO系体的子类,二是直接继承自Object。
但是他是IO包中成员,因为它具有读和写功能。外部封装了一个数组,而且通过针指对数组的元素停止操纵。可以通过getFilePointer取获针指置位,共事可以根据seek转变针指置位。
其实成完写读的道理就是外部封装了字节输入流和输出流
从构造函数可以看出,该类只能操纵文件。
而且操纵文件还有模式。只读 r, 写读 rw 等,
如果模式为只读r,不会建创文件,去回读取一个已存在的文件,如果不存在,则会出现异常。
如果模式为rw,操纵的文件不存在会动自建创,如果 存在则不会覆盖。
package IO2;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//writeFile();
readFile();
}
public static void writeFile() throws IOException
{
RandomAccessFile raf=new RandomAccessFile("ran.txt", "rw");
raf.write("李四".getBytes());
raf.writeInt(97);
raf.write("王五".getBytes());
raf.writeInt(99);
raf.close();
}
public static void readFile() throws IOException
{
//r: 只读
RandomAccessFile raf=new RandomAccessFile("ran.txt","r");
//调整象对中的针指
raf.seek(8*1);
//跳过指定的字节数
//raf.skipBytes(8);
byte[] buf=new byte[4];
raf.read(buf);
String name=new String (buf);
int age=raf.readInt(); //一次读4个字节
System.out.println("name="+name);
System.out.println("age="+age);
raf.close();
}
}
3,操纵基本数据类型的流象对
DataInputStream 与DataOutputStream
可以用于操纵基本数据类型的流象对
package IO2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class DataStreamDemo {
/*
*
*/
public static void main(String[] args) throws Exception {
readdata();
//writedata();
}
public static void writedata() throws Exception {
DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt"));
//入写13个字节
dos.writeInt(234);
dos.writeBoolean(true);
dos.writeDouble(2343.234);
dos.close();
}
public static void readdata() throws Exception {
DataInputStream dis=new DataInputStream(new FileInputStream("data.txt"));
int num=dis.readInt();
boolean b=dis.readBoolean();
double d=dis.readDouble();
System.out.print(num+","+b+","+d);
dis.close();
}
}
4,操纵字节数组的流象对
ByteArrayInputStream: 在构造的时候,需要接受数据源,而且数据源是一个字节数组
ByteArrayOutputStream: 在构造的时候不用定义数据目的,因为该象对中已经封装了可变长度的字节数组,这就是数据目的。
因为这2个流象对都操纵数组,并没有使用系统资源,所以不用停止close关闭。
在写流操纵规律时,
源设备:
键盘:System.in 硬盘: FileStream 内存 ArrayStream
目的设备:
控制台:System.out 硬盘 FileStream 内存 ArrayStream
用流的写读思想来操纵数组
package IO2;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
public class ByteArrayStream {
/**
* @param args
*/
public static void main(String[] args) {
ByteArrayInputStream bis=new ByteArrayInputStream("DFSDFSF".getBytes());
ByteArrayOutputStream bos=new ByteArrayOutputStream();
//System.out.println(bos.size());
int by=0;
while((by=bis.read())!=-1)
{
bos.write(by);
}
System.out.println(bos.toString());
//bos.writeTo(new FileOutputStream("a.txt"));
}
}
文章结束给大家分享下程序员的一些笑话语录:
人脑与电脑的相同点和不同点,人脑会记忆数字,电脑也会记忆数字;人脑会记忆程序,电脑也会记忆程序,但是人脑具有感知能力,这种能力电脑无法模仿,人的记忆会影响到人做任何事情,但是电脑只有程序软件。比尔还表示,人脑与电脑之间最重要的一个差别就是潜意识。对于人脑存储记忆的特别之处,比尔表示,人脑并不大,但是人脑重要的功能是联络,人脑会把同样的记忆存储在不同的地方,因此记忆读取的速度就不相同,而这种速度取决于使用的频率和知识的重要性。人脑的记忆存储能力会随着年龄增长而退化,同时记忆的质量也会随着年龄退化。经典语录网

浙公网安备 33010602011771号