当前位置:文档之家› Java输入输出流详细用法

Java输入输出流详细用法

String str=in.readLine(); byte b[]=str.getBytes(“ISO-8859-1”);l String str=new String(b); 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. 注意: 要用 try{}catch(){} 写入结束后要用 close()关闭 六、 数组流 数组流是以数组作为源,来写入和读取计算机内存的。数组流不对文件进行操作。 1. 字节数组输入流 ByteArrayInputStream 构造方法:
} 6. 文件的创建与删除
Public Boolean createNewFile(); File.delete(); 7. 运行可执行文件 Runtime ec=Runtime.getRuntime(); ec.exec(String command); 例 7-1:ec.exec(“notepad”);打开记事本 例 7-2:打开 QQ 登录界面 Runtime ec=Runtime.getRuntime(); File file=new File(“QQ 绝对路径”,”QQ.exe”); try {
e1.pห้องสมุดไป่ตู้intStackTrace(); } catch (Exception e1) {
e1.printStackTrace(); }
二、 文件字节流 1. 文件字节输入流 FileInputStream
创建: 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 长度,返回实际 读取的长度
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());
ByteArrayInputStream(byte [] b); ByteArrayInputStream(byte [] b, int off, int len); 数组 b 为输入流的源,从数组 b 里读出数据 方法: Public int read();从源中顺序的读出一个字节 Public int read(byte[]b);从源中读出 b.length 个字节,存放到数组 b 中,若未读出字节, 返回-1 Public int read(byte[]b,int off,int len);从源中读出 len 个字节,存放到数组 b 中,若未读 出字节,返回-1 注意: 要用 try{}catch(){} 写入结束后要用 close()关闭 2. 字节数组输出流 构造方法:
当返回值为-1 时,读取完毕 注意: 要用 try{}catch(){} 读取结束后要用 close()关闭 2. 文件字节输出流 FileOutputStream 创建: FileOutputStream(String name); FileOutputStream(File file); 方法: Public void write(byte b[]); Public void write(byte b[],int off,int len); 注意: 要用 try{}catch(){} 写入结束后要用 close()关闭 三、 文件字符流 1. 文件字符输入流 FileReader 创建: FileReader(String filename); FileReader(File filename); 方法: Int read(); 读出一个字符 Int read(char [] c); 读取到数组 c 中,返回实际读取的字符个数 Int read(char [] c,int off,int len); 读取到数组 c 中,从 off 开始,读取 len 个字符,返回实 际读取的字符个数 注意: 要用 try{}catch(){} 写入结束后要用 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 len); 将 c 从 off 开始的 len 个字符写入源 注意: 要用 try{}catch(){} 写入结束后要用 close()关闭 四、 缓冲流
ec.exec(f.getAbsolutePath()); } catch (IOException e) {
e.printStackTrace(); } 例 7-4:打开浏览器,且打开设置好的界面 Desktop dp=Desktop.getDesktop(); try {
dp.browse(new .URI("/")); } catch (IOException e1) {
缓冲流称为上层流,它们的源和目的地必须是字符输入流和输出流,把字符输入输出 流成为底层流,java 采用缓存技术将上层流和底层流连接,底层字符输入流首先将数 据读取缓存,BufferedReader 流再从缓存读取数据;BufferedWriter 流将数据写入缓存, 底层字符输出流会不断地将缓存中的数据写入到目的地。用 BufferedWriter 流调用 flush()刷新缓存或调用 close()方法关闭时,即使缓存没有溢出,底层流也会立刻将缓存
输入输出流
一、 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(); public 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. 列出目录中的文件 如果 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 filter=new FilenameFilter(){
的内容写入目的地。 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); 读取到数组 c 中,从 off 开始,读取 len 个字符,返回 实际读取的字符个数, 读取完毕返回-1 注意: 要用 try{}catch(){} 写入结束后要用 close()关闭,先关闭 FileReader,在关闭 BufferedReader 2. 缓冲输出流 BufferedWriter 创建: BufferedWriter(Writer out); 由于 FileWriter 是 Writer 的子类,所以常用下面的方法 BufferedWriter(FileWriter out); 方法: Void newline(); 写入一个回车符 Void write(int n); 写入一个字符 Void write(char [] c); 写入字符数组 c Void write(char[] c,int off,int len); 将 c 从 off 开始的 len 个字符写入源 注意: 要用 try{}catch(){} 写入结束后要用 close()关闭,先关闭 BufferedWriter,再关闭 FileWriter 五、 随机流 1. RandomAccessFile 类的两个构造方法: RandomAccessFile(String name,String mode); RandomAccessFile(File file,String mode); RandomAccessFile 类既可以读取文件,也可以写入数据到文件,mode 取 r(只读)或 rw(可读写),决定创建的流对文件的访问权限 2. 方法: 定位读写位置方法: seek(long a) 定位 RandomAccessFile 流的读写位置 getFilePointer() 获取流的当前读写位置 length() 获取文件长度 读文件方法: readLine() 从文件读取一行文本,但如果有中文会出现乱码,需要先用 ISO-8859-1 重 新编码,例:
相关主题