Java输入输出流详细用法

上传人:woxinch****an2018 文档编号:38631437 上传时间:2018-05-05 格式:DOCX 页数:9 大小:35.88KB
返回 下载 相关 举报
Java输入输出流详细用法_第1页
第1页 / 共9页
Java输入输出流详细用法_第2页
第2页 / 共9页
Java输入输出流详细用法_第3页
第3页 / 共9页
Java输入输出流详细用法_第4页
第4页 / 共9页
Java输入输出流详细用法_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《Java输入输出流详细用法》由会员分享,可在线阅读,更多相关《Java输入输出流详细用法(9页珍藏版)》请在金锄头文库上搜索。

1、输入输出流输入输出流一、 File 类 1.File 对象的四种构造方法 File(String filename); File(String directoryPath); File(String directoryPath,String filename); File(File f ,String filename);2.File 类的属性 public String getName(); public boolean canRead(); public boolean canWrite(); public boolean exits(); public long length();pub

2、lic String getAbsolutePath();/获取文件的绝对路径 public String getParent();/获取文件的父目录 public boolean isFile(); public boolean isDirectory(); public boolean isHidden();public long lastModified();/获取文件最后修改的时间(时间是从 1970 年午夜至文件最 后修改时刻的毫秒数) 3.创建目录(创建文件夹) public boolean mkdir();/创建成功返回 true,否则返回 false 4.列出目录中的文件 如果

3、 File 对象是一个目录,可以调用下面方法列出该目录下的文件和子目录 Public String list(); Public File listFiles();5.列出目录中特定类型的文件 Public String list(FilenameFilter obj); Public File listFiles(FilenameFilter obj);FilenameFilter 是一个接口,该接口有一个方法: Public Boolean accept(File dir,String name); 例: File file=new File(“路径”); FilenameFilter f

4、ilter=new FilenameFilter() Public Boolean accept(File dir,String name) Return name.endsWith(“.java”); File files=file.listFiles(filter); For(File f:files) System.out.println(f.getName();6.文件的创建与删除 Public Boolean createNewFile(); File.delete();7.运行可执行文件 Runtime ec=Runtime.getRuntime(); ec.exec(String

5、 command);例 7-1:ec.exec(“notepad”);打开记事本 例 7-2:打开 QQ 登录界面 Runtime ec=Runtime.getRuntime();File file=new File(“QQ 绝对路径”,”QQ.exe”); try ec.exec(file.getAbsolutePath(); catch (IOException e) e.printStackTrace(); 例 7-3:打开浏览器 Runtime ec=Runtime.getRuntime(); File f=new File(“C:Program FilesInternet Explo

6、rer“,“iexplore.exe“); try ec.exec(f.getAbsolutePath(); catch (IOException e) e.printStackTrace(); 例 7-4:打开浏览器,且打开设置好的界面 Desktop dp=Desktop.getDesktop(); try dp.browse(new .URI(“http:/ catch (IOException e1) e1.printStackTrace(); catch (Exception e1) e1.printStackTrace(); 二、 文件字节流 1.文件字节输入流 FileInput

7、Stream 创建: FileInputStream(String name); FileInputStream(File file);方法: Int read();读出一个字符 Int read(byte b);读取到数组 b 中,读出 b.length 的长度,返回实际读取的长度 Int read(byte b,int off,int len);读取到数组 b 中,从 off 处开始,读出 len 长度,返回实 际读取的长度当返回值为-1 时,读取完毕 注意: 要用 trycatch() 读取结束后要用 close()关闭 2.文件字节输出流 FileOutputStream 创建: Fi

8、leOutputStream(String name); FileOutputStream(File file);方法: Public void write(byte b); Public void write(byte b,int off,int len);注意: 要用 trycatch() 写入结束后要用 close()关闭 三、 文件字符流 1.文件字符输入流 FileReader 创建: FileReader(String filename); FileReader(File filename);方法: Int read(); 读出一个字符 Int read(char c); 读取到数

9、组 c 中,返回实际读取的字符个数 Int read(char c,int off,int len); 读取到数组 c 中,从 off 开始,读取 len 个字符,返回 实际读取的字符个数 注意: 要用 trycatch() 写入结束后要用 close()关闭 2.文件字符输出流 FileWriter 创建: FileWriter(String filename); FileWriter(File filename);方法: Void write(int n); 写入一个字符 Void write(char c); 写入字符数组 c Void write(char c,int off,int

10、len); 将 c 从 off 开始的 len 个字符写入源 注意: 要用 trycatch() 写入结束后要用 close()关闭 四、 缓冲流 缓冲流称为上层流,它们的源和目的地必须是字符输入流和输出流,把字符输入输出 流成为底层流,java 采用缓存技术将上层流和底层流连接,底层字符输入流首先将数 据读取缓存,BufferedReader 流再从缓存读取数据;BufferedWriter 流将数据写入缓 存,底层字符输出流会不断地将缓存中的数据写入到目的地。用 BufferedWriter 流调 用 flush()刷新缓存或调用 close()方法关闭时,即使缓存没有溢出,底层流也会立刻

11、将缓存的内容写入目的地。 1.缓冲输入流 BufferedReader 创建: BufferedReader(Reader in);由于 FileReader 是 Reader 的子类,所以常用下面的方法 BufferedReader(FileReader in);方法: String readLine(); 读出一行字符,返回一个字符串,读取完毕返回 Null Int read(); 读出一个字符,读取完毕返回-1 Int read(char c); 读取到数组 c 中,返回实际读取的字符个数, 读取完毕返回-1 Int read(char c,int off,int len); 读取到数组

12、 c 中,从 off 开始,读取 len 个字符,返回 实际读取的字符个数, 读取完毕返回-1 注意: 要用 trycatch() 写入结束后要用 close()关闭,先关闭 FileReader,在关闭 BufferedReader 2.缓冲输出流 BufferedWriter 创建: BufferedWriter(Writer out); 由于 FileWriter 是 Writer 的子类,所以常用下面的方法 BufferedWriter(FileWriter out);方法: Void newline(); 写入一个回车符 Void write(int n); 写入一个字符 Void

13、write(char c); 写入字符数组 c Void write(char c,int off,int len); 将 c 从 off 开始的 len 个字符写入源 注意: 要用 trycatch() 写入结束后要用 close()关闭,先关闭 BufferedWriter,再关闭 FileWriter 五、 随机流 1.RandomAccessFile 类的两个构造方法: RandomAccessFile(String name,String mode); RandomAccessFile(File file,String mode);RandomAccessFile 类既可以读取文件,

14、也可以写入数据到文件,mode 取 r(只读)或 rw(可读写) ,决定创建的流对文件的访问权限 2.方法: 定位读写位置方法: seek(long a) 定位 RandomAccessFile 流的读写位置 getFilePointer() 获取流的当前读写位置 length() 获取文件长度 读文件方法: readLine() 从文件读取一行文本,但如果有中文会出现乱码,需要先用 ISO-8859-1 重 新编码,例: String str=in.readLine(); byte b=str.getBytes(“ISO-8859-1”);l String str=new String(b)

15、;read() 读取一个字节数据readChar()读取一个字符数据 readByte()读取一个字节 readInt(),readDouble(),readlong(),readShort()等 写文件方法: writeChars(String s) 写一个字符串 writeBytes(String s) 写一个字符串 writeChar(char c)写一个字符 write(byte b) 写 b.length 个字节到文件 writeDouble(double v),writeFloat(float f),writeInt(int i)等等 3.注意: 要用 trycatch() 写入结

16、束后要用 close()关闭 六、 数组流 数组流是以数组作为源,来写入和读取计算机内存的。数组流不对文件进行操作。 1.字节数组输入流 ByteArrayInputStream 构造方法: ByteArrayInputStream(byte b); ByteArrayInputStream(byte b, int off, int len);数组 b 为输入流的源,从数组 b 里读出数据 方法: Public int read();从源中顺序的读出一个字节 Public int read(byteb);从源中读出 b.length 个字节,存放到数组 b 中,若未读出字节, 返回-1 Public int read(byteb,int

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 高中教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号