您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 输入输出流(带仓库系统作业)
1输入输出流概述File(文件类)FileInputStreamFileOutputStreamFileReader和FileWriterRandomAccessFilePipedInputStream/PipedOutputStreamDateInputStream/DateOutputStreamObjectInputStream/ObjectOutputStreamPrintStream/PrintWriter21.概述一个计算机的最简单模型由下列三个管道组成:输入,处理,输出。Java的I/O流库提供大量的流类(在包java.io中),其中,所有输入流类都是InputStream(抽象类)或抽象类Reader的子类,而所有输出流类都是OutputStream(抽象类)或抽象类Writer的子类32.文件(File类)File类不允许访问文件的内容,没有可用于访问文件的read()和write()方法.File类主要用于命名文件,查询文件属性和处理目录.42.1.创建文件对象构造方法:1.publicFile(Strings);(在Windows平台,分割符为“\”,在Linux/Unix上,为“/”,File提供的参数:File.separator)2.publicFile(StringDirectory,Strings);52.2查询文件属性File类提供了几个方法,查询文件属性:–文件是否存在–文件是否读保护–文件是否写保护–文件是否是一个目录–文件是否隐藏62.3查询文件属性Strings=e:+File.separator+Thread1.java;Filefile=newFile(s);Stringexists=file.exists()?Yes:No;StringcanRead=file.canRead()?Yes:No;StringcanWrite=file.canWrite()?Yes:No;StringisFile=file.isFile()?Yes:No;StringisHid=file.isHidden()?Yes:No;StringisDir=file.isDirectory()?Yes:No;StringisAbs=file.isAbsolute()?Yes:No;Attr.java73.FileInputStreamFileInputStream典型地表示一种顺序访问的文本文件。通过使用FileInputStream你可以访问文件的一个字节、几个字节或整个文件。由InputStream派生的类构造方法:FileInputStream(Stringname);//使用给定的文件名创建一个FileInputStream对象FileInputStream(Filefile);//使用File对象创建一个FileInputStream对象83.1使用FileInputStream读取文件使用构造方法来打开一个到达该文件的输入流:FileInputStreammyFileStream;myFileStream=newFileInputStream(“myfile.dat);或:FilemyFile;FileInputSteammyFileStream;myFile=newFile(myfile.dat);myFileStream=newFileInputStream(myFile);93.2处理I/O异常必须使用catch块检测并处理I/O异常(IOException),如:try{FileInputStreamins=newFileInputStream(“myfile.dat);}catch(IOExceptione){//文件I/O错误System.out.println(“Filereaderror:”+e);}103.3从FileInputStream中读出read()的成员函数:intread()//读取一个字节//到达输入流末尾时,返回-1intread(byteb[])//把多个字节读到字节数组中//到达输入流末尾时,返回-1intread(byteb[],intoff,intlen)//off指定read方法把数据存放在字节数组b中的什么地方。//len指定该方法将读取的最大字节数。//到达输入流末尾时,返回-1113.4关闭FileInputStream两种方法关闭:显式关闭和隐式关闭,隐式关闭是自动垃圾回收时的功能。显式关闭为:myFileStream.close();12intb;bytebuffer[]=newbyte[2500];try{Filef=newFile(E:\\lanhong\\,a.txt);FileInputStreamreadfile=newFileInputStream(f);b=readfile.read(buffer,0,2500);try{Stringstr=newString(buffer,0,b,Default);System.out.println(str);}catch(UnsupportedEncodingExceptione){System.out.println(theencodingwasnotfound:+e);}}catch(IOExceptione){System.out.println(FilereadError);}Example20_1.java134.FileOutputStreamFileOutputStream用于向一个文本文件写数据。由OutputStream派生的类144.1打开FileOutputStream和打开输入流FileInputStream类似:FileOutputStreammyFileStream;myFileStream=newFileOutputStream(“file.txt);或:FilemyFile;FileOutputSteammyFileStream;myFile=newFile(file.txt);myFileStream=newFileOutputStream(myFile);154.2写入一个流write()的成员函数:voidwrite(byteb[])//写b.length个字节到输出流voidwrite(byteb[],intoff,intlen)//b是数据,off是数据的起始偏移量,len是要输出的字节数16publicstaticvoidmain(Stringargs[]){intb;bytebuffer[]=newbyte[100];try{System.out.println(输入一行文本,并存入磁盘:);b=System.in.read(buffer);//把从键盘输入的字符存入bufferFileOutputStreamwritefile=newFileOutputStream(line.txt);writefile.write(buffer,0,b);//通过流把buffer写入到文件line.txt中}catch(IOExceptione){System.out.println(Error);}}Example20_3.java175.FileReader和FileWriter与FileInputStream和FileOutputStream等价的读取器是FileReader类和FileWriter类。它们分别是Reader和Writer的子类。构造方法分别是:FileReader(Stringfilename);FileWriter(Stringfilename);如:FileReaderfile=newFileReader(“Student.txt”);不能按行读取或写入185.1BufferedReaderFileReader类不能读取一行,Java提供了BufferedReader类。构造方法是:BufferedReader(Readerin);读取文本行的方法是:readLine();如:BufferedReaderin=BufferedReader(newFileReader(“Student.txt”));19P261,例4Example20_4TextAreatext;BufferedReaderin;Buttonbutton;FileReaderfile;EWindow(){super(流的读取);text=newTextArea(10,10);text.setBackground(Color.cyan);try{Filef=newFile(E:\\lanhong\\,a.txt);file=newFileReader(f);in=newBufferedReader(file);}catch(FileNotFoundExceptione){}catch(IOExceptione){}button=newButton(读取);button.addActionListener(this);20setLayout(newBorderLayout());setSize(40,40);setVisible(true);add(text,Center);add(button,South);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidactionPerformed(ActionEvente){Strings;if(e.getSource()==button){try{while((s=in.readLine())!=null)text.append(s);}catch(IOExceptionexp){}}}publicclassExample20_4{publicstaticvoidmain(Stringargs[]){EWindoww=newEWindow();w.pack();}}215.2BufferedWriter与BufferedReader类相对应的是BufferedWriter类。构造方法是:BufferedWriter(Writerout);写入文件的方法是:write(Strings,intoff,intlen);如:BufferedWriterout=BufferedWriter(newFileReader(“hello.txt”));out.write(“howareyou”,0,s.length());//写入缓冲区out.flush();//要写入文件,必须执行22P263,例5Example20_5TextAreatext;BufferedWriterout;Buttonbutton;FileWritertofile;FWindow(){super(流的写入);text=newTextArea(10,10);text.setBackground(Color.cyan);try{tofile=newFileWriter(hello.txt);out=newBufferedWriter(tofile);}catch(FileNotFoundExceptione){}catch(IOExceptione){}button=newButton(写入);button.addActionListener(this);setLayout(newBorderLayout());setSize(60,70);setVisible(true);add(text,Center);add(button,South);23addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){setVisible(fal
本文标题:输入输出流(带仓库系统作业)
链接地址:https://www.777doc.com/doc-1249453 .html