Java输入输出流详细用法.docx_第1页
Java输入输出流详细用法.docx_第2页
Java输入输出流详细用法.docx_第3页
Java输入输出流详细用法.docx_第4页
Java输入输出流详细用法.docx_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

输入输出流一、 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,否则返回false4. 列出目录中的文件如果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()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 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 Explorer,iexplore.exe);try ec.exec(f.getAbsolutePath(); catch (IOException e) e.printStackTrace();例7-4:打开浏览器,且打开设置好的界面Desktop dp=Desktop.getDesktop();try dp.browse(new .URI(/); catch (IOException e1) e1.printStackTrace(); 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长度,返回实际读取的长度当返回值为-1时,读取完毕注意:要用trycatch()读取结束后要用close()关闭2. 文件字节输出流FileOutputStream创建:FileOutputStream(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); 读取到数组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); 写入字符数组cVoid write(char c,int off,int len); 将c从off开始的len个字符写入源注意:要用trycatch()写入结束后要用close()关闭四、 缓冲流缓冲流称为上层流,它们的源和目的地必须是字符输入流和输出流,把字符输入输出流成为底层流,java采用缓存技术将上层流和底层流连接,底层字符输入流首先将数据读取缓存,BufferedReader流再从缓存读取数据;BufferedWriter流将数据写入缓存,底层字符输出流会不断地将缓存中的数据写入到目的地。用BufferedWriter流调用flush()刷新缓存或调用close()方法关闭时,即使缓存没有溢出,底层流也会立刻将缓存的内容写入目的地。1. 缓冲输入流BufferedReader创建:BufferedReader(Reader in);由于FileReader是Reader的子类,所以常用下面的方法BufferedReader(FileReader in);方法:String readLine(); 读出一行字符,返回一个字符串,读取完毕返回NullInt read(); 读出一个字符,读取完毕返回-1Int read(char c); 读取到数组c中,返回实际读取的字符个数, 读取完毕返回-1Int read(char c,int off,int len); 读取到数组c中,从off开始,读取len个字符,返回实际读取的字符个数, 读取完毕返回-1注意:要用trycatch()写入结束后要用close()关闭,先关闭FileReader,在关闭BufferedReader2. 缓冲输出流BufferedWriter创建:BufferedWriter(Writer out); 由于FileWriter是Writer的子类,所以常用下面的方法BufferedWriter(FileWriter out);方法:Void newline(); 写入一个回车符Void write(int n); 写入一个字符Void write(char c); 写入字符数组cVoid 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类既可以读取文件,也可以写入数据到文件,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”);lString 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. 注意:要用trycatch()写入结束后要用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中,若未读出字节,返回-1Public int read(byteb,int off,int len);从源中读出len个字节,存放到数组b中,若未读出字节,返回-1注意:要用trycatch()写入结束后要用close()关闭2. 字节数组输出流构造方法:ByteArrayOutputStream();ByteArrayOutputStream(int size);第一个构造方法的字节数组输出流指向一个默认大小为32字节的缓冲区,如果输出流向缓冲区写入的字节数大于缓冲区时,缓冲区的容量会自动增加。第二个构造方法构造的字节数组输出流指向的缓冲区的初始大小有参数size指定,如果输出流向缓冲区写入的字节数大于缓冲区时,缓冲区的容量会自动增加。方法:Public void write(byte b);向缓冲区写入数组bPublic void write(int b);顺序的向缓冲区写入一个字节Public void write(byte b,int off,int len);写入数组b中的len个字节Public byte toByteArray();返回输出流写入缓冲区的全部字节,用于读出例:ByteArrayOutputStream out=new ByteArrayOutputStream();try writer.write(你好.toByteArray();writer.close(); catch (IOException e1) e1.printStackTrace();ByteArrayInputStream in=new ByteArrayInputStream(out.toByteArray();注意:要用trycatch()写入结束后要用close()关闭3. 字符数组输入流构造方法:CharArrayReader(char c);CharArrayReader(char c, int off, int len);数组c为输入流的源,从数组c里读出数据方法:Public int read();从源中顺序的读出一个字符Public int read(char c); 从源中读出c.length个字节,存放到数组c中,未读出字节,返回-1Public int read(char c,int off,int len);从源中读出len个字节,存放到数组c中,未读出字节,返回-1注意:要用trycatch()写入结束后要用close()关闭4. 字符数组输出流构造方法:CharArrayWriter();CharArrayWriter(int size); 方法:Public void write(char b);向缓冲区写入数组bPublic void write(int b);顺序的向缓冲区写入一个字符Public void write(char b,int off,int len);写入数组b中的len个字符Public char toCharArray();返回输出流写入缓冲区的全部字符,用于读出例:CharArrayWriter out=new CharArrayWriter();try writer.write(你好.toCharArray();writer.close(); catch (IOException e1) e1.printStackTrace();CharArrayReader in=new CharArrayReader (out.toCharArray();注意:要用trycatch()写入结束后要用close()关闭七、 数据流DataInputStream和DataOutputStream这两个流很有用,它们允许程序按照机器无关的风格读取Java原始数据,即当读取一个数值时,不必再关心这个数值应当是多少个字节。构造方法:DataInputStream(InputStream in):创建的数据输入流指向一个有参数in指定的底层输入流DataOutputStream(OutputStream out):创建的数据输出流指向一个由参数out指定的底层输出流一般用FileInputStream作为源方法:close() 关闭流readBoolean() 读取一个布尔值readByte() 读取一个字节readChar() 读取一个字符readDouble() 读取一个双精度浮点值readFloat() readInt() readlong() readShort() readUnsignedByte() readUnsignedShort() readUTF() 读取一个UTF字符串skipBytes(int n)跳过给定数量的字节writeBlloean(boolear v) writeBytes(String s) writeChars(String s) writeDouble(double v) writeFloat(float v) writeInt(int v) writeLong(long v) writeShort(int v) writeUTF(String s)八、 带进度条的输入流带进度条的输入流是以字节为输入的,其构造方法:ProgressMonitorInputStream(Component c,String s, InputStream);进度条在参数c指定的组件的正前方显示,若该参数取null,则在屏幕的正前方显示。String s 是进度条上的文字,InputStream是输入源,一般取FileInputStream类例:FileInputStream in=new FileInputStream(file);ProgressMonitorInputStream input=new ProgressMonitorInputStream(null, 读取中, in);ProgressMonitor p=input.getProgressMonitor();获取进度条int i=0;String s=; byte b=new byte30;while(i= input.read(b)!=-1)System.out.println(new String(b,0,i);Thread.sleep(200);/由于文件较小,为了看清进度条这里有意延缓0.2s九、 对象流1. ObjectInputStreamInputStream的子类创建:ObjectInputStream(InputStream in);一般用FileInputStream类似BufferedReader方法:Object readObject(); /返回Object类型,若要返回特定类,则用类型转换注意:要用trycatch()写入结束后要用close()关闭2. ObjectOutputStreamOutputStream的子类创建: ObjectOutputStream(OutputStream out);一般用FileOutputStrea类似BufferedWriter方法:writeObject(Object obj); /向文件写入类,写入的这个类必须是序列化的,即要implements Serializable注意:要用trycatch()写入结束后要用close()关闭十、 序列化和对象克隆利用对象流,借用数组流来完成克隆。用数组流来作为输出的源,将对象输出到内存,然后再用数组流作为输入的源,将对象输出,即完成了克隆例:读出JTextArea textArea里的内容,克隆出JTextArea cloneTextAreaByteArrayOutputStream outputStream=new ByteArrayOutputStream();ObjectOutputStream out=new ObjectOutputStream(outputStream);out.writeObject(textArea);ByteArrayInputStream inputStream=new ByteArrayInputStream(outputStream.toByteArray();ObjectInputStream in=new ObjectInputStream(inputStream);JTextArea cloneTextArea=(JTextArea)in.readObject();注意:要用trycatch()写入结束后要用close()关闭十一、 文件锁1. 简介FileLock和FileChannel类分别在Java.nio和java.nio.channels包中。输入输出流可以使用文件锁,下面以RanndomAccessFile类为例2. 建立流对象RanndomAccessFile input=new RanndomAccessFile(File file,”rw”);必须是rw3. 得到信道FileChannelFileChannel channel=input.getChannel();4. 得到文件锁FileLockFileLock lock=channel.tryLock(); 或 FileLock lock=channel.lock();5. 上锁和解锁lock.release();/解锁/写入或读出等操作lock=channel.tryLock();/操作完后重新上锁十二、 使

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论