1.字节流是原生的操作,而字符流是经过处理后的操作
2.对于IO操作属于资源处理,所有的资源处理操作(IO操作、数据库操作、网络)最后必须要进行关闭。
3.字符流适合处理中文,字节流适合处理一切数据类型,图片、音乐、文字都可以使用字节流
4.字节流一定是优先考虑的,只有在处理中文时才会考虑字符流。因为所有的字符都需要通过内存缓冲来进行处理
一、字节输出流FileOutputStream(二进制文件的写入)
实现程序输出内容到文件的处理,程序到磁盘文件
接收File类(覆盖):public FileOutputStream(File file) throws FileNotFoundException
接收File类(追加):public FileOutputStream(File file, boolean append)
将给定的字节数组内容全部输出:public void write(byte b[]) throws IOException
将部分字节数组内容输出:public void write(byte b[], int off, int len) throws IOException
void flush () throws IOException 将输出流中缓存的数据全部写到目的地
public class FileTest1 {
public static void main(String[] args) throws Exception {
File file=new File(File.separator + "F:" + File.separator + "Java" +
File.separator + "Test1"
+ File.separator + "HH.txt");
//如果父类目录不存在创建多级目录
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
// OutputStream是一个抽象类,所以需要通过子类进行实例化
// 此时只能操作File类
OutputStream output1=new FileOutputStream(file);
//追加接收File类(追加):
OutputStream output=new FileOutputStream(file,true);
// 要求输出到文件的内容
String message="hello java !!!";
// 1.将全部内容变为字节数组
output.write(message.getBytes());
//2.将部分内容变为字节数组
output.write(message.getBytes(),0,5);
output.close();
}
}
二、字节输入流FileInputStream(二进制文件的读取)
从二进制磁盘文件中读取字节数据
//实现文件信息的读取
public class InputStreamTest {
public static void main(String[] args) throws IOException {
File file = new File(File.separator + "F:" + File.separator + "Java"
+ File.separator + "Test1" + File.separator + "HH.txt");
// 2.必须保证文件存在才能进行处理
if (file.exists()) {
InputStream input = new FileInputStream(file);
byte[] data = new byte[1024]; // 每次可以读取的最大数量
int len = input.read(data); // 此时的数据读取到了数组之中
String result = new String(data, 0, len); // 将字节数组转为String
System.out.println("读取内容【" + result + "】");
input.close();
}
}
}
三、(文件文本的读取)FileReader
用于写入字符流到磁盘
Writer类中提供有方法直接向目标源写入字符串,而在Reader类中没有方法可以直接读取字符串类型,这个时候只能通过字符数组进行读取操作
public class ReaderTest {
public static void main(String[] args) throws IOException {
File file=new File(File.separator + "F:" + File.separator + "Java" +
File.separator + "Test1"
+ File.separator + "BB.txt");
// 2.必须保证文件存在才能进行处理
if (file.exists()) {
Reader in = new FileReader(file) ;
char[] data = new char[1024] ;
int len = in.read(data) ; // 将数据读取到字符数组中
String result = new String(data, 0, len) ;
System.out.println("读取内容【"+result+"】") ;
in.close();
}
}
}
//文本文件的读取
public class FileReaderDemo {
public static void main(String[] args) throws IOException {
FileReader e=new FileReader("F:/Java/Test1/HH.txt");
int ch;
while((ch=e.read())!=-1){
System.out.print((char)ch);
}
e.close();
}
}
四、文本文件写入 FileWriter
有直接输出字符串的方法public void write(String str) throws IOException,如果要操作文件使用FileWriter子类
public class WriterTest {
public static void main(String[] args) throws IOException {
File file=new File(File.separator + "F:" + File.separator + "Java" +
File.separator + "Test1"
+ File.separator + "BB.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
String a="ok!";
Writer out=new FileWriter(file);
out.write(a);
out.close();
}
五、字符输入流
OutputStreamWriter类和InputStreamReader类用来实现字符和字节之间的转换,写入OutputStreamWriter的字符翻译为字节,从InputStreamReader类读出的字符翻译为字符
//文本文件读取
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("F:/Java/Test1/HH.txt"), "UTF-8");
int i;
while ((i = in.read()) != -1) {
System.out.print((char) i);
}
in.close();
}
}
六、字符输出流
//文本文件写入
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
FileOutputStream u = new FileOutputStream("F:/Java/Test1/HH.txt");
OutputStreamWriter out = new OutputStreamWriter(u, "UTF-8");
out.write(" UTF-8");
out.close();
}
}
、AutoCloseable自动关闭支持
这种自动关闭处理是需要结合try语句进行调用。
//使用自动关闭处理之前的操作
public class AutoCloseable1 {
public static void main(String[] args) {
File file = new File("F:" + File.separator + "Java" + File.separator + "Test1" + File.separator + "DD.txt");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try (OutputStream out = new FileOutputStream(file)) {
String a = "java";
out.write(a.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
//try...catch
class Message implements java.lang.AutoCloseable {
public Message() {
System.out.println("第一条消息");
}
@Override
public void close() throws Exception {
System.out.println("自动关闭");
}
public void print() {
System.out.println("www.java");
}
}
public class AutoCloseable {
public static void main(String[] args) {
try (Message m = new Message()) {
m.print();
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果字符流不关闭,则需要强制刷新out.flush(); // 写上此语句表示强制清空缓冲内容,所有内容都输出