Io流中的其他流
数据输入流,让应用程序读取原始java数据类型从底层输入流中的一个独立于机器的方式。一个应用程序使用一个数据输出流来写数据,以后可以通过数据输入流读取。
输入流是不一定安全的多线程访问。线程安全是可选的,是在这个类中的方法的用户的责任。
写基本数据类型
dos.writeInt(45) ;
dos.writeChar('中');
dos.writeUTF("你好");
读取数据int a = dis.readInt() ;System.out.println(a);char ch = dis.readChar() ;System.out.println(ch);String str = dis.readUTF() ;System.out.println(str);
演示
public class MyTest {public static void main(String[] args) throws IOException { // 数据输入输出流:特点就是能够读写基本数据类型 // writeData(); //注意读取的顺序,刚才怎么写的,就怎么读 DataInputStream in = new DataInputStream(new FileInputStream("a.txt")); boolean b = in.readBoolean(); double v = in.readDouble(); int i = in.readInt(); char c = in.readChar(); String s = in.readUTF(); System.out.println(b); System.out.println(v); System.out.println(c); System.out.println(s); in.close(); return;}private static void writeData() throws IOException { // 数据输入输出流:特点就是能够读写基本数据类型 DataOutputStream out = new DataOutputStream(new FileOutputStream("a.txt")); out.writeBoolean(true); out.writeDouble(3.14); out.writeInt(1000); out.writeChar('a'); out.writeUTF("薛晓燕"); out.flush(); out.close();}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write("今天是个好日子".getBytes());
out.write("今天我要嫁给你了".getBytes());
//取出他缓存中的数据
byte[] bytes = out.toByteArray();
String s = new String(bytes);
System.out.println(s);
String s2 = out.toString();
System.out.println(s);
out.close();//此流无需关闭
}
}
ByteArrayInputStream
ByteArrayOutputStream
此流关闭无效,所以无需关闭
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write("今天是个好日子".getBytes());
out.write("今天我要嫁给你了".getBytes());
//取出他缓存中的数据
byte[] bytes = out.toByteArray();
String s = new String(bytes);
System.out.println(s);
String s2 = out.toString();
System.out.println(s);
out.close();//此流无需关闭
}
}
CharArrayWrite CharArrayReader
演示
public class MyTest4 {
public static void main(String[] args) throws IOException {
//操作字符数组
//CharArrayWrite
//CharArrayReader
CharArrayWriter charArrayWriter =new CharArrayWriter(); charArrayWriter.write("abcd"); charArrayWriter.write(new char[]{'我','爱','你'}); char[] chars = charArrayWriter.toCharArray(); String s1 = new String(chars); String s2 = String.valueOf(chars); System.out.println(s1); System.out.println(s2); String s = charArrayWriter.toString(); System.out.println(s);}
}
操作字符串 StringWriter StringReader
演示
public class MyTest5 {
public static void main(String[] args) {
//操作字符串
// StringWriter
//StringReader
StringWriter stringWriter = new StringWriter();
stringWriter.write("abc");
stringWriter.write("呵呵呵呵呵");
String s = stringWriter.toString();
System.out.println(s);
}
}
内存操作流的概述一个 ByteArrayInputStream包含一个内部缓冲区包含的字节,可以从流中读取。一个内部计数器跟踪下一个字节是由 read提供的方法。
关闭ByteArrayInputStream没有影响。这个类中的方法可以在流一直没有产生一个IOException闭叫.
a: 打印流只能操作目的地,不能操作数据源(不能进行读取数据)- b: 可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型
c: 如果我们启用自动刷新,那么在调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新
/**
通过以下构造创建对象 能够启动自动刷新 然后调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新
*/
d: 这个流可以直接对文件进行操作(可以直接操作文件的流: 就是构造方法的参数可以传递文件或者文件路径)
演示public class MyTest {
public static void main(String[] args) throws IOException {
//打印流:只是写,不操作源文件 就是单个的一个流,只用来输出
//字节打印流 PrintStream
//字符打印流 PrintWriter
PrintStream out2 = System.out; //他关联的设备是屏幕
out2.println("abc");
//这种方式关联的是文件PrintStream stream = new PrintStream(new File("c.txt"));stream.println("abc");stream.println("abc");stream.println("abc");stream.println("abc");stream.println("abc");stream.println("abc");stream.println("abc");stream.println("abc");stream.println("abc");stream.write("welcome".getBytes());stream.close();
}
}
PrintWriter实现自动刷新和换行
PrintWriter pw = new PrintWriter(new FileWriter("printWriter2.txt") , true) ;
pw.println(true) ;
pw.println(100) ;
pw.println("中国") ;
public class MyTest4 {
public static void main(String[] args) throws IOException {
//PrintWriter(OutputStream out, boolean autoFlush)
//通过现有的 OutputStream 创建新的 PrintWriter。
PrintWriter pw = new PrintWriter(new FileOutputStream("cc.txt"), true);
// pw.write("abc");
// 如果启用了自动刷新,则只有在调用 println、printf 或 format 的其中一个方法时才可能完成此操作
pw.println("abc");
pw.flush();
pw.close();
}
}
标准输入输出流标准输入输出流概述在System这个类中存在两个静态的成员变量:
public static final InputStream in: 标准输入流, 对应的设备是键盘
public static final PrintStream out: 标准输出流 , 对应的设备就是显示器
System.in的类型是InputStream.
System.out的类型是PrintStream是OutputStream的孙子类FilterOutputStream 的子类.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
演示public class MyTest {
public static void main(String[] args) throws IOException {
//键盘录入的第二种方式
//Scanner sc = new Scanner(System.in);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
System.out.println("请输入字符串");
String s = reader.readLine();
System.out.println(s);
//自定义一个结束标记
if("886".equals(s)){
break;
}
}
}
}
输出语句用字符缓冲流改进
/**
获取System下的in成员变量
*/
InputStream in = System.in ;
/**
in是一个字节输入流对象,那么我们就可以通过这个字节输入流对象进行读取键盘录入的数据.那么我们既然要读取数据,之前我们讲解了两种读取数据的方式:一次读取一个字节一次读取一个字节数组那么我们在这个地方使用那种读取方式. 经过分析,这两种读取方式都不太合适.因为数据是客户通过键盘录入进来的,而我们希望直接读取一行数据. 而既然要读取一行数据,那么我们就需要使用readLine方法,而这个方法是属于BufferedReader的方法,而我们就需要创建一个BufferedReader对象进行读取数据.而我们这in有属于字节流,而创建BufferedReader对象的时候需要一个字符流,而我们就需要将这个字节流转换成字符流,那么既然要对其进行转换,那么就需要使用转换流. 需要使用InputStreamReader
*/
RandomAccessFile概述 最大特点 能读能写
RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入。
RandomAccessFile的父类是Object , 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据.
我们可以通过getFilePointer方法获取文件指针,并且可以通过seek方法设置文件指针序列化流和反序列化流序列化流的概述所谓的序列化:就是把对象通过流的方式存储到文件中.注意:此对象 要重写Serializable 接口才能被序列化
反序列化:就是把文件中存储的对象以流的方式还原成对象
序列化流: ObjectOutputStream
反序列化流: ObjectInputStream
像这样一个接口中如果没有方法,那么这样的接口我们将其称之为标记接口(用来给类打标记的,相当于猪肉身上盖个章)
一个对象可以被序列化的前提是这个对象对应的类必须实现Serializable接口
public class MyTest6 {
public static void main(String[] args) throws Exception{
ObjectInputStream stream = new ObjectInputStream(new FileInputStream("list.txt"));
Object obj = stream.readObject();
ArrayList<Student> list= (ArrayList<Student>) obj;
Student student = list.get(2);
System.out.println(student.getName()+"=="+student.getAge());
}
}
class Student implements Serializable {
private static final long serialVersionUID = 5760262756605700379L;
//生成一个类的唯一id
private String name;
//transient 修饰成员变量后,此成员变量的就不会序列化到文件中
//transient private int age;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
会验证这个标记和序列化前的标记是否一致,如果一致就正常进行反序列化,如果不一致就报错了. 而现在我们把这个类做了修改,将相当于更改了标记,而导致这两个标记不一致,就报错了.解决问题: 只要让这个两个标记一致,就不会报错了吧 怎么让这两个标记一致呢? 不用担心,很简单,难道你们没有看见×××警告线吗? ctrl + 1 , 生成出来如何让对象的成员变量不被序列化
使用transient关键字声明不需要序列化的成员变量
private transient int age ;// 可以阻止成员变量的序列化使用transient
Properties的概述的 Properties类代表一个持久的特性。的 Properties可以保存到流或流中加载。属性列表中的每个键和它的相应值是一个字符串。
属性列表可以包含另一个属性列表作为它的“默认”;如果在原始属性列表中没有找到属性键,则搜索该第二个属性列表。
public Object setProperty(String key,String value)
public String getProperty(String key)
public Set<String> stringPropertyNames()
Properties和IO流进行配合使用:
public void load(Reader reader): 读取键值对数据把数据存储到Properties中public void store(Writer writer, String comments)把Properties集合中的键值对数据写入到文件中, comments注释演示public class MyTest2 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("武大", "金莲");
properties.setProperty("武大2", "金莲2");
properties.setProperty("武大3", "金莲3");
//把集合中的数据,保存到文件中去
properties.store(new FileWriter("data.properties"),null);
}
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。