IO 流小记录
File类
构造函数: FIle file = new File(path);
常用函数:
是否存在: file.exists()
文件名: file.getName()
父目录: file.getParent()
=================================================================
FileInputStream 类
构造方法: InputStream is = new InputStream(File file)
常用方法:
len = is.read(buffer,off,len); ---> len = is.read(buffer);
b the buffer into which the data is read.
@return the total number of bytes read into the buffer, or -1
FileOutputStream 类
构造方法: OutputStream os = new FileOutputStream(File file)
常用方法:
os.write(buffer,0,len)
BufferedInputStream 添加了缓冲区的输入流
构造方法: BufferedInputStream bis = new BufferedInputStream(FileInputStream fis);
常用方法:
bis.read(buffer,0,len);
BufferedOutputStream bos 添加了缓冲区的输出流
构造方法: BufferedOutputStream bos = new BufferedOutputStream(FileOutputStream fos);
常见方法:
bos.write(buffer,0, len);
bos.flush()
bos.close();
前提 序列化与反序列化 要继承接口 java.io.Serializable;
ObjectInputStream 对象的输入流, 实现对象的反序列化。
构造方法: ObjectInputStream bis = new ObjectInputStream(FileInputStream fis);
主要方法:
bis.readObect();
ObjectOutputStream 对象的输出流, 实现对象的序列化
构造方法: ObjectOutputStream bos = new ObjectOutputStream(FileOutputStream fos);
主要方法:
bos.write(object obj);
============================================================================================================
字节流:
InputStreamReader 字符输入流,输入内存中时, 已经将字节装换成 unicode 16位的字符编码。 解决了乱码的问题
构造方法:InputStreamReader isr = new InputStreamReader( FileInputStream fis );
常用方法
len = isr.read(char [] buffer, int off, int len)
OutputStreamWriter 字符输出流。 char
构造方法: OutputStreamWriter osw = new OutputStreamWriter(FileOutputStream fos);
osw.write(char[] cbuff,off,len);
osw.flush()
osw.close()
FileReader 类
构造方法: FileReader fr = new FileReader( new File(path) )
常用方法:
fr.read(char[] buff, int off, int len);
FileWriter 类
构造方法: FileWriter fw = new FileWriter(new File(Path))
常用方法:
fr.write(char[] buff, int off, int len);
BufferedReader 类 filereader.readLine() return String 每次读取一行。
Reader r = new FileReader(new File("d:/w.txt")); BufferedReader bReader = new BufferedReader(r); //String str = bReader.readLine(); String str = null; while( ( str = bReader.readLine() ) != null ){ System.out.println(str); }
bufferedWriter 类
Writer w = new FileWriter(new File("d:/w.txt")); BufferedWriter bw = new BufferedWriter(w); String s = "叙利亚\r\n利比亚\r\n中国\r\n"; bw.write(s, 0, s.length()); bw.close();