一、低级流
1.字节输入流 FileInputStream
1.每次读入一个
//每次读取一个
public void inptutStream(String path) {
//文件
File file = new File ( path );
//字节型输入流
FileInputStream fis = null;
{
try {
fis = new FileInputStream ( file );
//每次读取一个
int i=fis.read ();
while (i != -1) {
//转为字符型
System.out.print( (char)i );
//再次读取一个字符
i=fis.read ();
}
} catch (IOException e) {
e.printStackTrace ();
}finally {
try {
//关闭输入流
fis.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
}
2.每次读入在byte数组中
public void inptutStream2(String path) {
//文件
File file = new File ( path );
//字节型输入流
FileInputStream fis = null;
{
try {
fis = new FileInputStream ( file );
//每次读取10个字节放在数组中
byte bytes[] = new byte[10];
//返回每次读取多少个数据
int code= fis.read (bytes);
while (code!=-1) {
//防止最后一次读数据出现脏数据
String value=new String ( bytes,0,code );
System.out.print( value);
code= fis.read (bytes);
}
} catch (IOException e) {
e.printStackTrace ();
}finally {
try {
fis.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
}
2.字节输出流FileOutPutDemo
public void FileOutPutDemo(String path){
//写入文件
File file=new File ( path );
//输出流
FileOutputStream fos=null;
try {
//append"已追加的方式传参,不加是以覆盖的方式
fos =new FileOutputStream ( file,true );
//写入字符串
String value ="\r写入文件";
byte bytes[]=value.getBytes ();
//字节写入文件
fos.write (bytes);
//刷新
fos.flush ();
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}finally {
try {
//关闭输入流
fos.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
3.使用字节流复制一个文件
public void inputOutput(String path, File file) {
//创建输入流
FileInputStream fis = null;
//复制文件路径
System.out.println ( path + "copy_" + file.getName () );
File newFile = new File ( path + "\\copy_" + file.getName () );
//创建输出流
FileOutputStream fos = null;
try {
fis = new FileInputStream ( file );
fos = new FileOutputStream ( newFile, true );
//读取数据
byte[] bytes = new byte[1024];
int code = fis.read ( bytes );
int i = 1;
while (code != -1) {
//将输入写入
fos.write ( bytes, 0, code );
//刷新
fos.flush ();
//再次读取数据
code = fis.read ( bytes );
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
fis.close ();
} catch (IOException e) {
e.printStackTrace ();
}
try {
fos.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
4.字符型输入流FileReader
1.每次读取一个字符
/**
*
* @param path 文件路径,文件名
* @param fileName
*/
public void fileReaderDemo(String path,String fileName){
File file=new File ( path+fileName );
//创建输入流
FileReader fr=null;
try {
fr=new FileReader ( file );
//返回每个字符的值
int value=fr.read ();
while (value!=-1){
//控制台打印
System.out.print ( (char) value );
//在从读取
value=fr.read ();
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}finally {
//关闭流
try {
fr.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
2.读取字符数组中
/**
*
* @param path 文件路径,文件名
* @param fileName
*/
public void fileReaderDemo1(String path,String fileName){
File file=new File ( path+fileName );
//输入流
FileReader fr=null;
try {
fr=new FileReader ( file );
//用来存放读取文件的数据
char value []=new char[1024];
//返回读取多少个字符,没有读取到返回-1
int code=fr.read (value);
while (code!=-1){
//控制台输出
System.out.print ( value );
//再次读取
code=fr.read ();
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}finally {
//关闭流
try {
fr.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
5.字符输出流 FileWriteDemo
public void FileWriteDemo(String path, String fileName) {
//数据写入的文件
File file = new File ( path + fileName );
//创建输出流
FileWriter fw = null;
try {
fw = new FileWriter ( file );
//将字符输入
String value = "可以看到类中的代码比较乱";
fw.write ( value );
fw.flush ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
try {
fw.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
6.字符型输入流与输出流配合复制文本型文件
/**
* @param path 文件路径,文件名
* @param fileName
*/
public void ReaderWriteDemo(String path, String fileName) {
//创建输入流与输出流
FileReader fr = null;
FileWriter fw = null;
//复制的新文件
File newFile = new File ( path + "new_" + fileName );
//将一次读取数据放在数组中
char values[] = new char[1024];
try {
//输入流与输出流
fr = new FileReader ( path + fileName );
fw = new FileWriter ( newFile );
//返回一次读取多少个字符,没有读取返回-1
int code = fr.read ( values );
while (code != -1) {
fw.write ( values, 0, code );
//刷新
fw.flush ();
//再次读取
code = fr.read ( values );
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
fr.close ();
} catch (IOException e) {
e.printStackTrace ();
}
try {
fw.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
二、缓存流
1. a.字节输入流
public void bufferFileInputDemo(String path, String fileName) {
//读取文件内容
File file = new File ( path + fileName );
//创建一个字节输入流
FileInputStream fis = null;
//创建一个字节缓存流
BufferedInputStream bfis=null;
try {
//实例化
fis = new FileInputStream ( file );
bfis = new BufferedInputStream ( fis );
//每次获取一个字节
int value = bfis.read ();
while (value != -1) {
//打印
System.out.print ( (char) value );
value = bfis.read ();
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
bfis.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
b将读取数据放入数组中
public void bufferFileInputDemo1(String path, String fileName) {
//创建一个文件对象
File file = new File ( path + fileName );
//创建一个字节输入流
FileInputStream fis = null;
//创建一个缓存流
BufferedInputStream bfis=null;
try {
//实例化
fis = new FileInputStream ( file );
bfis = new BufferedInputStream ( fis );
//存放数据的数组
byte bytes[]=new byte[1024];
//判断是否读取几个字节
int code = bfis.read (bytes);
while (code != -1) {
//将内容打印
String value =new String ( bytes );
System.out.print ( value );
//继续读取
code = bfis.read ();
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
fis.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
2.字节输出流
public void bufferOutputDemo(String path, String filename) {
//将数据存入文件中
File file = new File ( path + filename );
//创建输出流
FileOutputStream fos = null;
//创建缓存
BufferedOutputStream bops = null;
try {
//实例化
fos = new FileOutputStream ( file );
bops = new BufferedOutputStream ( fos );
//模拟数据
String value = "qwerdf";
//将数据写入文件中
bops.write ( value.getBytes () );
//刷新
bops.flush ();
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
bops.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
3.字符输入流
a.每次读取一个字符
public void bufferDemo(String path, String filaname) {
//读取数据文件
File file = new File ( path + filaname );
//创建一个字符型输入流
FileReader fr = null;
//创建一个缓存流
BufferedReader br = null;
try {
//实例化
fr = new FileReader ( file );
br = new BufferedReader ( fr );
//读取一个字符
int value = br.read ();
while (value != -1) {
//控制台打印
System.out.print ( (char) value );
value = br.read ();
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
br.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
b。一次读取一个数组的数据
public void bufferDemo1(String path, String filaname) {
//读取数据文件
File file = new File ( path + filaname );
//创建一个字符型输入流
FileReader fr = null;
//创建一个缓存流
BufferedReader br = null;
//存放放读取数据
char values[] = new char[1024];
try {
//示例化
fr = new FileReader ( file );
br = new BufferedReader ( fr );
//每次读取一个数组的数据
int code = br.read ( values );
while (code != -1) {
//将数据控制台打印
System.out.print ( (char) code );
//继续读取
code = br.read ();
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
br.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
c.每次读取一行数据
public void bufferDemo2(String path, String filaname) {
//读取数据文件
File file = new File ( path + filaname );
//创建一个字符型输入流
FileReader fr = null;
//创建一个缓存流
BufferedReader br = null;
try {
//示例化
fr = new FileReader ( file );
br = new BufferedReader ( fr );
//每次读取一行数据
String value = br.readLine ();
while (value != null) {
//将数据打印
System.out.println ( value );
//继续读取数据
value = br.readLine ();
}
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
br.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
4.字符输出流
public void bufferdWrite(String path, String filename) {
//写入目标文件
File file = new File ( path + filename );
//创建字符输出流
FileWriter fw = null;
//创建缓存流
BufferedWriter bw = null;
try {
//实例化
fw = new FileWriter ( file );
bw = new BufferedWriter ( fw );
//模拟数据
String value = "我是钢铁侠";
//数据写入文件
bw.write ( value );
//刷新
bw.flush ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
//关闭流
try {
bw.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}