Presentation is loading. Please wait.

Presentation is loading. Please wait.

西南科技大学网络教育系列课程 高级语程序设计(Java) 第九章 输入/输出流.

Similar presentations


Presentation on theme: "西南科技大学网络教育系列课程 高级语程序设计(Java) 第九章 输入/输出流."— Presentation transcript:

1 西南科技大学网络教育系列课程 高级语程序设计(Java) 第九章 输入/输出流

2 文 件 构造方法 java.io包中的File类提供了获得文件基本信息及文件操作的方法。 File(String filename);
File(String directoryPath,String filename); File(File f, String filename); File file1 = new File(test.txt); File file2 = new File(“e:/java“,”test.txt”); File file3 = new File(“e:/java”); File file4 = new File(file2,"test.txt");

3 public boolean canWrite():判断文件是否可被写入。 public boolean exits():判断文件是否存在。
File类的常用方法 文件的属性 public boolean canRead():判断文件是否是可读的。 public boolean canWrite():判断文件是否可被写入。 public boolean exits():判断文件是否存在。 public boolean isFile():判断文件是否是一个正常文件,而不是目录。 public boolean isDirectroy():判断文件是否是一个目录。

4 public String getPath():得到文件的路径名。
获取文件的名称、路径 public String getName():获取文件的名字。 public String getPath():得到文件的路径名。 public String getAbsolutePath():得到文件的绝对路径名。 public String getParent():得到文件的上一级目录名。 获取文件处理信息 public long length(): 获取文件的长度(单位是字节)。 public long lastModified():获取文件最后修改时间。 public boolean delete():删除文件。

5 目录操作 创建目录: 列出目录中的文件 在目录中创建文件 public boolean mkdir()
public String[ ] list() public File[ ] listFiles() 在目录中创建文件 public boolean createNewFile() File file2 = new File("e:/java"); File file1 = new File(file2,"test.txt"); file2.mkdir(); file1.createNewFile(); String[ ] files = file2.list(); for(int i = 0;i<files.length;i++) System.out.print(files[i]); File[ ] files = file2.listFiles(); for(int i = 0;i<files.length;i++) System.out.println(files[i].getName()+"\t" files[i].length());

6 要执行一个本地机上的可执行文件,可以使用java.lang包中的Runtime类提供的方法。
运行可执行文件 要执行一个本地机上的可执行文件,可以使用java.lang包中的Runtime类提供的方法。 Runtime对象的创建 Runtime ec; ec = Runtime.getRuntime(); 可执行文件的执行 public Process exec(String command) throws IOException Runtime ce = Runtime.getRuntime(); File file = new File("c:/windows","Notepad.exe"); ce.exec(file.getAbsolutePath());

7 Java约定是用UNIX和URL风格的斜线(/)来作路径分隔符;如果用Windows/DOS所使用的反斜线( \ )的约定,则需要在字符串内使用它的转义序列( \\ )。

8 流的基本概念 流的基本概念 流是指在计算机的输入输出之间运动的数据序列,分为输入流和输出流。 流都有自己的起点和终点,并且是顺序的。
流式输入输出的特点 每个数据都必须等待排在它前面的数据读入或送出之后才能被读写。 每次读写操作处理的都是序列中剩余的未读写数据中的第一个,而不能随意选择输入输出的位置。

9 流的类结构 流的实现是在java.io包的类层次结构上。 以四个顶层抽象类为基础,衍生出系列具体的类,来完成各种输入/输出。
InputStream,OutputStream:用于字节的读/写。 Reader,Writer:用于文本(字符)的读/写。 实际使用的是它们的子类的对象。

10 流的基本类型 文件流 FileInputStream, FileOutputStream FileReader,FileWriter 缓冲流
BufferedReader,BufferedWriter 数据流 DataInputStream, DataOutputStream 管道流 PipedInputStream,PepedOutputStream 对象流 ObjectInputStream, ObjectOutputStream

11 文件字节流 FileInputStream类 从InputStream中派生出来,其所有方法都从InputStream类继承而来。
基本操作步骤:1>建立文件的输入流对象 >从输入流中读取字节 >关闭流 常用构造方法 public FileInputStream(String name) public FileInputStream(File file) 均会抛出FileNotFoundException异常 构造方法参数指定的文件称作输入流的源

12 文件字节流的读取 read方法给程序提供从输入流中读取数据的基本方法。 read方法的格式 public int read() throws IOException 从输入流中顺序读取单个字节的数据 public int read(byte b[ ]) throws IOException public int read(byte b[ ], int off, int len) throws IOException 把多个字节读到一个字节数组中 read方法将顺序读取流,直到流的末尾或流被关闭(close()方法)

13 FileOutputStream类 从OutputStream中派生出来,其所有方法都从OutputStream类继承来的。
基本操作步骤:1>建立文件的输出流对象 >向输出流中写字节 >关闭流 常用构造方法 public FileOutputStream(String name) public FileOutputStream(File file) 均会抛出FileNotFoundException异常

14 public void write(byte b[ ]) throws IOException
文件字节流的输出 write方法把字节发送给输出流 write方法的格式 public void write(byte b[ ]) throws IOException public void write(byte b[ ], int off, int len) throws IOException write方法将顺序地写文件,直到流的末尾或流被关闭(close()方法) 如果输出流要写入数据的文件已经存在,该文件中的数据内容就会被刷新;如果要写入数据的文件不存在,该文件就会被建立。

15 文件字符流 FileReader类 从InputStreamReader中派生出来,其所有方法都从InputStreamReader类和Reader类继承而来。 基本操作步骤:1>建立文件的输出流对象 >向输出流中写字符 >关闭字符流 常用构造方法 public FileReader(String name) public FileReader (File file) 均会抛出FileNotFoundException异常 构造方法参数指定的文件称作输入流的源

16 read方法给程序提供从输入流中读取数据的基本方法。
文件字符流的读取 read方法给程序提供从输入流中读取数据的基本方法。 read方法的格式 public int read() throws IOException 从源中顺序读取一个字符 public int read(byte b[ ]) throws IOException public int read(byte b[ ], int off, int len) throws IOException 把多个字节读到一个字节数组中

17 FileWriter类 从OutputStreamReader中派生出来,其所有方法都从OutputStreamReader类和Writer类继承而来。 基本操作步骤:1>建立文件的输出流对象 >向输出流中写字符 >关闭字符流 常用构造方法 public FileWriter(String name) public FileWriter(File file) 均会抛出FileNotFoundException异常

18 public void write(char b[ ]) throws IOException
文件字符流的输出 write方法给程序提供把字符数据写入到输出流的基本方法。 write方法的格式 public void write(char b[ ]) throws IOException public int write(char b[ ], int off, int len) throws IOException public int write(String str) throws IOException public int write(String str, int off, int len) throws IOException

19 任何Reader流都可以用来包装成缓冲流
BufferedReader类 Reader类的直接子类 任何Reader流都可以用来包装成缓冲流 构造方法 public BufferedReader(Reader in,int sz) public BufferedReader(Reader in) BufferedReader对象的创建 先创建一个Reader子类的对象,然后使用这个对象来创建缓冲流对象。 FileReader inOne = new FileReader("Student.txt") BufferedReader inTwo = new BufferedReader(inOne);

20 public int read() throws IOException 读取一个字符
从缓冲区读取 缓冲流的读取 public int read() throws IOException 读取一个字符 public int readLn() throws IOException 读取一行文本 读取完毕后,最好调用close()方法关闭流。

21 public BufferedWriter (Writer in,int sz)
构造方法 public BufferedWriter (Writer in,int sz) public BufferedWriter(Reader in) BufferedWriter对象的创建 先创建一个Writer子类的对象,然后使用这个对象来创建缓冲流对象。 FileWriter outOne = new FileWriter("Student.txt") BufferedWriter outTwo = new BufferedWriter(outOne);

22 缓冲流的写入 public void write(String str) throws IOException 向缓冲区写入一个字符串。 public int writeLn(String str,int off,int len) throws IOException 向缓冲区写入一个字符串的一部分。 输出完毕后,最好调用close()方法关闭流。

23 数据流 DataInputStream类 数据流允许通过数据流来读写Java基本数据类型。
读取一个数值时,不必再关心这个数值应当是多少个字节。 DataInputStream类 DataInputStream的构造方法 DataInputStream(InputStream in)

24 数据流的读取 public final char readChar() throws IOException public final byte readByte() throws IOException public final short readShort() throws IOException public final int readInt() throws IOException public final long readLong() throws IOException public final float readFloat() throws IOException public final double readDouble() throws IOException public final boolean readBoolean() throws IOException public final String readUTF() throws IOException

25 DataOutputStream类 DataOutputStream的构造方法
DataOutputStream(OutputStream Out) 数据流的写入 public final void writeChar(String s) throws IOException public final void writeByte(int v) throws IOException public final void writeShort(int v) throws IOException

26 public final void WriteInt(int v) throws IOException
public final void writeLong(long v) throws IOException public final void writeFloat(float v) throws IOException public final void writeDouble(double v) throws IOException public final void writeBoolean(boolean v) throws IOException public final void readUTF(String s) throws IOException

27 管道流 管道流主要用于线程间的通信,一个线程中的PipedInputStream对象从另一个线程中互补的PipedOutputStream对象中接收输入。 管道流必须同时具备可用的输入端和输出端。 PipedInputStream类的构造方法 PipedInputStream() PipedInputStream(PipedOutputStream src) PipedOutputStream类的构造方法 PipedOutputStream() PipedOutputStream(PipedInputStream snk)

28 管道流的输入输出 建立输入流 建立输出流 将输入流和输出流连接起来 对使用无参数的构造方法的情况下,需要使用方法:void connect(PipedOutputStream src)或void connect(PipedOutputStream src)。 使用有参数的构造方法时,输入流和输出流会自动地直接连接。

29 对象流 对象流主要用于把对象写入文件数据或从文件数据中读出。 ObjectInputStream类的构造方法
ObjectInputStream(InputStream in) ObjectOutputStream类的构造方法 ObjectOutputStream(OutputStream out) FileInpurtStream f1 = new FileInputStream(“f1.txt”); ObjectInputStream obj = new ObjectInputStream(f1);

30 使用对象流写入或读入对象时,要保证对象是序列化的。
把对象存为某种永久存储类型,称为序列化 实现java.io.Serializable接口。


Download ppt "西南科技大学网络教育系列课程 高级语程序设计(Java) 第九章 输入/输出流."

Similar presentations


Ads by Google