---------------------- 黑马程序员 Android培训、期待与您交流! ---------------------
Io流
按照操作数据可分为: 1.字节流 2.字符流按照流向可分为: 1.输入流 2.输出流
InputStream和OutputStream是java.io包中的两个抽象类,他们分别是其他所有输入、输出流类的父类。 File类将文件或者文件夹封装成对象,可将对象作为参数传递给流的构造函数。 递归:函数方法自身调用自身的功能成为递归。
FileReader和FileWrite类分别继承自Reader类和Writer类,继承关系如图6-2所示。FileReader类用于读取文件;FileWrite类用于将数据写入文件。在使用这两个类之前,必须调用其构造方法创建相应的对象,然后才能调用相应的read()或write()方法进行文件的读写操作
BufferedReader和BufferedWrite类分别继承自Reader类和Writer类,BufferedReader类用来从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。BufferedWrite类用于将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
FileInputStream类继承于InputStream类;FileOutputStream类继承于OutputStream类。FileInputStream和FileOutputStream类中提供的文件处理方式是文件中数据流的顺序读写,而不是利用文件指针进行定位的随机读写
按照数据的类型可视情况选定对象类 文件:FileInputStream/FileOutputStream;FileReader/FileWriter byte[]:ByteArrayInputStream/ByteArrayOutputStream Char[]:CharArrayreader/CharArrayWriter String:StringBufferInputStream,StringReader,StringWriter 图片切割练习: import java.io.*; import java.util.*; classQiege { public static void main(String[] args)throws IOException { //Qiege(); Hebing(); } public static void Hebing()throws IOException { ArrayList<FileInputStream> a =new ArrayList<FileInputStream>(); for (int x=1; x<3; x++) { a.add(new FileInputStream("d:\\abc\\"+x+".分卷")); } final Iterator<FileInputStream> it =a.iterator(); Enumeration<FileInputStream> en= new Enumeration<FileInputStream>() { public boolean hasMoreElements() { return it.hasNext(); } public FileInputStream nextElement() { return it.next(); } }; SequenceInputStream sis= new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("d:\\abc\\2.jpg"); byte[] buf =new byte[1024]; int len =0; while ((len=sis.read(buf))!=-1) { fos.write(buf,0,len); } fos.close(); sis.close(); } public static void Qiege()throws IOException //切割 将一个文件通过切割方法,进行切割,通过流的写入方法,写写成等份的几段数据 { FileInputStream fis = new FileInputStream("d:\\1.jpg"); FileOutputStream fos = null; byte[] buf = new byte[1024*1024]; int len=0; int count =1; while ((len=fis.read(buf))!=-1) { fos = new FileOutputStream("d:\\abc\\"+(count++)+".分卷"); fos.write(buf,0,len); fos.close(); } fis.close(); } }
import java.io.*;
class
public static void main(String[] args)
BufferedReader br= null;
BufferedWriter bw= null;
try //对异常抓取{
br= new BufferedReader(new FileReader("abc.txt"));
bw= new BufferedWriter(new FileWriter("abc1.txt"));
String line=null;
while ((line= br.readLine())!=null) {
bw.write(line);//BufferedWriter中有写入一行的方法
bw.newLine(); //换行方法 newLine();
bw.flush(); //刷新缓冲区,但没有关闭
}
}
catch (IOException e) {
throw new RuntimeException("读写失败");//抛出ioe异常
}
finally
try{
if (br!=null){
br.close();
}
}
catch (IOException e){
throw new RuntimeException("读取关闭失败");
}
try{
if (bw!=null)
bw.close();//当缓冲区的对象不为空时,可以关闭
}
catch (IOException e){
throw new RuntimeException("写入关闭失败");
}
}
}
}
import java.io.*;
class DeleteTest
public static void main(String[] args)
File dir =new File("d:\\test");//建立目录对象,将文件目录传入方法中
delete(dir);//调用方法,将定义的目录对象传入方法中
}
public static void delete(File dir){
File[] files = dir.listFiles();
for (int x=0; x<files.length; x++){
if (files[x].isDirectory())//文件是目录,判断这一时刻中是否存在目录
delete(files[x]);//是目录调用delete方法,递归
else{
System.out.println(files[x].toString()+"___---"+files[x].delete());//判断不是目录,将文件删除,files[x].toString()为将此时代表文件的元素转换成字符串
System.out.println(dir+"---目录---"+dir.delete());//递归循环结束后,文件夹中不存在文件,删除目录
}
}
---------------------- 黑马程序员 Android培训、期待与您交流! ---------------------