IO:指应用程序和外部文件之间的数据传递
流:指一连串的字符或字节以先进先出的方式传输信息的通道
一般来说关于流的特性有下面几点:
1.先进先出:最先写入输出流的数据最先被输入流读取到。
2.顺序存取:可以一个接一个地往流中写入一串字节,读出时也将按写入顺序读取一串字节,不能随机访问中间的数据。(RandomAccessFile除外)
3.只读或只写:每个流只能是输入流或输出流的一种,不能同时具备两个功能,输入流只能进行读操作,对输出流只能进行写操作。在一个数据传输通道中,如果既要写入数据,又要读取数据,则要分别提供两个流。
FileInputStream
1.俩种创建方式
//方式一
//传入的参数必须具体到对应文件,否则会报拒绝访问异常,并且传入的路径必须正确,否则会报FileNotFoundException异常
InputStream fis = new FileInputStream("D:\\eclipseWork\\file");
//创建方式二
File file = new File("D:\\eclipseWork\\file\\ThreeKingdom.txt");
InputStream fis1 = new FileInputStream(file);
俩种方法底层其实都是通过方式一去创建的。
2.read()方法:读取一个字节,返回类型是int,当读取不到的时候返回-1
//read()方法读取一个字节,返回类型是int,当读取不到的时候返回-1
for(int i=0;i<10;i++)
System.out.println(fis.read());
将读取内容放到数组中进行读取,
注意:读取过程中可能会出现乱码,这是因为UTF-8和ASCALL码的编码格式不一样,中文字符在UTF-8中占3个字节,ASCALL码中占2个字节,本例中使用的是UTF-8进行编码解码,所以设置的字节数组大小为3.
//读取内容放到数组中进行读取
byte[] b = new byte[3];
while((i=fis.read(b))!=-1) {
System.out.println(new String(b,0,i));
}
BufferedInputStream
BufferedInputStream 的父类是FilterInputStream
BufferedInputStream:缓冲流,会用一个字节对象一次从内存中读取8192字节放到缓冲区
1.使用read()方法读取,提高读取效率
public static void main(String[] args) {
InputStream is = null;
InputStream bis = null;
try {
is = new FileInputStream("D:\\eclipseWork\\file\\ThreeKingdom.txt");
bis = new BufferedInputStream(is);
//每次从缓冲区读取3个字节
byte[] b =new byte[3];
//当前读取的字节的长度
int i;
while((i=bis.read(b))!=-1) {
System.out.println(new String(b,0,i));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
FileOutStream
BufferedOutputStream
BufferedOutputStream:效果跟BufferedInputStream一样
//路径必须明确到文件,如果没有文件则会自动创建出文件
os = new FileOutputStream("D:\\eclipseWork\\file\\girl.txt");
bos = new BufferedOutputStream(os);
在new FileOutputStream()时,参数可以传递俩个,第二个参数的true/false决定是否在文件里追加写的内容,如不追加会覆盖原文件
public static void main(String[] args) {
InputStream is = null;
InputStream bis = null;
OutputStream os = null;
OutputStream bos = null;
try {
is = new FileInputStream("D:\\eclipseWork\\file\\ThreeKingdom.txt");
bis = new BufferedInputStream(is);
//每次从缓冲区读取3个字节
byte[] b =new byte[3];
//当前读取的字节的长度
int i;
// while((i=bis.read(b))!=-1) {
// System.out.println(new String(b,0,i));
// }
//系统的换行符
String string = System.lineSeparator();
String aa = "苍天已死,黄天当立。岁在甲子,天下大吉。雷公助我!!!";
byte[] bb= (aa+string).getBytes();
//路径必须明确到文件,如果没有文件则会自动创建出文件
//参数true/false 决定是否追加内容 不追加默认为覆盖
os = new FileOutputStream("D:\\eclipseWork\\file\\girl.txt",false);
bos = new BufferedOutputStream(os);
bos.write(bb);
while((i=bis.read(b))!=-1) {
bos.write(b);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bis.close();
is.close();
bos.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
FileWriter
FileWriter:字符缓冲区8192个字节,构造方法中传入第二个参数true,开启追加不覆盖模式。
public static void main(String[] args) {
Writer writer = null;
String string = System.lineSeparator();
try {
//字符输出缓冲区8192字节
writer = new FileWriter("D://eclipseWork//file//writer.txt",true);
writer.write("半年实习生,一生java情!"+string);
writer.write(89);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
FileReader
InputStreamReader
转换流,public InputStreamReader(InputStream in, String charsetName)
第二个参数传的是编码类型