输入流(读取数据的流)
BufferedInputStream---继承--->FileInputStream--继承--->InputStream------>
(1)字节流操作中文数据不是特别的方便,所以就出现了转换流。
转换流的作用就是把字节流转换字符流来使用。
(2)转换流其实是一个字符流
字符流 = 字节流 + 编码表
a. 字节输入流
InputStram
read();
read(byte b[]);
read(byte b[], int off, int len);
skip(long n);
available();
close();
InputStream fis=new FileInputStream("D:\1.txt");
int by=0;
while((by=fis.read())!=-1)
{
System.out.print((char)by);
}
fis.close();
FileInputStream: 继承InputStream
方法:
FileInputStream(String name);
FileInputStream(File file);
FileInputStream(FileDescriptor fdObj);
open(String name);
read();
read(byte b[]);
read(byte b[], int off, int len);
skip(long n);
available();
close();
FileInputStream fis=new FileInputStream("D:\1.txt");
int by=0;
while((by=fis.read())!=-1)
{
System.out.print((char)by);
}
fis.close();
b. 字符输入流
Writer
abstract class Writer implements Appendable, Closeable, Flushable
应使用它们的子类来创建实体对象
子类:
CharArrayReader | 从字符数组读取的输入流 |
BufferedReader | 缓冲输入字符流 |
PipedReader | 输入管道 |
InputStreamReader | 将字节转换到字符的输入流 |
FilterReader | 过滤输入流 |
StringReader | 从字符串读取的输入流 |
LineNumberReader | 为输入数据附加行号 |
PushbackReader | 返回一个字符并把此字节放回输入流 |
FileReader | 从文件读取的输入流 |
FileReader----继承---->InputStreamReader
FileReader fd=new FileReader("D:\1.txt");
int i=0;
while((i=fd.read())!=-1)
{
System.out.print((char)i);
}
c. 缓冲区输入流
BufferedReader
InputStream fis=new FileInputStream("D:\1.txt");
BufferedReader w=new BufferedReader(new InputStreamReader(fis));
String s=null;
while((s=w.readLine())!=null)
{
System.out.println(s);
}
BufferedInputStream 继承FilterInputStream
InputStream fis=new FileInputStream("D:\1.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
int s=0;
while((s=bis.read())!=-1)
{
System.out.print((char)s);
}
d.内存输入流(特殊流) 需要先写再读
ByteArrayInputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 写数据
for (int x = 0; x < 10; x++) {
baos.write(("hello" + x).getBytes());
}
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
int s=0;
while((s=bais.read())!=-1)
{
System.out.print((char)s);
}
CharArrayStream
CharArrayWriter baos = new CharArrayWriter();
String str="asdfgasgretfgertgergasdrg";
// 写数据
for (int x = 0; x < 2; x++)
{
baos.write(str.toCharArray());
}
CharArrayReader bais=new CharArrayReader(baos.toCharArray());
int s=0;
while((s=bais.read())!=-1)
{
System.out.print((char)s);
}
StringReader
StringWriter baos = new StringWriter();
String str="asdfgasgretfgertgergasdrg";
// 写数据
for (int x = 0; x < 2; x++)
{
baos.write(str);
}
StringReader bais=new StringReader(baos.toString());
int s=0;
while((s=bais.read())!=-1)
{
System.out.print((char)s);
}
e. 数据输入流
DataInputStream
// 创建数据输入流对象
DataInputStream dis = new DataInputStream(new FileInputStream("dos.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();
// 释放资源
dis.close();
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);
f.转换输入流(字节转字符)
InputStreamReader
InputStream is = new FileInputStream("D:\1.txt");
InputStreamReader isr = new InputStreamReader(is, "GBK");
int i = 0;
while ((i=isr.read())!=-1)
{
System.out.print((char)i);
}
g. 打印输入流(只能写不能读)