1.主要用于文件的读写,数据的网络传输(发送,接收)。 2.流是一组有序的字符集和,是对数据传输的总称或抽象。
1.按照处理数据类型分类:字节流和字符流

以上为 Java IO流中的四大基流 2.按照流向分类:输入流和输出流 输出:把程序(内存)中的内容输出到磁盘、光盘等存储设备中 输入:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中

操作 IO 流的模板: ①、创建源或目标对象 输入:把文件中的数据流向到程序中,此时文件是 源,程序是目标 输出:把程序中的数据流向到文件中,此时文件是目标,程序是源 ②、创建 IO 流对象 输入:创建输入流对象 输出:创建输出流对象 ③、具体的 IO 操作 ④、关闭资源 输入:输入流的 close() 方法 输出:输出流的 close() 方法 注意:1、程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源。如果不关闭该资源,那么磁盘的文件将一直被程序引用着,不能删除也不能更改。所以应该手动调用 close() 方法关闭流资源
字节流读操作
package com.guor.javaSE;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IOTest {
public static void main(String[] args) {
test05();
}
private static final String const_filePath = "D:\\guor\\data\\test.txt";
private static final String const_filePathChinese = "D:\\guor\\data\\中文.txt";
private File const_file = null;
private File const_fileChinese = null;
public IOTest() {
this.const_file = new File(const_filePath);
this.const_fileChinese = new File(const_filePathChinese);
}
/**
* 字节流读取文件:单个字符读取
* @param b_chinese_file
*/
private static void test01(boolean b_chinese_file) {
IOTest ioTest = new IOTest();
FileInputStream fis = null;
try {
if(true == b_chinese_file) {
//测试字节流读取中文乱码问题
fis = new FileInputStream(ioTest.const_fileChinese);
}else {
fis = new FileInputStream(ioTest.const_file);
}
int read = 0;
while ((read = fis.read())!=-1) {
log((char)read, false);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 字节流读取文件:数组循环读取
*/
private static byte[] test02() {
IOTest ioTest = new IOTest();
FileInputStream fis = null;
int len = 512;
byte[] buffer = new byte[len];
try {
fis = new FileInputStream(ioTest.const_file);
int read;
while ((read = fis.read(buffer,0,len)) != -1) {
log(buffer + "", true, false);
}
for(byte b : buffer) {
if(true == Character.isLetterOrDigit((char)b) || (char)b == '\n') {
log((char)b, false, false);
}
}
} catch (FileNotFoundException e) {
return new byte[1];
} catch (IOException e) {
return new byte[1];
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer;
}
}字符流读操作
/**
* 字符流读取中文文档,解决字节流读取中文乱码问题
*/
private static void test03() {
IOTest ioTest = new IOTest();
FileReader fr = null;
try {
fr = new FileReader(ioTest.const_fileChinese);
int read = 0;
while ((read = fr.read()) != -1) {
log((char)read, false);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}字节流写操作
/**
* 字节流写操作
* @throws IOException
* @throws FileNotFoundException
*/
private static void test04() {
String outPath = "D:\\guor\\data\\testNew.txt";
FileOutputStream fos = null;
try {
File file = new File(outPath);
byte[] buffer = test02();
fos = new FileOutputStream(file);
fos.write(buffer);
} catch (FileNotFoundException e) {
log("FileNotFoundException: " + e);
} catch (IOException e) {
log("IOException: " + e);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}字符流写操作
/**
* 字符流写操作
* @throws IOException
* @throws FileNotFoundException
*/
private static void test05() {
String outPath = "D:\\guor\\data\\中文New.txt";
IOTest ioTest = new IOTest();
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(ioTest.const_fileChinese);
StringBuilder sb = new StringBuilder();
int read = 0;
while ((read = fr.read()) != -1) {
log((char)read, false);
sb.append((char)read);
}
File file = new File(outPath);
fw = new FileWriter(file);
fw.write(sb.toString());
} catch (FileNotFoundException e) {
log("FileNotFoundException: " + e);
} catch (IOException e) {
log("IOException: " + e);
} finally {
if(fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}