目录
IO 流 抽象类
IO流基本模式
1.创建源
2.选择流
3.操作
4.释放资源
文件字节输入流父类(InputStream)
FileInputStream
InputStream is =new FileInputStream(File类型/路径);
以字节或者字节数组的方式读取数据,
is .read();
is.read(flush);
byte[] flush =new byte[5] //创建字节数组
String str =new String(字节数组,起始位置,末位置)//将字节数组转化成字符串
package cn.sxt.IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/*
* 文件 字节输入流
* 1.创建源文件
* 2.选择流
* 3.操作
* 4.释放
*/
/*
* 文件读取
* 创建文件和读取流,字节数组控制读每次取个数,read() 读取
*/
public class TestIO01 {
public static void main(String[] args) throws IOException {
File src =new File("test");
InputStream is =new FileInputStream(src);
int len=-1;
byte flush [] =new byte[3];
while((len=is.read(flush))!=-1) {
String str =new String(flush,0,len);
System.out.println(str);
}
is.close();
}
}
文件字节输出流父类(OutputStream)
FileOutputStream
OutputStream os= new FileOutputStream(文件/文件路径,布尔类型);
如果为路径,则系统自动创建file对象,boolean 表示写入的文本是否覆盖。
os.write(字节数组,起始位置,末位置)
String msg="sf",msg.getBytes()//将字符串转化成字符数组
只有String 中由 字符串和字符数组的转换方法
os.flush();//刷新,防止数据滞留
package cn.sxt.IO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/*
* 文件字节输出流
* 1.创建源
* 2.选择流
* 3.操作(往文件里面输入东西)
* 4.关闭
*/
/*字节输出对文件编写
* 1.选择目标文件,创建输出流,创建字节数组控制输出,write(),编写
*
*/
public class IOtest02 {
public static void main(String[] args) {
File src =new File("test");
OutputStream os=null;
try {
os= new FileOutputStream(src,true);
String msg="fhailsgagias";
byte [] datas=msg.getBytes();//字符串到字节数组,编码
os.write(datas,0,datas.length);
os.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(null!=os)
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
字符输入流父类(Reader)
实现类FileReader
通过字节数组控制读取个数
char [] flush =new char[10];
.reade(flush)
String str=new String(flush,0,len);//将字符数组转换成字符串
package cn.sxt.IO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class IO_FileReader {
public static void main(String[] args) {
File src=new File("test");
Reader reader=null;
try {
reader = new FileReader(src);
char[] flush=new char [20];
int len=-1;
while((len=reader.read(flush))!=-1) {
String str=new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(null!=reader) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字符输出流父类(Writer)
实现类FileWriter
writer.append("tuutyiyi").append("sga");//FileWriter特有方法
char [] datas= msg.toCharArray();//字符串转换成字符数组
writer.write(datas,0,datas.length);
//字符串本事也是字符数组
String msg1="iyuiuiui";
writer.write(msg1);
writer.write("hkl");
//write.append 方法也可以//writer.write("hkl")不能连续的点
package cn.sxt.IO;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class IO_FileWriter {
public static void main(String[] args) {
File dest=new File("test02");
Writer writer=null;
try {
writer =new FileWriter(dest,true);
String msg ="asdgkhlaghaggas";
char [] datas= msg.toCharArray();//字符串转换成字符数组
writer.write(datas,0,datas.length);
//字符串本事也是字符数组
String msg1="iyuiuiui";
writer.write(msg1);
writer.write("hkl");
//write.append 方法也可以//writer.write("hkl")不能连续的点
writer.append("tuutyiyi").append("sga");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null!=writer)
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}