IO流技术

时间:2023-03-09 14:59:38
IO流技术

IO流常用基类

字节流的抽象基类:InputStream,OutputStream

字符流的抽象基类:Reader,Writer

Writer类

子类:BufferedWriter,CharArrayWriter,FilterWriter,OutputStreamWriter,PipedWriter,PrintWriter,StringWriter

FileWriter extends OutputStreamWriter

FileWriter(String filenme,boolean append)

如果append为true,则将字节写入文件末尾处,而不是写入文件开始处。

public static void main(String[] args) {
FileWriter fr=null;
// TODO Auto-generated method stub
try {
fr= new FileWriter("demo.txt");
fr.write("hello,world!");
fr.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(fr!=null)
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

FileReader的两种读取方式

fr = new FileReader("demo.txt");
int c=0;
while((c=fr.read())!=-1){
System.out.print((char) c);
}
char [] buf= new char[1024];
try {
fr = new FileReader("demo.txt");
int c=0;
while((c=fr.read(buf))!=-1){
System.out.print(new String(buf,0,c));
}
}

复制功能的实现

char [] buf= new char[1024];
try {
fr = new FileReader("demo.txt");
fw = new FileWriter("dest.txt");
int c=0;
while((c=fr.read(buf))!=-1){
fw.write(buf, 0, c);
}
}

BufferedWriter,BufferedReader
bufferedWriter的用法

private static FileWriter fw;
private static BufferedWriter bw;
public static void main(String[] args) {
try {
fw=new FileWriter("buffered.txt");
bw=new BufferedWriter(fw); bw.write("hello,lvjy");
bw.newLine();
bw.write("hello,world");
bw.flush();

bufferedReader的readLine()方法不包含任何行终止符

LineNumberReader

setLineNumber()和getLineNumber()

字节流:InputStream,OutputStream

想要操作图片数据,这时就要用到字节流

FileInputStream

available()方法返回可读取的字节数

byte[] buf=new byte[fis.available()];定义一个刚刚好缓冲区,不需要再用循环了

字节流缓冲区:BufferedInputStream,BufferedOutputStream

读取键盘录入:

System.out:对应的是标准输出设备,控制台。

System.in:对应的的是标准输入设备,键盘。

package com.travelsky.io;

import java.io.IOException;
import java.io.InputStream; public class ReadIn { public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream in = System.in;
StringBuilder sb=new StringBuilder();
while(true){
int c=in.read();
if(c=='\r')
continue;
if(c=='\n'){
String s=sb.toString();
if("over".equals(s)){
break;
}
System.out.println(s.toUpperCase());
sb.delete(0, sb.length());
}else{
sb.append((char)c);
}
}
} }

InputStreamReader转换流

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream in = System.in;
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while(true){
String line = br.readLine();
if("over".equals(line)){
break;
}else{
System.out.println(line.toUpperCase());
}
}
}

流操作的基本规律

通过两个明确来完成:

1.明确源和目的

  源:输入流——InputStream,Reader

  目的:输出流——OutputStream,Writer

  

2.操作的数据是否为纯文本

  是:字符流

  否:字节流

3.当体系明确后,再明确要使用哪个具体的对象

通过设备来进行区分:

  源设备:内存,硬盘,键盘

  目的设备:内存,硬盘,控制台

OutputStreamWriter可以进行字节编码方式的转换

OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("filename.txt"),"UTF-8");

字符和字节编解码相关内容

public byte[] getBytes(String charsetName)
使用指定的字符集将此String编码为byte序列,结果存在一个byte数组中
public String(byte[] bytes, String charsetName)
通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
网络传输中,信息都是以字节序列的方式传输的。所以,发送方的String要按照某种编码方式(如UTF-8,GBK)编码为字节序列,在网络中传输后,接收方取得这个字节序列,按照相同的编码方式将字节序列解码为String。

改变标准输入输出设备:

System.setIn(InputStream in)

System.setOut(PrintStream out)

获取系统信息

public class ReadIn {

    public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Properties props=System.getProperties();
props.list(System.out);
} }