收藏 分销(赏)

输入输出流带仓库系统作业.pptx

上传人:人****来 文档编号:10252690 上传时间:2025-04-30 格式:PPTX 页数:45 大小:193.19KB
下载 相关 举报
输入输出流带仓库系统作业.pptx_第1页
第1页 / 共45页
输入输出流带仓库系统作业.pptx_第2页
第2页 / 共45页
点击查看更多>>
资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,*,Click to edit Master title style,Click to edit Master text styles,Second Level,Third Level,Fourth Level,Fifth Level,*,Click to edit Master title style,Click to edit Master text styles,Second Level,Third Level,Fourth Level,Fifth Level,1,输入输出流,概述,File(,文件类),FileInputStream,FileOutputStream,FileReader,和,FileWriter,RandomAccessFile,PipedInputStream/PipedOutputStream,DateInputStream/DateOutputStream,ObjectInputStream/ObjectOutputStream,PrintStream/PrintWriter,第1页,1.,概述,一个计算机最简单模型由以下三个管道组成:输入,处理,输出。,Java,I/O,流库提供大量流类(在包,java.io,中),其中,全部输入流类都是,InputStream(,抽象类)或抽象类,Reader,子类,而全部输出流类都是,OutputStream(,抽象类)或抽象类,Writer,子类,第2页,2.,文件,(File,类,),File,类不允许访问文件内容,没有可用于访问文件,read(),和,write(),方法,.,File,类主要用于命名文件,查询文件属性和处理目录,.,第3页,2.1.,创建文件对象,结构方法,:,1.public File(String s);,(,在,Windows,平台,分割符为“”,在,Linux/Unix,上,为“/”,File,提供参数:,File.separator,),2.public File(String Directory,String s);,第4页,2.2,查询文件属性,File,类提供了几个方法,查询文件属性,:,文件是否存在,文件是否读保护,文件是否写保护,文件是否是一个目录,文件是否隐藏,第5页,2.3,查询文件属性,String s=e:+File.separator+Thread1.java;,File file=new File(s);,String exists=file.exists()?Yes:No;,String canRead=file.canRead()?Yes:No;,String canWrite=file.canWrite()?Yes:No;,String isFile=file.isFile()?Yes:No;,String isHid=file.isHidden()?Yes:No;,String isDir=file.isDirectory()?Yes:No;,String isAbs=file.isAbsolute()?Yes:No;,Attr.java,第6页,3.FileInputStream,FileInputStream,典 型 地 表 示 一 种 顺 序 访 问 文 本 文 件。经过 使 用,FileInputStream,你 可 以 访 问 文 件 一 个 字 节、几 个 字 节或 整 个 文 件。,由,InputStream,派生类,结构方法:,FileInputStream(String name);,/,使用给定文件名创建一个,FileInputStream,对象,FileInputStream(File file);,/,使用,File,对象创建一个,FileInputStream,对象,第7页,3.1,使用,FileInputStream,读取文件,使用结构方法来打开一个抵达该文件输入流:,FileInputStream myFileStream;,myFileStream=new FileInputStream(“myfile.dat);,或,:,File myFile;FileInputSteam myFileStream;,myFile=new File(myfile.dat);,myFileStream=new FileInputStream(myFile);,第8页,3.2,处理,I/O,异常,必须使用,catch,块检测并处理,I/O,异常(,IOException),如:,try,FileInputStream ins=,new FileInputStream(“myfile.dat);,catch(IOException e),/,文件,I/O,错误,System.out.println(“File read error:”+e);,第9页,3.3,从,FileInputStream,中读出,read(),组员函数,:,int read(),/,读取一个字节,/,抵达输入流末尾时,返回-1,int read(byte b),/,把多个字节读到字节数组中,/,抵达输入流末尾时,返回-1,int read(byte b,int off,int len),/off,指定,read,方法把数据存放在字节数组,b,中什么地方。,/len,指定该方法将读取最大字节数。,/,抵达输入流末尾时,返回-1,第10页,3.4,关闭,FileInputStream,两 种 方 法 关 闭:,显式 关 闭 和 隐 式 关 闭,隐 式 关 闭 是 自 动 垃 圾 回 收 时 功 能。显 式 关 闭 为:,myFileStream.close();,第11页,int b;byte buffer=new byte2500;,try File f=new File(E:lanhong,a.txt);,FileInputStream readfile=new,FileInputStream(f);,b=readfile.read(buffer,0,2500);,try String str=new,String(buffer,0,b,Default);,System.out.println(str);,catch(UnsupportedEncodingException e),System.out.println(the encoding was not found:+e);,catch(IOException e),System.out.println(File read Error);,Example20_1.java,第12页,4.FileOutputStream,FileOutputStream,用 于 向 一 个 文 本 文 件 写 数 据。,由,OutputStream,派生类,第13页,4.1,打开,FileOutputStream,和 打 开 输 入 流,FileInputStream,类似,:,FileOutput Stream myFileStream;,myFileStream=new FileOutputStream(,“file.txt);,或,:File myFile;FileOutputSteam myFileStream;,myFile=new File(file.txt);,myFileStream=new FileOutputStream(myFile);,第14页,4.2,写入一个流,write(),组员函数,:,void write(byte b),/,写,b.length,个字节到输出流,void write(byte b,int off,int len),/b,是数据,,off,是数据起始偏移量,,len,是,要输出字节数,第15页,public static void main(String args),int b;byte buffer=new byte100;,try,System.out.println,(,输入一行文本,并存入磁盘:);,b=,System.in.read,(buffer);/,把从键盘输入字符存入,buffer,FileOutputStream writefile=new FileOutputStream(line.txt),;,writefile.write,(buffer,0,b);/,经过流把,buffer,写入到文件,line.txt,中,catch(IOException e),System.out.println(Error);,Example20_3.java,第16页,5.FileReader,和,FileWriter,与,FileInputStream,和,FileOutputStream,等价读取器是,FileReader,类和,FileWriter,类。它们分别是,Reader,和,Writer,子类。,结构方法分别是:,FileReader(String filename);,FileWriter(String filename);,如:,FileReader file=new FileReader(“Student.txt”);,不能按行读取或写入,第17页,5.1 BufferedReader,FileReader,类不能读取一行,,Java,提供了,BufferedReader,类。,结构方法是:,BufferedReader(Reader in);,读取文本行方法是:,readLine();,如:,BufferedReader in=BufferedReader(new FileReader(“Student.txt”);,第18页,P261,例4,Example20_4,TextArea text;,BufferedReader in,;Button button;,FileReader file,;,EWindow(),super(,流读取);,text=new TextArea(10,10);,text.setBackground(Color.cyan);,try,File f=new File(E:lanhong,a.txt);,file=new FileReader(f);,in=new BufferedReader(file);,catch(FileNotFoundException e),catch(IOException e),button=new Button(,读取);,button.addActionListener(this);,第19页,setLayout(new BorderLayout();,setSize(40,40);,setVisible(true);,add(text,Center);add(button,South);,addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););,public void actionPerformed(ActionEvent e),String s;,if(e.getSource()=button),try while(s=in.readLine()!=null)text.append(s);,catch(IOException exp),public class Example20_4,public static void main(String args),EWindow w=new EWindow();w.pack();,第20页,5.2 BufferedWriter,与,BufferedReader,类相对应是,BufferedWriter,类。,结构方法是:,BufferedWriter(Writer out);,写入文件方法是:,write(String s,int off,int len);,如:,BufferedWriter out=BufferedWriter(new,FileReader(“hello.txt”);,out.write(“how are you”,0,s.length();/,写入缓冲区,out.flush();,/,要写入文件,必须执行,第21页,P263,例5,Example20_5,TextArea text;,BufferedWriter out,;Button button;,FileWriter tofile;,FWindow(),super(,流写入);,text=new TextArea(10,10);,text.setBackground(Color.cyan);,try,tofile=new FileWriter(hello.txt);,out=new BufferedWriter(tofile);,catch(FileNotFoundException e),catch(IOException e),button=new Button(,写入);,button.addActionListener(this);,setLayout(new BorderLayout();setSize(60,70);setVisible(true);add(text,Center);add(button,South);,第22页,addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e),setVisible(false),;System.exit(0););,public void actionPerformed(ActionEvent e),String s;,if(e.getSource()=button),try,out.write(text.getText(),0,(text.getText().length();,out.flush();,catch(IOException exp),public class Example20_5,public static void main(String args),FWindow w=new FWindow();,w.pack();,第23页,P267,例7,Example20_7,class Frame_FileDialog extends Frame implements ActionListener,FileDialog filedialog_save,filedialog_load;,/,申明两个文件对话框,MenuBar menubar1;Menu menu1;MenuItem item1,item2;TextArea text;,BufferedReader in;,FileReader file_reader;BufferedWriter out;FileWriter tofile;,Frame_FileDialog(),super(,带文件对话框窗口);,setSize(60,70);setVisible(true);menubar1=new MenuBar();,menu1=new Menu(,文件);,item1=new MenuItem(,打开文件);,item2=new MenuItem(,保留文件);,item1.addActionListener(this);,item2.addActionListener(this);,menu1.add(item1);menu1.add(item2);menubar1.add(menu1);,setMenuBar(menubar1);,第24页,/下面创建一个依赖于该窗口保留文件对话框,filedialog_save,=new FileDialog(this,保留文件对话框,FileDialog.SAVE);,filedialog_save.setVisible(false);,/,再创建一个依赖于该窗口打开文件对话框,filedialog_load,=new FileDialog(this,打开文件对话框,FileDialog.LOAD);,filedialog_load.setVisible(false);,filedialog_save.addWindowListener,(new WindowAdapter()public void windowClosing(WindowEvent e),filedialog_save.setVisible(false),;);,filedialog_load.addWindowListener,(new WindowAdapter()public void windowClosing(WindowEvent e),filedialog_load.setVisible(false),;);,addWindowListener,(new WindowAdapter()public void windowClosing(WindowEvent e),setVisible(false);System.exit(0),;);,第25页,public void actionPerformed(ActionEvent e),if(e.getSource()=,item1,),filedialog_load.setVisible(true),;String s;,try/,建立到文件,file,FileReader,流,该文件经过,File,类和对话框来确定。,File file=new File(filedialog_load.getDirectory(),filedialog_load.getFile();,file_reader=new FileReader(file);,in=new BufferedReader(file_reader);,while(s=in.readLine()!=null),text.append(s+n);,catch(FileNotFoundException e1),catch(IOException e2),try,in.close();,file_reader.close();,catch(IOException exp),第26页,else if(e.getSource()=,item2,),filedialog_save.setVisible(true);,try/,建立到文件,file,FileWriter,流,该文件经过,File,类和对话框来确定。,File file=new File(filedialog_save.getDirectory(),filedialog_save.getFile();,tofile=new FileWriter(file);,out=new BufferedWriter(tofile);,out.write(text.getText(),0,(text.getText().length();,out.flush();,catch(FileNotFoundException e1),catch(IOException e2),try,out.close();,tofile.close();,catch(IOException exp),第27页,6.RandomAccessFile,类,因为,File,类不能读写文件,可使用,Stream,类或,RandomAccessFile,类来读写。,RandomAccessFile,类既不是输入流类,InputStream,类子类,也不是输出流类,OutputStream,类子类。,RandomAccessFile,类创建流指向既能够作为源,也能够作为目标地。,结构方法分别是:,RandomAccessFile,(String name,String mode);,RandomAccessFile,(File file,String mode);,参数,mode,取,r(,只读)或,rw(,可读写),决定文件访问权利.,第28页,创建对象时应捕捉,FileNotFoundException,异常,当流进行读写操作时,应捕捉,IOException,异常。,RandomAccessFile,类中方法:,seek(long a),;/a,确定文件指针距离文件开头字节位置。,getFilePointer(),方法获取当前文件指针位置。,第29页,P271,例 9,in_and_out=new RandomAccessFile(“tom.txt”,“rw”);,try,for(int i=0;i=0;i-)/,一个,int,型数据占4个字节,我们从,in_and_out.seek(i*4);,/,文件第36个字节读取最终面一个整数,System.out.println(“,”+in_and_out.readInt();/,每隔4个字节往前读取一个整数,第30页,7.,管道流,管道是不一样,线程,之间直接传输数据基本伎俩。,PipedInputStream,类创建对象称为一个输入管道,,PipedOutputStream,类创建对象称为一个输出管道。输出管道与输入管道连接形成一个传输数据通道,使用这么管道,用户能够在不一样线程之间实现数据共享。,第31页,7.1 PipedInputStream,类,PipedInputStream(),:,创建一个管道输入流,它还没有被连接,在使用之前必须连接到一个管道输出流。,使用,connect(PipedOutputStream c),方法连接。,PipedInputStream()in=new PipedInputStream();,PipedOutputStream()out=new PipedOutputStream();,in.connect(out);,PipedInputStream(PipedOutputStream a);,/,创建一个管道输入流,它被连接到由参数,a,指定管道输出流。,第32页,7.2 PipedOutputStream,类,PipedOutputStream(),:,创建一个管道输出流,它还没有被连接,在使用之前必须连接到一个管道输入流。,使用,connect(PipedInputStream c),方法连接。,PipedOutputStream()out=new PipedOutputStream();,PipedInputStream()in=new PipedInputStream();,out.connect(in);,PipedOutputStream(PipedInputStream a);,/,创建一个管道输出流,它被连接到由参数,a,指定管道输入流。,第33页,7.3,管道流异常,创建管道流都必须捕捉,IOException,异常。,try,PipedInputStream()in=new PipedInputStream();,catch(IOException e),第34页,P274,例 11,Example20_11,import java.io.*;,public class Example20_11,public static void main(String args),PipedOutputStream out=null;,PipedInputStream in=null;,try,out=new PipedOutputStream();,in=new PipedInputStream();,in.connect(out);,catch(IOException e),thread1 one=new thread1(out,in);,thread2 two=new thread2(in,out);,one.start();two.start();,第35页,class,thread1,extends Thread,PipedOutputStream out;PipedInputStream in;,byte b=1,2,3;,thread1(PipedOutputStream a,PipedInputStream b),try,out=a;in=b;,out.connect(in);,catch(IOException e),public void run(),try,out.write(b,0,3);,catch(IOException e),第36页,class,thread2,extends Thread,PipedOutputStream out;PipedInputStream in;,byte a=new byte3;,thread2(PipedInputStream a,PipedOutputStream b),try in=a;out=b;in.connect(out);,catch(IOException e),public void run(),try,in.read(a,0,3);,for(int i=0;i=2;i+),System.out.println(+ai);,int c=a0+a1+a2;,System.out.println(+c);,catch(IOException e),第37页,8.,数据流,数据流允许程序按与机器无关格调读取,Java,原始数据。,结构方法:,DateInputStream(InputStream in);,DateOutputStream(OutputStream out);,惯用方法:,P276.,P277,,例12,第38页,9.,对象流,ObjectInputStream,类和,ObjectOutputStream,类分别是,DateInputStream,和,DateOutputStream,类子类.,结构方法:,DateInputStream(InputStream in);,DateOutputStream(OutputStream out);,ObjectInputStream,类和,ObjectOutputStream,类创建对象被称为对象输入流和对象输出流.,对象输出流使用,writeObject(Object obj),方法写文件.,对象输入流使用,readObject(),方法读文件.,第39页,9.1,创建对象流方法,ObjectInputStream/DateOutputStream,指向是一个输入/输出流对象,因而要首先用,FileInputStream/,FileOutputStream,创建一个文件流.如:,FileInputStream file_in=new FileInputStream(“a.txt”);,ObjectInputStream object_in=new ObjectInputStream(file_in);,FileOutputStream file_out=new FileOutputStream(“a.txt”);,ObjectOutputStream object_out=new ObjectOutputStream(file_out);,第40页,P281,例 14,public class Example20_14 extends Frame implements ActionListener,TextArea text=null;Button reader=null,writer=null;,FileInputStream file_in=null;,FileOutputStream file_out=null;,ObjectInputStream object_in=null;,ObjectOutputStream object_out=null;,Example20_14(),setLayout(new FlowLayout();text=new TextArea(6,10);,reader=new Button,读入对象,);writer=new Button(“,写出对象,);,reader.addActionListener(this);writer.addActionListener(this);,setVisible(true);add(text);add(reader);add(writer);,addWindowListener(new WindowAdapter()public void,windowClosing(WindowEvent e),System.exit(0););,pack();setSize(300,300);,第41页,public void actionPerformed(ActionEvent e),if(e.getSource()=,writer,),try,file_out=new FileOutputStream(tom.txt);,object_out=new ObjectOutputStream(file_out);,object_out.writeObject(text);,object_out.close();,catch(IOException Event),else if(e.getSource()=,reader,),try,file_in=new FileInputStream(tom.txt);,object_in=new ObjectInputStream(file_in);,TextArea temp=(TextArea)object_in.readObject();,temp.setBackground(Color.pink);this.add(temp);,this.pack();this.setSize(600,600);object_in.close();,catch(ClassNotFoundException e),System.out.println(“,不能读出对象,);,catch(IOException Event),System.out.println(can not read file);,第42页,10.PrintStream,类,使用数据流写成文件用其它文件阅读器无法进行阅读(看上去是乱码)。,PrintStream,类提供了一个过滤输出流,该输出流能以文本格式显示,Java,数据类型。,结构方法:,PrintStream(OutputStream osteam),;,try,PrintStream ps=new PrintStream(new FileOutputStream(p.txt);,ps.print(12345);ps.println(how are you);,ps.println(234);ps.println(x);,ps.println(button);ps.close();,catch(IOException e),第43页,11.PrintWriter,类,与,PrintStream,类类似。,结构方法:,PrintWriter(OutputStream osteam);,try,PrintWriter ps=new PrintWriter(new FileOutputStream(p.txt);,ps.print(12345);ps.println(how are you);,ps.println(234);ps.println(x);,ps.println(button);ps.close();,catch(IOException e),第44页,菜单项,(,保留原有,),仓库管理小程序,货物编号:,货物名称:,单 价:,数 量:,金 额:,录 入,查 询,第45页,
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服