基本信息
文件名称:JAVA语言程序设计:流.ppt
文件大小:315 KB
总页数:10 页
更新时间:2025-03-31
总字数:约6.43千字
文档摘要

流1File类文件是相关记录或放在一起的数据的集合.Java程序一般通过java.io.File类对象,获取文件本身的一些信息,例如文件所在的目录、文件的长度、文件读写权限等,不涉及对文件的读写操作。1File类的构造方法File(Stringpathname)Filef=newFile(c:\\data\\temp.dat”);Filef=newFile(data\\temp.dat”);Filef=newFile(temp.dat”);File(Stringparent,Stringchild)Filef=newFile(c:\\data,temp.dat);Filef=newFile(data,temp.dat);File(Fileparent,Stringchild)Filef=newFile(newFile(c:\\data),temp.dat);Filef=newFile(newFile(data),temp.dat);1File类的常用方法File类对象既可以是文件对象,也可以是一个文件夹对象,以下是常用方法:booleanisFile()booleanisDirectory()booleanexists()StringgetName()longlength()StringgetAbsolutePath()StringgetCanonicalPath()booleancanRead()booleancanWrite()booleanisHidden()longlastModified()booleandelete()1File类File类的文件对象的常用的方法:booleancreateNewFile()File类的文件夹对象的常用的方法:booleanmkdir()booleanmkdirs()File[]listFiles()String[]list()Filefile=newFile(directory);file.mkdir();file=newFile(directory\\abc.txt);file.createNewFile();file.delete();1File类 System.out.println(请输入想创建的文件名:); StringfileName=newScanner(System.in).nextLine(); Filef1=newFile(fileName); if(f1.exists()){ if(f1.isFile()){ System.out.println(文件名:+f1.getName()); System.out.println(规范路径:+f1.getCanonicalPath()); System.out.println(文件大小:+f1.length()+字节); }else{ String[]s=f1.list(); System.out.println(文件夹+f1+共有+s.length+对象); for(inti=0;is.length;i++) System.out.println(s[i]); } }C:\ProgramFiles\Java文件夹Java共有2对象jre1.5.0_05jre6 publicstaticvoidlistChild(Filedir,intlevel){ StringBuffers=newStringBuffer(|--); for(inti=0;ilevel;i++){ s.insert(0,|); } File[]childs=dir.listFiles(); intlength=childs==null?0:childs.length; for(inti=0;ilength;i++){ if(childs[i].isDirectory()){ listChild(childs[i],level+1); } System.out.println(s.toString()+childs[i].getName()); } }2I/O流大部分程序都需要输入/输出处理,比如从键盘读取数据、向屏幕中输出数据