(1)可以操作基本类型的数据
(2)流对象名称
DataInputStream
DataOutputStream
import java.io.*;
public class DataStreamDemo {
public static void main(String[] args) throws IOException {
writer();
reader();
}
private static void writer() throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("F:\\test.txt"));
//写数据
dos.writeByte(13);
dos.writeShort(14);
dos.writeInt(520);
dos.writeLong(5200);
dos.writeFloat(13.14f);
dos.writeDouble(5.20);
dos.writeChar('a');
dos.writeBoolean(true);
dos.close();
}
private static void reader() throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("F:\\test.txt"));
Byte b = dis.readByte();
short s = dis.readShort();
int i = dis.readInt();
long l = dis.readLong();
float f = dis.readFloat();
Double d = dis.readDouble();
char c = dis.readChar();
boolean bb = dis.readBoolean();
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(c);
System.out.println(bb);
}
}
数据操作流写入到外部文件的数据,不是我们可读的,可以通过对应的流读入。
(1)有些时候我们操作完毕后,未必需要产生一个文件,就可以使用内存操作流。
(2)三种
A:ByteArrayInputStream,ByteArrayOutputStream
B:CharArrayReader,CharArrayWriter
C:StringReader,StringWriter
查看源码可见,不需要close
(1)字节打印流,字符打印流
(2)特点:
A:只操作目的地,不操作数据源
B:可以操作任意类型的数据
C:如果启用了自动刷新,在调用println()方法的时候,能够换行并刷新
D:可以直接操作文件
问题:哪些流可以直接操作文件呢?
看API,如果其构造方法能够同时接收File和String类型的参数,一般都是可以直接操作文件的
(3)复制文本文件
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("b.txt"),true);
String line = null;
while((line=br.readLine())!=null) {
pw.println(line);
}
pw.close();
br.close();
(1)System类下面有这样的两个字段
(2)三种键盘录入方式
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
(3)输出语句的原理和如何使用字符流输出数据
A:原理
System.out.println("helloworld");
PrintStream ps = System.out;
ps.println("helloworld");
B:把System.out用字符缓冲流包装一下使用
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
可以按照文件指针的位置写数据和读数据。
RandomAccessFile类不属于流,是Object类的子类
但是它融合了InputSream和OutputStream的功能
支持对文件的随机访问读取和写入
Public RandomAccessFeile(String name,String mode)
第一个参数是文件路径,第二个参数是操作文件的模式
模式有四种,我们最常用的一种叫做 “rw”
这种方式即可以写数据也可以读数据
(1)可以把对象写入文本文件或者在网络中传输
(2)如何实现序列化呢?
让被序列化的对象所属类实现序列化接口。
(java.io.Serializable)
该接口是一个标记接口。没有功能需要实现。
(3)注意问题:
把数据写到文件后,在去修改类会产生一个问题。
如何解决该问题呢?
在类文件中,自动给出一个固定的序列化id值。
而且,这样也可以解决黄色警告线问题
(4)如何让部分对象的成员变量不被序列化
使用transient关键字声明不需要序列化的成员变量
(1)是一个集合类,Hashtable的子类
(2)特有功能
//如果键值都是字符串的情况下只能使用特殊功能
//添加元素
public Object setProperty(String key,String value)
//获取元素
public String getProperty(String key)
//获取所有的键的集合
public Set<String> stringPropertyNames()
(3)和IO流结合的方法
//把键值对形式的文本文件内容加载到集合中
//把文件中的数据读取到集合中
public void load(Reader reader)
//把集合中的数据存储到文件
public void load(InputStream inStream)
//把集合中的数据存储到文本文件中
public void store(Writer writer,String comments)
public void store(OutputStream out,String comments)
(4)案例:
A:根据给定的文件判断是否有键为"lisi"的,如果有就修改其值为100
B:写一个程序实现控制猜数字小游戏程序不能玩超过5次
(1)JDK4出现的NIO,对以前的IO操作进行了优化,提供了效率。但是大部分我们看到的还是以前的IO
(2)JDK7的NIO的使用
Path:路径
Paths:通过静态方法返回一个路径
Files:提供了常见的功能
复制文本文件
把集合中的数据写到文本文件