流!
分为两类:字符流,和字节流
一、字符流:Wirter,Reader==>FileWriter,FileReader==>BufferedReader,BufferedWriter
1,字符的写入
public static void main(String[] args) {
String path = "D:\\IO\\io.txt";
FileWriter fileWriter=null;
try {
fileWriter = new FileWriter(new File(path));
//fileWriter = new FileWriter("D:\\IO\\io.txt"); 可以直接填写路径,也可以new出新的file
fileWriter.write("zheshigeceshi这是个测试IO,呵呵哒");
/*fileWriter.flush(); //如果不加这个立即写入的话,只能关闭流之后才能写入。*/
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileWriter !=null){
try {
fileWriter.close(); //关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2,字符的读取
public static void main(String[] args) {
String path = "D:\\IO\\io.txt";
FileReader fileReader = null;
try {
fileReader = new FileReader(new File(path));
int temp = 0;
char[] chars = new char[1024]; //是char[] 字符串
while ((temp =fileReader.read(chars)) != -1) {
String string = new String(chars, 0, temp);
System.out.println(string);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileReader !=null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String path = "D:\\IO\\io.txt";
String copyPath = "D:\\IO\\ioCopy.txt";
FileReader fileReader=null;
FileWriter fileWriter=null;
try {
fileReader = new FileReader(new File(path)); //new 对象
fileWriter = new FileWriter(new File(copyPath));
int temp = 0;
char[] bs = new char[1024];
String string = null;
while ((temp = fileReader.read(bs)) != -1) { //读数据
string = new String(bs, 0, temp);
fileWriter.write(string); //写数据,如果不关闭流,则不写入
}
} catch (IOException e) {
e.printStackTrace();
}finally { //关闭流
if(fileReader !=null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fileWriter !=null){
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4,文件的复制(使用BufferedReader,BufferedWrite 利用缓冲区,快速的读写能力)
public static void main(String[] args) {
BufferedReader bReader =null;
BufferedWriter bWriter = null;
try {
bReader = new BufferedReader(new FileReader(new File("D:\\IO\\io.txt")));
bWriter = new BufferedWriter(new FileWriter(new File("D:\\IO\\ioCopy.txt")));
String line= null;
while ((line = bReader.readLine()) !=null) {
bWriter.write(line);
System.out.println(line); //输出每行的内容
bWriter.newLine(); //接着读取下一行
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bReader != null) { //关闭缓冲区,就能关闭流对象了
try {
bReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bWriter !=null) {
try {
bWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二、字节流:InputStream,OutputStream==>FileInputStream,FileOutputStream==>BufferedInputStream,BufferedOutputStream
1,字节流的读取
public static void main(String[] args) {
String pathname ="D:\\IO\\io.txt";
FileInputStream fileInputStream =null;
try {
fileInputStream= new FileInputStream(new File(pathname));
int temp = 0;
byte[] b = new byte[1024]; // 是byte[] 字节
while((temp = fileInputStream.read(b))!=-1){
System.out.println(new String(b, 0, temp));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileInputStream !=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2,字节流实现文件复制 (FileInputStream,FileOutputStream)
public static void main(String[] args) {
String pathname ="D:\\IO\\io.txt";
String pathCopyName ="D:\\IO\\ioCopy.txt";
FileInputStream fileInputStream =null;
FileOutputStream fileOutputStream =null;
try {
fileInputStream= new FileInputStream(new File(pathname));
fileOutputStream = new FileOutputStream(new File(pathCopyName));
int temp =0;
byte[] b = new byte[1024];
while((temp = fileInputStream.read(b))!=-1){
System.out.println(new String(b, 0, temp));
fileOutputStream.write(b, 0, temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileInputStream !=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2,字节流实现文件复制 (BufferedInputStream,BufferedOutputStream)
<strong> </strong>public static void main(String[] args) {
String pathname ="D:\\IO\\io.txt";
String pathCopyName ="D:\\IO\\ioCopy.txt";
BufferedInputStream bufferedInputStream =null;
BufferedOutputStream bufferedOutputStream=null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(pathname)));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(pathCopyName)));
int temp =0;
byte[] b = new byte[1024];
while((temp=bufferedInputStream.read(b))!=-1){
System.out.println(new String(b, 0, temp));
bufferedOutputStream.write(b, 0, temp);
}
System.out.println("复制完成!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedOutputStream !=null) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、问题来了
1,什么时候用字符流,什么时候用字节流?
所谓字符流,肯定是用于操作类似文本文件,或者带有字符文件的比较多。
所谓字节流,是操作那些无法直接获取文本信息的二进制文件,比如图片,mp3,视频文件等 说白了在硬盘上都是以字节存储的,只不过字符流在操作文本上面更方便一点而已
2,为什么使用缓冲区?
硬盘本身是有缓冲区的,试想一下,如果一有数据,不论大小就开始读写,势必会给硬盘造成很大负担,它会感觉很不爽,人不也一样,一顿饭不让你一次吃完,每分钟喂一勺,你怎么想?因此,采用缓冲区能够在读写大文件的时候有效提高效率