杂七杂八的流
DataInputStream与DataOutputStream:
数据输入输出流:特点就是能够读写基本数据类型
注意读取的顺序,刚才怎么写的,就怎么读
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与ByteArrayInputStream:
操作字节数组, ByteArrayOutputStream 此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray () 和 toString () 获取数据。
关闭 ByteArrayOutputStream 无效。
public class MyTest2 {
public static void main(String[] args) throws IOException {
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);
}
}
PrintWriter:
打印流
打印流(PrintStream、PrintWriter)的特点:
PrintStream在OutputStream基础之上提供了增强的功能,即可以方便地输出各种类型的数据(而不仅限于byte类型)的格式化表示形式。PrintStream的方法从不抛出IOException
PrintWriter提供了PrintStream的所有打印方法,其方法也从不抛出IOException。
与PrintStream的区别:作为处理流使用时,PrintStream只能封装OutputStream类型的字节流,而PrintWriter既可以封装OutputStream类型的字节流,还能够封装Writer类型的字符输出流并增强其功能。
PrintWriter的构造方法:
PrintWriter(OutputStream out, boolean autoFlush):
参数autoFlush为true时,自动完成自动刷新功能
PrintWriter(Writer out, boolean autoFlush):
参数autoFlush为true时,自动完成自动刷新功能
PrintWriter的常用方法:
public void print(String s):
写出数据, 不换行
public void println(String x):
写出数据, 同时换行
public class MyTest3 {
public static void main(String[] args) throws IOException {
//字符打印流
// PrintWriter
PrintWriter writer = new PrintWriter("ee.txt");
writer.write("abc");
writer.write("ccc");
writer.flush();
writer.print(true);
writer.print(2.9);
writer.println(1000);
writer.flush();//字符流记得刷新
writer.close();
}
}
RandomAccessFile:
RandomAccessFile的特点:
最大特点 能读能写
RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入。
RandomAccessFile的父类是Object, 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据.
public long getFilePointer():
获取文件指针目前处于的位置
raf.seek(0):
可以设置文件指针的位置
public class MyTest2 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
boolean b = raf.readBoolean();
int i = raf.readInt();
String s = raf.readUTF();
System.out.println(b);
System.out.println(i);
System.out.println(s);
System.out.println(raf.getFilePointer()); //获取文件指针目前处于的位置
//可以移动文件指针
System.out.println("---------------------");
raf.seek(0);//可以设置文件指针的位置
b = raf.readBoolean();
i = raf.readInt();
s = raf.readUTF();
System.out.println(b);
System.out.println(i);
System.out.println(s);
return;
}
private static void writeData() throws IOException {
RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
raf.writeBoolean(false);
raf.writeInt(1000);
raf.writeUTF("你好"); //会多写两个字节
raf.close();
}
}