Java课 IO流详解.ppt
安卓越科技(北京)有限公司第十三章第十三章JAVA IO安卓越科技(北京)有限公司回顾与作业点评回顾与作业点评线程的创建和启动线程的状态及转换线程的调度和优先级 线程的同步集合类的同步问题Timer类的调度任务安卓越科技(北京)有限公司本章任务本章任务掌握File类的使用掌握IO原理掌握文件流、缓冲流、转换流 掌握数据流、打印流、对象流掌握随机存取文件流、ZIP文件流安卓越科技(北京)有限公司知识要点知识要点File类的使用IO原理文件流、缓冲流、转换流 数据流、打印流、对象流随机存取文件流、ZIP文件流安卓越科技(北京)有限公司13.1 java.io.File类1.文件和目录2 java对文件和目录的操作1.)File类 的常用构造方法2.)File类 的常用属性3.)File类 中常用的访问属性的方法.安卓越科技(北京)有限公司 class FileAttributeTest public static void main(String args)throws IOException/把存储介质中指定路径中的文件抽象成Filte类对象File file=new File(D:IOTestsource.txt);/指定路径下一定要有这个文件存在,否则会有异常/后面的IO程序基本上都是在D:/IOTest下操作的System.out.println(文件或目录是否存在:+file.exists();System.out.println(是文件吗:+file.isFile();System.out.println(是目录吗:+file.isDirectory();System.out.println(名称:+file.getName();System.out.println(路径:+file.getPath();System.out.println(绝对路径:+file.getAbsolutePath();System.out.println(绝对路径规范表示:+file.getCanonicalPath();System.out.println(最后修改时间:+file.lastModified();System.out.println(文件大小:+file.length()+字节);安卓越科技(北京)有限公司4.)对文件的操作 createNewFile(),delete(),mkdir(),mkdirs(),renameTo()5.)浏览目录中的文件和子目录的方法 list()listFiles()安卓越科技(北京)有限公司import java.io.File;import java.io.IOException;/*文件操作演示*/public class FileOperateTest public static void main(String args)throws IOException File dir1=new File(D:/IOTest/dir1);if(!dir1.exists()/如果D:/IOTest/dir1不存在,就创建为目录dir1.mkdir();File dir2=new File(dir1,dir2);/创建以dir1为父目录,名为dir2的File对象if(!dir2.exists()/如果还不存在,就创建为目录dir2.mkdirs();File dir4=new File(dir1,dir3/dir4);if(!dir4.exists()dir4.mkdirs();File file=new File(dir2,test.txt);/创建以dir2为父目录,名为test.txt的File对象if(!file.exists()/如果还不存在,就创建为目录file.createNewFile();System.out.println(dir1.getAbsolutePath();/输出dir1的绝对路径名listChilds(dir1,0);/递归显示dir1下的所有文件和目录信息deleteAll(dir1);/删除目录 安卓越科技(北京)有限公司 static void listChilds(File dir,int level)/生成有层次感的空格StringBuilder sb=new StringBuilder(|-);for(int i=0;i level;i+)sb.insert(0,|);File childs=dir.listFiles();/递归出口int length=childs=null?0:childs.length;for(int i=0;i length;i+)System.out.println(sb.toString()+childsi.getName();if(childsi.isDirectory()listChilds(childsi,level+1);/删除目录或文件,如果参数file代表目录,会删除当前目录以及目录下的所有内容 public static void deleteAll(File file)/如果file代表文件,就删除该文件if(file.isFile()System.out.println(删除文件:+file.getAbsolutePath();file.delete();return;/如果file代表目录,先删除目录下的所有子目录和文件File lists=file.listFiles();for(int i=0;i 阻塞程序 if(s.equalsIgnoreCase(e)|s.equalsIgnoreCase(exit)System.out.println(安全退出!);break;/将读取到的整行字符串转成大写输出 System.out.println(-:+s.toUpperCase();System.out.println(继续输入信息);catch(IOException e)e.printStackTrace();finally try if(null!=br)br.close();/关闭过滤流时,会自动关闭它包装的底层节点流 catch(IOException e)e.printStackTrace();安卓越科技(北京)有限公司 13.7数据流:可操作基本数据类型的数据,分为1.DataInputSTream2.DataOutputStreamimport java.io.DataOutputStream;import java.io.FileOutputStream;import java.io.IOException;public class DataOutputStreamTest public static void main(String args)DataOutputStream dos=null;try/创建连接到指定文件的数据输出流对象dos=new DataOutputStream(new FileOutputStream(d:IOTestdestData.dat);dos.writeUTF(ab中国);/写UTF字符串dos.writeBoolean(false);/写入布尔值dos.writeLong(1234567890L);/写入长整数System.out.println(写文件成功!);catch(IOException e)e.printStackTrace();finally try if(null!=dos)dos.close();/关闭过滤流时,会自动关闭它包装的底层节点流 catch(IOException e)e.printStackTrace();安卓越科技(北京)有限公司13.8打印流:PrintStream和 PrintWriter:可将基本数据类型的数据转化成字符串输出。import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;public class PrintStreamTest public static void main(String args)FileOutputStream fos=null;try fos=new FileOutputStream(new File(D:IOTesttext.txt);catch(FileNotFoundException e)e.printStackTrace();/创建打印输出流,设置为自动刷新模式(写入换行符或字节 n 时都会刷新输出缓冲区)PrintStream ps=new PrintStream(fos,true);if(ps!=null)/把标准输出流(控制台输出)改成文件System.setOut(ps);for(int i=0;i=255;i+)/输出ASCII字符System.out.print(char)i);if(i%50=0)/每50个数据一行System.out.println();/换行ps.close();安卓越科技(北京)有限公司13.9对象流:ObjectInputStream 和ObjectOutputStream:用于存取和读取基本类型数据或对象的过滤流,可把java中的对象写到数据源中,也能把对象从数据源中还原回来。1.序列化和反序列化public class Student implements java.io.Serializable private static final long serialVersionUID=6858255444765880074L;private int id;private String name;private transient int age;/不需要序列化的属性 public Student()public Student(int id,String name,int age)this.id=id;this.name=name;this.age=age;public int getId()return id;public String getName()return name;public int getAge()return age;public String toString()return id=+id+,name=+name+,age=+age;安卓越科技(北京)有限公司import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;/*序列化示例*/public class SerializationTest public static void main(String args)ObjectOutputStream oos=null;try/创建连接到指定文件的对象输出流实例oos=new ObjectOutputStream(new FileOutputStream(D:IOTestobjectSeri.dat);oos.writeObject(new Student(101,张三,22);/把stu对象序列化到文件中oos.flush();/刷新输出流System.out.println(序列化成功!);catch(IOException e)e.printStackTrace();finally try if(null!=oos)oos.close();/关闭输出流实例 catch(IOException e)e.printStackTrace();安卓越科技(北京)有限公司 import java.io.FileInputStream;import java.io.IOException;import java.io.ObjectInputStream;/*反序列化示例*/public class DeserializationTest public static void main(String args)ObjectInputStream ois=null;try/创建连接到指定文件的对象输入流实例ois=new ObjectInputStream(new FileInputStream(D:IOTestobjectSeri.dat);Student stu=(Student)ois.readObject();/读取对象System.out.println(stu);/输出读到的对象信息 catch(ClassNotFoundException e)e.printStackTrace();catch(IOException e)e.printStackTrace();finally try if(null!=ois)ois.close();/关闭对象流实例 catch(IOException e)e.printStackTrace();安卓越科技(北京)有限公司 2 序列化的版本13.10 随机存取文件流RandomAccessFile可在程序的任何地方读取或写入数据安卓越科技(北京)有限公司 import java.io.BufferedInputStream;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import .URL;import .URLConnection;/*利用多线程下载文件的示例*/public class MultiThreadDownloadTest public static void main(String args)throws IOException String urlStr=http:/ url=new URL(urlStr);/创建URL URLConnection con=url.openConnection();/建立连接 intcontentLen=con.getContentLength();/获得资源总长度int threadQut=10;/线程数int subLen=contentLen/threadQut;/每个线程要下载的大小int remainder=contentLen%threadQut;/余数File destFile=new File(D:IOTestbjhyl.mp3);/目标文件/创建并启动线程 for(int i=0;i threadQut;i+)int start=subLen*i;/开始位置int end=start+subLen-1;/结束位置if(i=threadQut-1)/最后一个线程的结束位置end+=remainder;Thread t=new Thread(new DownloadRunnable(start,end,url,destFile);t.start();安卓越科技(北京)有限公司 DownloadRunnable implements Runnable private final int start;private final int end;private final URL srcURL;private final File destFile;public static final int BUFFER_SIZE=8192;/缓冲区大小 DownloadRunnable(int start,int end,URL srcURL,File destFile)this.start=start;this.end=end;this.srcURL=srcURL;this.destFile=destFile;public void run()System.out.println(Thread.currentThread().getName()+启动.);BufferedInputStream bis=null;RandomAccessFile ras=null;byte buf=new byteBUFFER_SIZE;/创建一个缓冲区 URLConnection con=null;try con=srcURL.openConnection();/创建网络连接 /设置连接的请求头字段:获取资源数据的范围从start到end con.setRequestProperty(Range,bytes=+start+-+end);/网络连接中获取输入流并包装成缓冲流 bis=new BufferedInputStream(con.getInputStream();安卓越科技(北京)有限公司 ras=new RandomAccessFile(destFile,rw);/创建RandomAccessFile ras.seek(start);/把文件指针移动到start位置 int len=-1;/读取到的字节数 while(len=bis.read(buf)!=-1)/从网络中读取数据 ras.write(buf,0,len);/用随机存取流写到目标文件 System.out.println(Thread.currentThread().getName()+已经下载完毕);catch(IOException e)e.printStackTrace();finally /关闭所有的IO流对象 if(ras!=null)try ras.close();catch(IOException e)e.printStackTrace();if(bis!=null)try bis.close();catch(IOException e)e.printStackTrace();安卓越科技(北京)有限公司13.11 ZIP文件流采用压缩方式存放文件