IO包中的其他类
打印流o
· PrintWriter 与 PrintStream
· 可以直接操作输入流和文件。
序列流
· SequencelnputStream
·对多个流进行合并。
操作对象
ObjectlnputStream 与 ODjieaiOOutputStream
·被操作的对象需要实现 Serializable (标记接口);
·练习:文件分割程序。
RandomAccessFile
·随机访问文件,自身具备读写的方法。
·通过skipBytes(intx), seek(intx)来达到随机访问。
管道流
PipedInputStream 和 PipedOutputStream
·输入输出可以直接进行连接,通过结合线程使用。
import java.io.*;
class Read implements Runnable
{
private PipedInputStream in;
Read(PipedInputStream i)
{
this in = in /
}
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)
{
throw new RuntimeException ("管道读取流失败");
}
}
}
class Write implements Runnable
{
private PipedOutputStream out;
Write(PipedOutputStream out)
this.out = out;
}
public void run()
{
try
{
system.out.printin("开始写入数据、筹待6秒后。");
Thread.sleep(6000);
out.write("piped lai la".getBytes());
out.close();
}
catch (Exception e)
{
throw new RuntimeException("管道输出流失败");
}
}
}
class PipedStreamDemo
{
public static void main(String[]args) throws IOException
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
Read r = new Read();
write w = new weite();
}
}
·操作基本数据类型
DatalnputStream 与 DataOutputStream
·操作字节数组
" ByteArraylnputStream 与 ByteArrayOutputStream
·操作字符数组I
CharArrayReader 与 CharArrayWrite
·操作字符串
StringReader 与 StringWriter
mport java.io.*;
class DataStreamDemo
{
public static void main(String[] args) throws IOException
{
//writeData():
//readData();
// writeUTFDemo ();
//OutputStreamWriterosw = new OutputStreamWriter(new File Output Btream("ghk. txt"), "ghk");
//osw.write(“你好”);
//osw.close();
// readUTFDemp();
}
readUTFDemo();
}
public static void readUTFDemo ()
{
DataInputStream dis = new DataInputStream (new File Inputstream ("utfdate."));
String s = dis.readUTF();
System.out.println(s);
dis.close();
}
public static void writeUTFDemo () throws IOException
{
Dataoutputstream dos = new Dataoutputstream (new Fileoutput Stram("utfdate. txt"));
dos writeUTF("你好");
dos.close();
}
}
用于操作字节数组的流对象。
ByteArrayInputstream :在构造的时候,需要接收数据源,。而且数据源是一个字节数组,
Bytechnicatgoutsbrean :在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组,这就是数据目的地.
因为这两个流对象都操作的数组,并没有使用系统资源
所以,不用进行close关闭,
在流操作规律讲解时:
源设备,
键盘System. in,硬盘 FileStream ,内存 Arraystream .目的设备:
控制台system. out,硬盘 Filestream ,内存 ArrayStream .
用流的读写思想来操作数据。
import java.io.*;
class ByteArrayStream
{
public static void main(String[]args)
{
//数据源。
ByteArayinputStreamb is = new ByteArrayInputStream (“ABCDEFD”, get Bytea());
// 数据目的
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
int by = 0;
while ((by = bis.read())!- 1)
{
bos.write(by);
}
System.out.println(bos.size());
System.out.println(bos.toString());
}
}
字符编码
·字符流的出现为了方便操作字符。
·更重要是的加入了编码转换。
·通过子类转换流来完成:
InputStreamReader
OutputStreamWriter
·在两个对象进行构造的时候可以加入字符集。
编码:字符串变成字节数组。
解码:字节数组变成字符串。
String-- > byte[]; str.getBytes(charsetName );
bytel]-- > String:new String(byte[], charsetName );
import java.util.*;
{
class EncodeDemo
{
public static void main(String[] args) throws Exception
{
string s = “你好”;
byte[] b1 = s.ɡetBytes("GBK");
System.out.println(Arrays.toString(b1));
Strings1 = new String(b1, "ISO8859-1");
system.out.println("sì=" + s1);
//对s1进行iso859-1编码。
byte[]b2 = s1.getBytes("iso9859−1");
System.out.printIn(Arrays.toString(b2));
Strings2 = new String(b2, "gb/:");
system.out.println("s2="ts2);
}
}



