1、BIO分类:I/O为input/output 即读/写
1)按流向:输入流、 输出流
2)读取方式:字节流 、字符流
字节输入流(InputStream)和字节输出流(OutputStream);
字符输入流和字符输出流;
例:文件的读出:
import java.io.*;
public class Test2 {
public static void main(String[] args) throws FileNotFoundException {
InputStream is = new FileInputStream(“d://3/5.txt”);
try {
byte[] b = new byte[1024];
int i = is.read(b);
String str = “”;
while(i>0){
str += new String(b,0,i);
i = is.read(b);
}
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
例:文件的写入:
public static void main(String[] args) throws Throwable {
OutputStream os = new FileOutputStream(“d://3/诗情画意.txt”);
String s = “天山鸟飞绝,万径人踪灭。”;//文件的写入
os.write(s.getBytes());
os.flush();
OutputStream os2 = new FileOutputStream(“d://3/诗情画意.txt”,true);//true表示跟随之前的内容继续写,若无则替换
String s2 = “孤舟蓑笠翁,独钓寒江雪”;//文件的写入
os2.write(s2.getBytes());
os2.flush();
}
2、文件复制
public static void main(String[] args) throws Throwable {
InputStream is = new FileInputStream(“d://3/诗情画意.txt”);
OutputStream os = new FileOutputStream(“d://3/诗.txt”);
byte[] b = new byte[1024];
int i = is.read(b);
while(i>0){
os.write(b,0,i);
i = is.read(b);
}
os.flush();
}