IO流之过滤流介绍:
過濾流:
IO流按功能分類可以分為節點流和過濾流,節點流是用來直接操作目標設備的流,比如我們前面介紹的FileReader、FileWriter、FileInputStream、FileOutputStream,而過濾流是對應已存在的流進行包裝,以提供更強大和靈活的讀寫功能。
轉換流:
將字節流和字符流進行相互轉換的高級流:
InputStreamReader :將字節輸入流轉換為字符輸入流
OutputStreamWriter:將字節輸出流轉換成字符輸出流
通過研究FileReader的實現
public class FileReader extends InputStreamReader {public FileReader(String fileName) throws FileNotFoundException {super(new FileInputStream(fileName));}FileReader實現是繼承自InputStreamReader轉換流,轉換流的構造使用到FileInputStream字節輸入流,FileReader的實現是將讀取到的字節流通過轉換流(本質是通過編碼器將字節按照對應的編碼表:ASCII、GBK、UTF-8)將其解析為我們能夠識別的文本內容
緩沖流:
提高IO的讀取速度
字節緩沖流:BufferInputStream、BufferOutputStream
字符緩沖流:BufferReader、BufferWriter
特有方法:
BufferWriter
void newLine() throws IOException ;根據當前系統,寫入一個換行符
BufferReader
String readLine() throws IOException //讀取文本中一行內容
RandomAccessFile:
RandomAccessFile既可以讀取文件內容,也可以向文件輸出數據。同時,RandomAccessFile支持“隨機訪問”的方式,程序也可以直接跳轉到文件的任意地方來讀寫數據。RandomAccessFile的構造函數
mode:指定RandomAccessFile的訪問模式,一共有4種模式。
**"r" : ** 以只讀方式打開。調用結果對象的任何 write 方法都將導致拋出 IOException。 "rw": 打開以便讀取和寫入。 "rws": 打開以便讀取和寫入。相對于 "rw","rws" 還要求對“文件的內容”或“元數據”的每個更新都同步寫入到基礎存儲設備。 "rwd" : 打開以便讀取和寫入,相對于 "rw","rwd" 還要求對“文件的內容”的每個更新都同步寫入到基礎存儲設備。 public RandomAccessFile(String name, String mode)throws FileNotFoundException{this(name != null ? new File(name) : null, mode);} public RandomAccessFile(File file, String mode)throws FileNotFoundException//拋出IO異常{this(file, mode, false);}private RandomAccessFile(File file, String mode, boolean openAndDelete)throws FileNotFoundException{String name = (file != null ? file.getPath() : null);int imode = -1;if (mode.equals("r"))imode = O_RDONLY;else if (mode.startsWith("rw")) {imode = O_RDWR;rw = true;if (mode.length() > 2) {if (mode.equals("rws"))imode |= O_SYNC;else if (mode.equals("rwd"))imode |= O_DSYNC;elseimode = -1;}}if (openAndDelete)imode |= O_TEMPORARY;if (imode < 0)throw new IllegalArgumentException("Illegal mode \"" + mode+ "\" must be one of "+ "\"r\", \"rw\", \"rws\","+ " or \"rwd\"");SecurityManager security = System.getSecurityManager();if (security != null) {security.checkRead(name);if (rw) {security.checkWrite(name);}}if (name == null) {throw new NullPointerException();}if (file.isInvalid()) {throw new FileNotFoundException("Invalid file path");}fd = new FileDescriptor();fd.attach(this);path = name;open(name, imode);FileCleanable.register(fd); // open sets the fd, register the cleanup}RandomAccessFile的特有方法:
long getFilePointer( ):返回文件記錄指針的當前位置 void seek(long pos ):將文件指針定位到pos位置RandomAccessFile練習:
在指定文件的指定位置插入指定數據:
public static void indexAdd(String puth,Long index,String date) throws IOException {File file = new File(puth);if(!file.exists()){throw new IOException();}RandomAccessFile accessFile = null;FileOutputStream stream =null;FileInputStream stream1 = null;try {accessFile = new RandomAccessFile(file,"rw");accessFile.seek(index);stream = new FileOutputStream(file.getParent()+ File.separator + "tmp.txt",true);byte[] bytes = new byte[100];int i ;while ((i=accessFile.read(bytes))!= -1){stream.write(bytes,0,i);}accessFile.seek(index);accessFile.writeUTF(date);stream1 = new FileInputStream(file.getParent() + File.separator + "tmp.txt");while ((i=stream1.read(bytes))!= -1){accessFile.write(bytes,0,i);}new File(file.getParent() + File.separator + "tmp.txt").deleteOnExit();}catch (Exception e){e.printStackTrace();}finally {if (accessFile !=null) accessFile.close();if (stream!=null) stream.close();if (stream1!=null) stream1.close();}}總結
以上是生活随笔為你收集整理的IO流之过滤流介绍:的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IO流中的设计模式
- 下一篇: ConcurrentHashMap介绍