Java流一、流的分类•按数据流动方向–输入流:只能从中读取字节数据,而不能向其写出数据–输出流:只能向其写入字节数据,而不能从中读取数据•按照流所处理的数据类型–字节流:用于处理字节数据。
–字符流:用于处理Unicode字符数据。
•按照流所处理的源–节点流:从/向一个特定的IO设备读/写数据的流。
(低级流)–处理流:对已存在的流进行连接和封装的流。
(高级流)二、缓冲流•缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。
•J2SDK提供了四种缓存流:–BufferedReader–BufferedWriter–BufferedInputStream s–BufferedOutputStream•缓冲输入流支持其父类的mark()和reset()方法:–mark()用于“标记”当前位置,就像加入了一个书签,可以使用reset()方法返回这个标记重新读取数据。
•BufferedReader提供了readLine()方法用于读取一行字符串(以\r 或\n分隔)。
•BufferedWriter提供了newLine()用于写入一个行分隔符。
•对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()方法将会使内存中的数据立刻写出。
三、类层次3.1、InputStream类层次3.2、OutputStream类层次3.3、Reader类层次3.4、Writer类层次四、常用的字符流与字节流的转化说明:1.字节流用于读写诸如图像数据之类的原始字节流。
2.字符流用于读写诸如文件数据之类的字符流。
3.低级流能和外设交流。
4.高级流能提高效率。
5.InputStreamReader 是字节流通向字符流的桥梁。
6.OutputStreamWriter 是字符流通向字节流的桥梁。
五、代码实例5.1、常用读文件:/***以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*@param fileName:文件的名*/public static void readFileByBytes(String fileName) { File file = new File(fileName);InputStream in = null;try {logger.debug("以字节为单位读取文件内容,一次读多个字节:");/*一次读多个字节*/byte[] tempbytes = new byte[100];int byteread = 0;in = new FileInputStream(file);/*读入多个字节到字节数组中,byteread为一次读入的字节数*/while ((byteread = in.read(tempbytes)) != -1) {logger.debug(tempbytes);logger.debug(0);logger.debug(byteread);}} catch (Exception e1) {logger.error("读取文本文件异常",e1);} finally {if (in != null) {try {in.close();} catch (IOException e1) {logger.error("读取文本文件异常",e1);}}}}*以字符为单位读取文件,常用于读文本,数字等类型的文件*@param fileName:文件名*/public static void readFileByChars(String fileName) {Reader reader = null;try {logger.debug("以字符为单位读取文件内容,一次读多个字节:");/*一次读多个字符*/char[] tempchars = new char[100];int charread = 0;if(fileName!=null&&!"".equals(fileName)){reader = new InputStreamReader(newFileInputStream(fileName));/*读入多个字符到字符数组中,charread为一次读取字符数*/while ((charread = reader.read(tempchars)) != -1) {/*对于windows下,rn这两个字符在一起时,表示一个换行。
*//*但如果这两个字符分开显示时,会换两次行。
*//*因此,屏蔽掉r,或者屏蔽n。
否则,将会多出很多空行。
*/if ((charread == tempchars.length)&& (tempchars[tempchars.length- 1] != 'r')) {logger.debug(tempchars);} else {for (int i = 0; i < charread; i++) {if (tempchars[i] == 'r') {continue;} else {logger.debug(tempchars[i]);}}}}}} catch (Exception e1) {logger.error("读取文本文件异常",e1);} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {logger.error("读取文本文件异常",e1);}}}/***以行为单位读取文件,常用于读面向行的格式化文件*@param fileName:文件名*/public static List<String> readFileByLines(String fileName) { List<String> list = new ArrayList<String>();if(fileName!=null&&!"".equals(fileName)){File file = new File(fileName);BufferedReader reader = null;try {logger.debug("以行为单位读取文件内容,一次读一整行:");reader = new BufferedReader(new FileReader(file));String tempString = null;/*一次读入一行,直到读入null为文件结束*/while ((tempString = reader.readLine()) != null) {logger.debug(tempString);list.add(tempString);}} catch (IOException e) {logger.error("读取文本文件异常",e);} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {logger.error("读取文本文件异常",e1);}}}}return list;}5.2、常用写文件:/***把内容写到文件*@param filePathName文件名*@param List<String>文件内容*/public static boolean writerFile(String filePathName,String content){boolean flag=false;OutputStreamWriter osw=null;try {if(filePathName!=null&&!"".equals(filePathName)){osw = new OutputStreamWriter(newFileOutputStream(filePathName));}} catch (FileNotFoundException e1) {flag=false;e1.printStackTrace();}if(osw!=null){BufferedWriter bw=new BufferedWriter(osw);try {if(content!=null&&!"".equals(content)){bw.write(content);flag= true;}} catch (IOException e) {flag=false;e.printStackTrace();}finally{try {bw.close();osw.close();} catch (IOException e) {flag=false;e.printStackTrace();}}}return flag;}/***把内容写到文件或追加到文件中*@param filePathName文件名*@param List<String>文件内容*/public static boolean writerFileIsAppend(String filePathName,String content){boolean flag=false;OutputStreamWriter osw=null;try {if (filePathName!=null&&!"".equals(filePathName)) {osw = new OutputStreamWriter(newFileOutputStream(filePathName,true));}} catch (Exception e1) {flag=false;e1.printStackTrace();}if(osw!=null){BufferedWriter bw=new BufferedWriter(osw);try {if(content!=null&&!"".equals(content)){bw.write(content);flag= true;}} catch (IOException e) {flag=false;e.printStackTrace();}finally{try {bw.close();osw.close();} catch (IOException e) {flag=false;e.printStackTrace();}}}return flag;}六、RandomAccessFile6.1:说明•RandomAccessFile是一种特殊的文件流,可以用它在文件的任何地方查找或者插入数据•RandomAccessFile同时实现了DataInput和DataOutput接口,所以可以用它来读/写文件•构造器:---RandomAccessFile(java.io.File f,String mode)---RandomAccessFile(String file,String mode)6.2:代码示例/***Description:读取文件最后一行内容*@param fileName文件路径名+文件名*/public static String getfinalLineData(String pathName){R andomAccessFile raf = null;S tring lastLine = "";try {raf = new RandomAccessFile(pathName, "r");long len = raf.length();if (len != 0L) {long pos = len - 1;while (pos > 0) {pos--;raf.seek(pos);if (raf.readByte() == '\n') {lastLine = raf.readLine();break;}}}} catch (Exception e) {e.printStackTrace();}finally {if (raf != null) {try {raf.close();} catch (IOException e1) {e1.getStackTrace();}}}return lastLine;}七、注意事项1、将高级流“套接“在低级流上,这样起到缓冲的作用可以提高效率。