IO流
IO流的分类
1.根据流向分:输出流,输入流。(以程序为参照物,流入程序的为输入流,流出程序的为输出流)
2.根据数据单元分:字节流,字符流。(处理的单元为字节的是字节流,处理单元为字符的是字符流)
3.根据源头分:节点流,处理流。(直接与数据源接触的是节点流,反之是处理流)
字节输入流与字节输出流:万能流什么都可以读写
InputStream:
OutputStream:
InputStream is = new FileInputStream(“D:\copy.png”);
OutputStream os = new FileOutputStream(“D:\copy3.png”);
int b = 0;
while((b=is.read())!=-1){
System.out.println(b);//单字节读取显示在屏幕上
os.write(b);//单字节输出至磁盘}
byte[] buf = new byte[1024];
for(int len ;(len = is.read(buf))!=-1;){
System.out.println(new String(buf, 0, len));//byte数组读取显示在屏幕上
os.write(buf, 0, len);//byte数组输出至磁盘
}
is.close();
os.close();
字符输入流与字符输出流:专门来读取汉字的输入输出流
Reader:
Writer:
Reader r = new FileReader(“D:\再别康桥.txt”);
Writer w = new FileWriter(“D:\再别康桥复制.txt”);
for(int b=0;(b=r.read())!=-1;){
System.out.println((char)b);
w.write(b);
}
char [] cbuf = new char[1024];
for(int len=0;(len = r.read(cbuf))!=-1;){
System.out.print(new String(cbuf, 0, len));
w.write(cbuf, 0, len);
}
r.close();
w.close();
字节缓冲流与字符缓冲流:让数据不直接与磁盘接触,间接的提取数据,提高数据读取效率
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(ffile));
int b = 0;
while((b = bis.read())!=-1){
System.out.println((char)b);
bos.write(b);
}
byte [] buf = new byte[1024];
int len = 0;
while((len = bis.read(buf))!=-1){
System.out.println(new String(buf, 0, len));
bos.write(buf, 0, len);
}
bos.close();
bis.close();
数据流:读写Java中的基本数据类型和字符串
DataOutputStream dos = new DataOutputStream(new FileOutputStream(“D:\data.txt”));
dos.writeInt(99);
dos.writeUTF(“寸秒寸金”);
DataInputStream dis = new DataInputStream(new FileInputStream(“D:\data.txt”));
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
对象流:是将Java中的对象保存在磁盘上,或者在网上传输
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(“D:\write.txt”));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(“D:\write.txt”));
oos.writeObject(new Use(“琳”,123));
System.out.println((Use)ois.readObject());
oos.close();
ois.close();
数组流:将各种数据类型转换成Byte类型数组,目的:只要转成了字节就可以在网络上传输
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeInt(96);
oos.writeChar(‘C’);
oos.writeUTF(“轻轻的我走了”);
oos.writeObject(new Date());
byte[] buf = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
ObjectInputStream ois = new ObjectInputStream(bais);
System.out.println(bais.read());
System.out.println(ois.readInt());
System.out.println(ois.readChar());
System.out.println(ois.readUTF());
System.out.println(ois.readObject());
打印流:打印流不会抛出IOException
PrintStream:打印字节流
PrintWriter:打印字符流
PrintStream ps = new PrintStream(new FileOutputStream(“D:\PrintStream.txt”));
ps.println(“这是打印的字符”);
PrintWriter pw = new PrintWriter(System.out);
pw.write(“你好”);
pw.close();
ps.close();
转换流:用于字节与字符之间的转换
InputStreamReader isr = new InputStreamReader(new FileInputStream(“D:\再别康桥.txt”));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(“D:\再别康桥复制.txt”));
int b2 = 0 ;
while((b2=isr.read())!=-1){
System.out.print((char)b2);//单字符读取显示在屏幕上
osw.write(b2);//单字符输出至磁盘
}
char [] cbuf = new char[1024];
int len = 0;
while((len=isr.read(cbuf))!=-1){
System.out.println(new String(cbuf, 0, len));
osw.write(cbuf, 0, len);}
isr.close();
osw.close();
System.out指的是显示器,输出
System.out.print();
System.out.println();
System指的是类
System.out指的是类中的一个属性,这个属性是一个PrintStream类型
源码:
public final class System {
public final static PrintStream out = null;
}
System.in 指的是键盘,输入。例如:Scanner(Syteam.in);指的就是从键盘上读取输入
System.in指的是类中的一个属性,这个属性是一个InputStream类型
源码:
public final class System {
public final static InputStream in = null;
}
Scanner:扫描器
源码:
public final class Scanner implements Iterator, Closeable {…}
从Scanner类的标签可以看到它实现了Iterator接口所以就可以用迭代器遍历。
Scanner input = new Scanner(file);
while(input.hasNext()){System.out.println(input.nextLine());}