Java语言的输入输出流.ppt_第1页
Java语言的输入输出流.ppt_第2页
Java语言的输入输出流.ppt_第3页
Java语言的输入输出流.ppt_第4页
Java语言的输入输出流.ppt_第5页
已阅读5页,还剩67页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第10章Java语言的输入输出与文件处理,本章主要内容,流的概念处理字节流的基本类:InputStream和OutputStream处理字符流的基本类:Reader和Writer标准输入输出文件管理类:File,10.1Java语言的输入输出类库,流(stream)是指计算机各部件之间的数据流动。流是由位或字符组合而成的序列。分类:传输方向:输入流与输出流内容上:字符流和字节流,流用来顺序地读写数据信息,它是一个单向的数据通道。输入流从某个外部的数据源向程序输入数据输出流从程序向外部数据目标输出数据,内存,文件,输入流InputStream(字节流)Reader(字符流),输出流OutputStream(字节流)Writer(字符流),10.2输入输出流类库,Java语言的流类都封装在java.io包中。字节流每次读写8位(一个字节)的二进制数,一般为二进制文件。字符流每次读写16位二进制数,并将其作为一个字符而不是二进制为来处理。可以表示任何的文本信息。,处理字节流:InputStream和OutputStream处理字符流:Reader和Writer,输入流(InputStream)和输出流(OutputStream)构成字节流的祖先,用来读写如图片、声音之类的二进制文件。这两个类直接继承了Object类。InputStream和OutputStream都是抽象类,InputStream为其他所有字节输入流的超类,而OutputStream为其他所有字节输出流的超类。,读取器(Reader)和写出器(Writer)是所有字符流的超类,它们是直接继承Object类的抽象类。用来处理纯文本文件。Reader和Writer可读写16位的字符流。作为抽象类,Reader和Writer必须被子类化才能实现字符流的读写工作。,10.2使用InputStream和OutputStream,InputStream的方法:publicintread()throwsIOException从输入流读取下一个字节,字节值为0-255,如果输入流中不再有数据,返回-1表示输入结束。这是一个阻塞方法,直到有数据可读,或数据流结束,或发生异常时才返回。这是一个抽象方法,需要在具体的子类中加以实现。publicintread(byteb)throwsIOException从输入流读取一组数据存入缓冲区b中。返回读取字节个数,如果达到数据流结束返回-1。publicintread(byteb,intoff,intlen)throwsIOException从输入流读取最多len字节数据存入缓冲区b中,第一个字节存储在boff,随后的按序摆放。返回读取字节个数,如果至数据流结束返回-1。,publicintavailable()throwsIOException返回输入流中无需阻塞可直接读取字节个数。publicvoidmark(intreadlimit)throwsIOException在输入流中标记当前位置,以后可调用reset方法返回该位置,这样可以重复读取标记以后的数据。publicvoidreset()throwsIOException重置流的读取位置,回到上次调用mark方法标记的位置。publiclongskip(longn)throwsIOException从输入流中忽略n个字节的数据,返回值为实际忽略的字节个数。publicvoidclose()throwsIOException关闭输入流,释放占用的系统资源。,流中方法都声明抛出异常,在程序中调用流方法时必须处理异常,否则编译不能通过,在OutputStrem中定义了5个方法:publicabstractvoidwrite(intb)throwsIOException向输出流写入一个字节。写出字节为整数b的低字节,整数b的3个高字节被忽略。这是一个抽象方法,需要在具体的子类中加以实现。publicvoidwrite(byteb)throwsIOException把缓冲区b中的全部数据写入输出流。publicvoidwrite(byteb,intoff,intlen)throwsIOException把缓冲区b中从boff开始的len个字节的数据写入输出流。publicvoidflush()throwsIOException刷新输出流,强制输出缓冲区的数据立即写出。publicvoidclose()throwsIOException关闭输出流。,文件输入输出流,文件读写是最常见的I/O操作,通过文件流来连接磁盘文件,读写文件内容是一件很轻松的工作。文件输入流(FileInputStream)和文件输出流(FileOutputStream)是抽象类InputStream和OutputStream的具体子类,实现了文件的读写。文件的读写工作包括3个步骤:打开文件输入流或输出流文件读或写操作关闭文件输入流或输出流,FileInputStream实现读文件,调用FileInputStream的构建器可以打开一个文件输入流。FileInputStream的有3个构建器:publicFileInputStream(StringfileName)throwsFileNotFoundExceptionpublicFileInputStream(Filefile)throwsFileNotFoundExceptionpublicFileInputStream(FileDescriptorfdObj)第一个方法指定文件名,第二个方法指定一个File对象。第三个方法需要指定一个文件描述符对象(FileDescriptor)。如果我们试图在一个不存在的文件上打开一个文件输入流,FileInputStream的构建器会抛出异常FileNotFoundException,它是IOException的一个子类,程序应该捕捉这个异常,或者也可以捕捉IOException。,实例,读取文件(文件字节输入流,目的:使用FileInputStream类)用FileInputStream的对象把文件读入到内存。,importjava.io.*;publicclasstest23publicstaticvoidmain(Stringargs)FileInputStreamfin=null;tryfin=newFileInputStream(d:a.txt);intn=0;/while(n=fin.read()!=-1)/System.out.print(char)n);byteb=newbyte12;while(n=fin.read(b)!=-1)Strings=newString(b,0,n);System.out.println(s);catch(IOExceptione)System.out.println(e.getMessage();finallytryif(fin!=null)fin.close();catch(IOExceptione)System.out.println(e.getMessage();,importjava.io.*;publicclassFileInputS2publicstaticvoidmain(Stringargs)throwsIOExceptionFileInputStreamfin=newFileInputStream(d:/c.txt);byteb=newbytefin.available();intn=fin.read(b);Strings=newString(b,0,n);System.out.print(s);fin.close();,和FileInputStream对应的文件输出流FileOutputStream实现了文件输出功能,它的构建器如下:publicFileOutputStream(Stringname)throwsFileNotFoundExceptionpublicFileOutputStream(Stringname,booleanappend)throwsFileNotFoundExceptionpublicFileOutputStream(Filefile)throwsFileNotFoundExceptionpublicFileOutputStream(FileDescriptorfdObj)throwsFileNotFoundException调用第一个构建器时,如果name指定的文件不存在,将创建该文件,并同时建立一个输出流;如果name指定的文件已存在,该文件的内容将会被覆盖。在第二个构建器中,我们可以通过第二个参数append指定是否对已存在的文件进行覆盖,如果append为true,将对文件进行追加写,即新输出的内容将添加到文件尾端,如果append为false,则覆盖原文件。,importjava.io.*;publicclasstest29publicstaticvoidmain(Stringargs)FileOutputStreamfout=null;tryfout=newFileOutputStream(d:a.txt);Strings=hellojava1123;fout.write(s.getBytes();catch(IOExceptione)System.out.println(e.getMessage();finallytryif(fout!=null)fout.close();catch(IOExceptione)System.out.println(e.getMessage();,mportjava.io.*;publicclassFileOUtpublicstaticvoidmain(Stringargs)throwsIOExceptionFileOutputStreamfout=newFileOutputStream(d:123.txt,true);Strings1=hellojava;Strings2=你好!;fout.write(s1.getBytes();fout.write(s2.getBytes();fout.close();,二进制图形文件的复制,importjava.io.*;publicclassapp10_2publicstaticvoidmain(Stringargs)FileInputStreamfin=null;FileOutputStreamfout=null;tryfin=newFileInputStream(d:a1.jpg);fout=newFileOutputStream(d:a2.jpg);System.out.println(文件的大小=+fin.available();byteb=newbytefin.available();fin.read(b);fout.write(b);System.out.println(文件已被复制并更名);catch(FileNotFoundExceptione)e.printStackTrace();catch(IOExceptione)e.printStackTrace();finallytryif(fin!=null)fin.close();if(fout!=null)fout.close();catch(IOExceptione)e.printStackTrace();,习题:通过键盘输入学号,姓名,年龄,利用FileOutputStream类把以上信息写入“student.txt”文件中,再复制文件生成“student2.txt”习题:通过键盘输入学号,姓名,年龄,利用DataOutputStream类把以上信息写入“student.txt”文件中,再复制文件生成“student2.txt”,importjava.io.*;importjava.util.*;publicclassstudatapublicstaticvoidmain(Stringargs)throwsIOExceptionScannerreader=newScanner(System.in);System.out.println(请输入姓名、学号、年龄);Stringname=reader.next();Stringnum=reader.next();Stringage=reader.next();FileOutputStreamfout=newFileOutputStream(d:student.txt);fout.write(name.getBytes();fout.write(t);fout.write(num.getBytes();fout.write(t);fout.write(age.getBytes();fout.close();FileInputStreamfin=newFileInputStream(d:student.txt);fout=newFileOutputStream(d:student2.txt);byteb=newbytefin.available();fin.read(b);fout.write(b);fin.close();fout.close();Runtime.getRuntime().exec(“notepad.exe”+“d:student2.txt”);/运行可执行文件,过滤输入输出流,Java的流按使用方式可以分为两类,一类是建立了程序和其他数据源或数据目标的数据通道,程序通过这类流可以和流的另一端的数据源或目标进行数据交互,这类数据流称为节点流(nodestream)。例如文件输入流FileInputStream和文件输出流FileOutputStream,在它们的另一端是磁盘文件。另一类流,本身并不和具体的数据源和数据目标连接,它们连接在其他输入或输出流上,提供各种数据处理,诸如转换、缓存、加密、压缩等功能,这类流称为过滤器流(filterstream)。过滤器输入流从已存在的输入流(比如FileInputStrem)读取数据,对数据进行适当的处理和改变然后再送入程序。过滤器输出流向已存在的输出流(比如FilterOutputSteam)写入数据,在数据抵达底层流之前进行转换处理等工作。,特点:过滤输入输出流是建立在基本输入输出流之上,并在输入、输出数据的同时能对所传输的数据做指定类型或格式的转换,即可实现对二进制字节数据的理解和编码的转换。,类的层次图,过滤输入流类FilterInputStream和过滤输出流类FilterOutputStream,两者都为抽象类,Java语言中按照基本数据类型进行读写就是数据输入流DataInputStream和数据输出流,DataOutputStream,将基本字节输入输出流,自动转换成按基本数据类型进行读写的过滤流,然后利用流串联的方式,以达到数据转换的目的其分别实现了DataInput和DataOutput两个接口,DataInputStream继承了类FilterInputStream,并且实现了DataInput接口。在类DataInputStream中提供的read方法:booleanreadBoolean()从输入流读取一个字节。如果字节值非0,返回布尔值true;否则返回false。bytereadByte()读取一个有符号的字节,字节值范围:-128至127。charreadChar()读取一个字符,一个Unicode字符由两个字节构成。doublereadDouble()读取8个字节,并返回一个double型值。floatreadFloat()读取4个字节,并返回一个float型值。intreadInt()读取4个字节,并返回一个int型值。longreadLong()读取8个字节,并返回一个long型值。shortreadShort()读取2个字节,并返回一个short型值。,DataOutputStream继承了类FilterOutputStream,并且实现了DataOutput接口。在类DataOutputStream中提供的write方法:voidwriteBoolean(booleanv)写出一个boolean值,如果v值为true,则写出1;v值为false,则写出0。voidwriteChar(intv)输出一个字符,把v的低16位写到输出流,高16位被丢弃。voidwriteChars(Strings)把字符串s写到输出流,字符串s的每个字符对应2个输出字节。voidwriteDouble(doublev)输出一个double,对应于8个字节。voidwriteFloat(floatv)输出一个float,对应于4个字节。voidwriteInt(intv)输出一个int,对应于4个字节。voidwriteLong(longv)输出一个long,对应于8个字节。voidwriteShort(intv)输出一个short,对应于2个字节。,DataInputStream(InputStreamin),建立一个新的数据输入流,从指定的输入流in读数据DataOutputStream(OutputStreamout)建立一个新的数据输出流,从指定的输入流out读数据,importjava.io.*;publicclassapp10_4publicstaticvoidmain(Stringargs)FileOutputStreamfout;DataOutputStreamdout;FileInputStreamfin;DataInputStreamdin;try/dout=newDataOutputStrem(newFileOutpustream(“d:temp”);fout=newFileOutputStream(d:temp);dout=newDataOutputStream(fout);dout.writeInt(10);dout.writeLong(12345);dout.writeFloat(3.14159267f);dout.writeDouble(987654321.123);dout.writeBoolean(true);dout.writeChars(Goodbye!);dout.close();catch(IOExceptione),tryfin=newFileInputStream(d:temp);din=newDataInputStream(fin);System.out.println(din.readInt();System.out.println(din.readLong();System.out.println(din.readFloat();System.out.println(din.readDouble();System.out.println(din.readBoolean();/System.out.println(din.readChars();charch;while(ch=din.readChar()!=0)System.out.print(ch);din.close();catch(FileNotFoundExceptione)System.out.println(文件未找到!);catch(IOExceptione),importjava.io.*;publicclassStudentDatapublicstaticvoidmain(Stringargs)throwsIOExceptionStringfileName=d:student.dat;Stringstudents=ZhangSan,LiSi;intages=10,9;DataOutputStreamdout=newDataOutputStream(newFileOutputStream(fileName);/Tab符用来分隔字段for(inti=0;i2;i+)dout.writeChars(studentsi);dout.writeChar(t);dout.writeInt(agesi);dout.writeChar(t);dout.close();DataInputStreamdin=newDataInputStream(newFileInputStream(fileName);for(inti=0;i2;i+)StringBuffername=newStringBuffer();charchRead;/遇到Tab符结束String字段读取while(chRead=din.readChar()!=t)name.append(chRead);intage=din.readInt();din.readChar();/丢弃分隔符System.out.println(Theageofstudent+name+is+age+.);din.close();,写出程序的运行结果,写上注释?,标准输入输出,Java语言包中有一个类System,这个类没有共有的构建器,因此我们并不能创建这个类的对象实例,不它提供了3个有用的静态类字段:publicfinalstaticInputStreamin;publicfinalstaticPrintStreamout;publicfinalstaticPrintStreamerr;,标准输入输出,System类的这三个静态字段就是系统的标准流:System.in表示系统标准输入流,通常环境下标准输入流指向键盘输入;System.in.read()该方法从键盘缓存区读入一个字节的二进制数据。Charc=(char)System.in.read();System.out表示系统标准输出流,通常环境下标准输出流指向屏幕输出;System.out.print()System.out.println()System.err表示系统标准错误输出流,通常环境下标准错误输出流也指向屏幕输出。System.err.print()System.err.println()在程序中,这3个标准流不需我们要显式地打开就可以直接使用,其实它们由系统负责打开。对于标准流如果程序不再使用,也不需要关闭。,System.out和System.err都用于输出,通常情况下System.out用于程序输出一般信息,而System.err用于输出错误信息和其他需要引起用户立即注意的信息。在系统中,System.out具有缓存机制,而System.err是没有缓存的,这是两者之间的一个重要区别。,System.err.print(e);,importjava.io.*;publicclassapp10_5publicstaticvoidmain(Stringargs)trybyteb=newbyte128;System.out.print(请输入字符:);intcount=System.in.read(b);/读取标准输入流System.out.println(输入的是:);for(inti=0;icount;i+)System.out.print(bi+“”);/System.out.println();for(inti=0;icount-2;i+)System.out.print(char)bi+“”);/System.out.println();System.out.println(输入的字符个数为+count);ClassInClass=System.in.getClass();ClassOutClass=System.out.getClass();System.out.println(in所在的类是:+InClass.toString();System.out.println(out所在的类是:+OutClass.toString();catch(IOExceptione)e.printStackTrace();,importjava.io.*;publicclassEchopublicstaticvoidmain(Stringargs)intline=newint1024;intcount=0;booleandone=false;while(!done)tryintread=System.in.read();if(read=r)elseif(read=n)for(inti=0;icount;i+)System.out.write(linei);System.out.write(n);count=0;elseif(read=.程序的功能是什么?,10.3使用Reader和Writer流类,Reader和Writer类是用来处理“字符流”的。这两个类是抽象类,只是提供了一系列用于字符流处理的接口,不能生成这两个类的实例,只能通过使用由它们派生出来的子类对象来处理字符流。Reader和Writer要解决的,最主要的问题就是国际化。原先的I/O类库只支持8位的字节流,因此不可能很好地处理16位的Unicode字符流。Unicode是国际化的字符集(更何况Java内置的char就是16位的Unicode字符),这样加了Reader和Writer之后,所有的I/O就都支持Unicode了。,Reader-提供的操作,intread()throwsIOException;读取一个字符,返回值为读取的字符intread(charcbuf)throwsIOException;读取一系列字符到数组cbuf中,返回值为实际读取的字符的数量abstractintread(charcbuf,intoff,intlen)throwsIOException;读取len个字符,从数组cbuf的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现booleanmarkSupported();判断当前流是否支持做标记voidmark(intreadAheadLimit)throwsIOException;给当前流作标记,最多支持readAheadLimit个字符的回溯。voidreset()throwsIOException;将当前流重置到做标记处abstractvoidclose()throwsIOException;关闭,Writer提供的操作,voidwrite(intc)throwsIOException;将整型值c的低16位写入输出流voidwrite(Stringstr)throwsIOException;将字符串str中的字符写入输出流voidwrite(charcbuf)throwsIOException;将字符数组cbuf写入输出流abstractvoidwrite(charcbuf,intoff,intlen)throwsIOException;将字符数组cbuf中的从索引为off的位置处开始的len个字符写入输出流voidwrite(Stringstr,intoff,intlen)throwsIOException;将字符串str中从索引off开始处的len个字符写入输出流flush()刷空输出流,并输出所有被缓存的字节。close()关闭流,例子:FileRead.java,InputStreamReader/OutputStreamWriter,InputStreamReader类是将字节转换为字符的桥梁,且可以在构造器中指定编码的方式,默认使用操作系统的编码方式.常用方法:voidclose():关闭当前流StringgetEncoding():返回此流使用的字符编码的名称intread():读取单个字符intread(charcbuf,intoffset,intlength):将字符读入数组中的某一部分。booleanready():告知是否准备读取此流,OutputStreamWriter是将字符流通过字节流的桥梁,使用指定的charset将写入的字符编码转换为字节输出。可以指定编码方式,默认接受平台的字符集。常用方法voidclose():关闭当前流StringgetEncoding():返回此流使用的字符编码的名称intwrite(intc):写入单个字符intwrite(charcbuf,intoffset,intlength):写入cbuf数组中从offset开始长度为length的字符。booleanflush():刷新流的缓冲,InputStreamReader/OutputStreamWriter,FileReader/FileWriter,FileReader类继承了InputStreamReader,除了父类的方法外,它还有自己的方法可以直接读取文件内容。构造函数:publicFileReader(StringfileName)throwsFileNotFoundExceptionpublicFileReader(Filefile)throwsFileNotFoundExceptionpublicFileReader(FileDescriptorfd),importjava.io.*;publicclassapp10_6publicstaticvoidmain(Stringargs)throwsIOExceptionFileReaderfr=newFileReader(d:a.txt);charc=newchar512;intnum=fr.read(c);Strings=newString(c,0,num);System.out.println(读取的字符个数为+num+,其内容如下:);System.out.println(s);,FileWriter类继承了OutputStreamWriter,除了父类的方法外,它还有自己的方法可以直接写字符内容到文件。构造函数publicFileWriter(StringfileName)throwsIOExceptionpublicFileWriter(StringfileName,booleanappend)throwsIOExceptionpublicFileWriter(Filefile)throwsIOExceptionpublicFileWriter(Filefile,booleanappend)throwsIOException,FileReader/FileWriter,importjava.io.*;publicclassapp10_7publicstaticvoidmain(Stringargs)throwsIOExceptionFileWriterfw=newFileWriter(d:c.txt);charc=h,e,l,l,o,r,n;Stringstr=欢迎使用Java!;fw.write(c);fw.write(str);fw.close();,复制,importjava.io.*;publicclasscopyfpublicstaticvoidmain(Stringargs)throwsIOExceptionFileReaderfr=newFileReader(d:c.txt);FileWriterfw=newFileWriter(e:d.txt);charc=newchar500;intn=fr.read(c);fw.write(c);fr.close();fw.close();,10.3.4使用BufferedReader类读取文件,BufferedReader的父类为Reader。BufferedReader类用来读取缓冲区里的数据。BufferedReader使用之前,需先创建FileReader类对象,以该对象为参数来创建BufferedReader类的对象。构造方法publicBufferedReader(Readerin,intsz)创建一个使用指定大小输入缓冲区的缓冲字符输入流。publicBufferedReader(Readerin)创建一个使用默认大小输入缓冲区的缓冲字符输入流。,BufferedReader类的常用方法,publicintread()throwsIOException读取单个字符。publicintread(charcbuf)throwsIOException将字符读入数组。在某个输入可用、发生I/O错误或者已到达流的末尾前,此方法一直阻塞。publicintread(charcbuf,intoff,intlen)throwsIOException将字符读入数组的某一部分。,publicStringreadLine()throwsIOException读取一个文本行。通过下列字符之一即可认为某行已终止:换行(n)、回车(r)或回车后直接跟着换行。publiclongskip(longn)throwsIOException跳过字符。publicvoidclose()throwsIOException,importjava.io.*;publicclassapp3_4publicstaticvoidmain(Stringargs)throwsIOExceptionfloatnum;Stringstr;BufferedReaderbuf=newBufferedReader(newInputStreamReader(System.in);System.out.print(请输入一个实数);str=buf.readLine();num=Float.parseFloat(str);System.out.println(您输入的数是:+num);,importjava.io.*;publicclassapp10_8publicstaticvoidmain(Stringargs)StringthisLine;intcount=0;tryFileReaderfr=newFileReader(d:c.txt);BufferedReaderbfr=newBufferedReader(fr);while(thisLine=bfr.readLine()!=null)count+;System.out.println(thisLine);System.out.println(共读取了+count+行。);bfr.close();catch(IOExceptione)System.out.println(错误+e);,使用BufferedWriter类写入文件,BufferedWriter的父类为WriterBufferedWriter类用来将数据写入到缓冲区里首先创建FileWriter对象,再以该对象为参数来创建BufferedWriter类的对象。特点:缓冲区内的数据最后必须用flush()方法将缓冲区清空,也就是将缓冲区中的数据全部写到文件里。,构造方法publicBufferedWriter(Writerout)创建一个使用默认大小输出缓冲区的缓冲字符输出流。publicBufferedWriter(Writerout,intsz)创建一个使用给定大小输出缓冲区的新缓冲字符输出流。,常用方法publicvoidwrite(intc)throwsIOException写入单个字符。publicvoidwrite(charcbuf,intoff,intlen)throwsIOException写入字符数组的某一部分。publicvoidwrite(Strings,intoff,intlen)throwsIOException写入字符串的某一部分。publicvoidnewLine()throwsIOException写入一个行分隔符。行分隔符字符串由系统属性line.separator定义,并且不一定是单个新行(n)符。publicvoidflush()throwsIOException刷新该流的缓冲。publicvoidclose()throwsIOException,importjava.io.*;publicclassBuffedWriterpublicstaticvoidmain(Stringargs)throwsIOExceptionBufferedWriterbfw=newBufferedWriter(newFileWriter(d:d.txt);Strings1=Java,我会好好研究你的!;Strings2=学习的乐趣,要自己去发掘!;bfw.write(s1);bfw.newLine();bfw.write(s2);bfw.flush();bfw.close();,importjava.io.*;publicclassapp10_9publicstaticvoidmain(Stringargs)Stringstr=newString();tryBufferedReaderin=newBufferedReader(newFileReader(d:d.txt);BufferedWriterout=newBufferedWriter(newFileWriter(d:d1.txt);while(str=in.readLine()!=null)System.out.println(str);out.write(str);out.newLine();out.flush();in.close();out.close();catch(IOExceptione)System.out.println(错误!+e);,习题、将一个千千静听中的歌词文件(.lrc)作为数据源,按行读入,并将每行歌词前的时间格式字符去掉(如:00:00.62),将歌名和歌词按行写入一个新文件中,文件名和歌词文件名相同,后缀名为txt;,10.4文件的处理与随机访问,10.4.1Java语言对文件与文件夹的管理Java.io包中定义了File类专门用来管理磁盘文件和文件夹,而不负责数据的输入输出。每个File类对象表示一个磁盘文件或文件夹,其对象属性中包含了文件或文件夹的相关信息,如文件名、长度、和所含文件个数等,调用它的方法可以完成对文件或文件夹的管理操作。,File的基本用法,File给出的构造函数publicFile(Stringpath)如果path是实际存在的路径,则该File对象表示的是目录如果path是文件名,则该File对象表示的是文件publicFile(Stringpath,Stringname)path是路径名,name是文件名publicFile(Filedir,Stringname)dir是路径名,name是文件名,文件名的处理StringgetName();/得到一个文件的名称(不包括路径)StringgetPath();/得到一个文件的路径名StringgetAbsolutePath();/得到一个文件的绝对路径名StringgetParent();/得到一个文件的上一级目录名文件属性测试booleanexists();/测试当前File对象所指示的文件是否存在booleancanWrite();/测试当前文件是否可写booleancanRead();/测试当前文件是否可读booleanisFile();/测试当前文件是否是文件(不是目录)booleanisDirectory();/测试当前文件是否是目录,文件属性,importjava.io.*;publicclassDemo9publicstaticvoidmain(Stringargs)Filef1=newFile(d:a.txt);/创建File类对象System.out.println(文件路径+f1.getAbsoluteFile();/得到文件的绝对路径System.out.println(“文件长度+f1.length();/得到文件的大小File不负责创建文件,创建文件和文件夹,importjava.io.*;publicclassDemo0publicstaticvoidmain(Stringargs)Filef=newFile(d:hello.txt);if(!f.exists()tryf.createNewFile();/创建文件catch(IOExceptione)e.printStackTrace();elseSystem.out.println(有文件,不能创建);,importjava.io.*;publicclassDemo11

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论