1 19 20 21 package org.netbeans.editor.ext; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.RandomAccessFile ; 26 27 28 33 public class FileAccessor implements DataAccessor{ 34 35 private File f; 36 private RandomAccessFile file; 37 38 39 40 public FileAccessor(File file) { 41 f = file; 42 } 43 44 51 public void append(byte[] buffer, int off, int len) throws IOException { 52 file.write(buffer, off, len); 53 } 54 55 60 61 public void read(byte[] buffer, int off, int len) throws IOException { 62 file.readFully(buffer, off, len); 63 } 64 65 68 public void open(boolean requestWrite) throws IOException { 69 file = new RandomAccessFile (f, requestWrite ? "rw" : "r"); if (!f.exists()){ 71 f.createNewFile(); 72 } 73 } 74 75 76 public void close() throws IOException { 77 if (file!=null){ 78 file.close(); 79 } 80 } 81 82 88 public long getFilePointer() throws IOException { 89 return file.getFilePointer(); 90 } 91 92 93 public void resetFile() throws IOException { 94 file.setLength(0); 95 } 96 97 public void seek(long pos) throws IOException { 98 file.seek(pos); 99 } 100 101 public int getFileLength() { 102 return (int)f.length(); 103 } 104 105 public String toString() { 106 return f.getAbsolutePath(); 107 } 108 109 } 110 | Popular Tags |