文件流可以分為字節(jié)流和字符流
字節(jié)流
字節(jié)流可以對(duì)任何文件進(jìn)行操作 ,但效率不如字符流高
字節(jié)流分為字節(jié)輸入流和字節(jié)輸出流
輸入輸出是相對(duì)于電腦屏幕來(lái)說(shuō)
輸入流FileInputStream 的read(byte b[])函數(shù)是從指定文件中讀出數(shù)據(jù)字符數(shù)組b[]中
用字節(jié)輸入流向文件輸入數(shù)據(jù)之前,此文件必須存在,否則會(huì)拋異常
package com.qf.demo3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("abc.txt"));
int num = fis.available();// 查看剩余的 字節(jié)個(gè)數(shù)
System.out.println(num);
// 效率高
byte[] b = new byte[10];
// 文件內(nèi)容
// 偏移量+ 讀取的個(gè)數(shù) <= 數(shù)組的長(zhǎng)度
int num2 = fis.read(b, 5, 6);// p偏移量 偏移的是數(shù)組的 幾個(gè)
System.out.println(Arrays.toString(b));
System.out.println(num2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字節(jié)輸出流FileOutputStream 的write()函數(shù)
package com.qf.demo3;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
FileOutputStream fos = null;
try { // 輸出流 沒(méi)有文件會(huì)自動(dòng)幫助創(chuàng)建, 輸入流必須要求讀取文件存在
fos = new FileOutputStream(new File("def.txt"));// 只要輸出流與文件關(guān)聯(lián), 就會(huì)自動(dòng)的幫助創(chuàng)建文件
fos.write(97);
fos.write(98);
fos.write(new byte[]{97,98,99,100});
// 偏移量是便宜的數(shù)組的 偏移量+ 長(zhǎng)度 <= 數(shù)組長(zhǎng)度
fos.write(new byte[]{97,98,99,100}, 2, 2);
// 解決 執(zhí)行了 write 文件中有可能不能里面將內(nèi)容寫(xiě)進(jìn)去
fos.flush();// 刷新
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
- 復(fù)制文件
- 1 創(chuàng)建流對(duì)象
- 2 讀
- 3 寫(xiě)
- 4 關(guān)流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test3 {
public static void main(String[] args) {
// 1 創(chuàng)建流對(duì)象
FileInputStream fis = null;
FileOutputStream fos =null;
try {
fis = new FileInputStream(new File("abc.txt"));
fos = new FileOutputStream(new File("aaa.txt"),true);// boolean append true 追加 false 覆蓋
// 2 讀
byte[] bs = new byte[10];
int num = 0;
while((num = fis.read(bs))!=-1){
// 3 寫(xiě)
fos.write(bs,0,num);
fos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字符輸入流FileReader 以字節(jié)的形式將數(shù)據(jù)從文件中讀出來(lái)
package com.qf.demo4;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
FileReader reader = null;
try {
reader = new FileReader(new File("二狗.txt"));
int num = reader.read();
System.out.println(num);
char[] cbuf = new char[10];
// 返回值 代表 讀取的數(shù)據(jù)個(gè)數(shù) 讀取出來(lái)的內(nèi)容 放到char數(shù)組中
int num2 = reader.read(cbuf);
System.out.println(Arrays.toString(cbuf));
int num3 = reader.read(cbuf, 3, 7);
System.out.println(Arrays.toString(cbuf));
System.out.println("有效個(gè)數(shù)"+num3);
// 前面只讀了18
// 還剩下28個(gè)
reader.skip(28);// 跳過(guò)28個(gè)
int num4 = reader.read();
System.out.println(num4);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字符輸出流 FileWriter
package com.qf.demo4;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter(new File("副班長(zhǎng)"),true);
writer.write("明天會(huì)更好");
writer.write(new char[]{'后','天','呵','呵'});
writer.write(new char[]{'大','大','大','大','后','天','放','假'},0 , 6);
writer.write("開(kāi)心就好", 1, 3);
writer.append("說(shuō)的太對(duì)了");
writer.append('錯(cuò)');
writer.append("呵呵呵呵呵額呵呵哈", 2, 6);// 左閉右開(kāi)
writer.flush();// 刷新
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}