400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Io流中的其他流

数据输入输出流

数据输入流: DataInputStream

数据输出流: DataOutputStream

数据输入输出流的概述

数据输入流,让应用程序读取原始java数据类型从底层输入流中的一个独立于机器的方式。一个应用程序使用一个数据输出流来写数据,以后可以通过数据输入流读取。
输入流是不一定安全的多线程访问。线程安全是可选的,是在这个类中的方法的用户的责任。

成都创新互联公司IDC提供业务:乐山服务器托管,成都服务器租用,乐山服务器托管,重庆服务器租用等四川省内主机托管与主机租用业务;数据中心含:双线机房,BGP机房,电信机房,移动机房,联通机房。

特点: 可以写基本数据类型,可以读取基本数据类型
数据输入输出流的使用

写基本数据类型
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闭叫.

构造方法: public ByteArrayOutputStream()

打印流

打印流的特点

a: 打印流只能操作目的地,不能操作数据源(不能进行读取数据)
- b: 可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型

PrintWriter实现自动刷新和换行

PrintWriter实现自动刷新和换行
PrintWriter pw = new PrintWriter(new FileWriter("printWriter2.txt") , true) ;
pw.println(true) ;
pw.println(100) ;
pw.println("中国") ;

如果启用了自动刷新,则只有在调用 println、printf 或 format 的其中一个方法时才可能完成此操作
演示

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这个类中存在两个静态的成员变量:

/**


网站题目:Io流中的其他流
标题网址:http://mzwzsj.com/article/jeeddc.html

其他资讯

让你的专属顾问为你服务