Package java.io

Examples of java.io.RandomAccessFile


        if (!isWritable()) {
            throw new IOException("No write permission : " + file.getName());
        }

        // create output stream
        final RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.setLength(offset);
        raf.seek(offset);

        // The IBM jre needs to have both the stream and the random access file
        // objects closed to actually close the file
        return new FileOutputStream(raf.getFD()) {
            @Override
            public void close() throws IOException {
                super.close();
                raf.close();
            }
        };
    }
View Full Code Here


        if (!isReadable()) {
            throw new IOException("No read permission : " + file.getName());
        }

        // move to the appropriate offset and create input stream
        final RandomAccessFile raf = new RandomAccessFile(file, "r");
        raf.seek(offset);

        // The IBM jre needs to have both the stream and the random access file
        // objects closed to actually close the file
        return new FileInputStream(raf.getFD()) {
            @Override
            public void close() throws IOException {
                super.close();
                raf.close();
            }
        };
    }
View Full Code Here

            return filepath.substring(0, index);
        }
    }

    public static void truncateFile(File file) {
        final RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(file, "rw");
        } catch (FileNotFoundException fnfe) {
            throw new IllegalStateException(fnfe);
        }
        try {
            raf.setLength(0);
        } catch (IOException ioe) {
            throw new IllegalStateException(ioe);
        } finally {
            IOUtils.closeQuietly(raf);
        }
View Full Code Here

                      }
                                       
                      Utilities utils = plugin_interface.getUtilities();
                     
                      try{
                        RandomAccessFile raf = new RandomAccessFile(f, "r" );
                       
                        while( !chunks.isEmpty()){
                         
                          long[] chunk = chunks.remove((int)(Math.random()*chunks.size()));
                         
                          final long  position   = chunk[0];
                          final int  size    = (int)chunk[1];
                         
                          raf.seek( position );
                         
                          byte[] buffer = new byte[ size ];
                         
                          raf.read( buffer );
                         
                          int  piece_number   = (int)(position/piece_size);
                          int  piece_offset  = (int)(position % piece_size);
                                             
                          dm.write(
View Full Code Here

      File fDescriptions = FileUtil.getUserFile("ipfilter.cache");
      try {
        if (fDescriptions.exists()) {
          fDescriptions.delete();
        }
        rafDescriptions = new RandomAccessFile(fDescriptions, "rw");
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } else if (!enable && rafDescriptions != null) {
View Full Code Here

     * @param file the file
     * @param data the data to write
     */
    public static void writeFile(File file, byte[] data) {
        try {
            RandomAccessFile ra = new RandomAccessFile(file, "rw");
            ra.write(data);
            ra.setLength(data.length);
            ra.close();
        } catch (IOException e) {
            throw new RuntimeException("Error writing to file " + file, e);
        }
    }
View Full Code Here

     */
    void parse(String baseDir, String className) {
        String fileName = baseDir + "/" + className.replace('.', '/') + ".java";
        current = new ParseState();
        try {
            RandomAccessFile file = new RandomAccessFile(fileName, "r");
            byte[] buff = new byte[(int) file.length()];
            file.readFully(buff);
            source = new String(buff, "UTF-8");
            file.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        source = replaceUnicode(source);
        source = removeRemarks(source);
View Full Code Here

    }

    private static void process(String fileName, String find,
            boolean head, boolean tail, long start, int lines,
            boolean quiet) throws IOException {
        RandomAccessFile file = new RandomAccessFile(fileName, "r");
        long length = file.length();
        if (head) {
            file.seek(start);
            list(start, "Head", readLines(file, lines));
        }
        if (find != null) {
            file.seek(start);
            long pos = find(file, find.getBytes(), quiet);
            if (pos >= 0) {
                file.seek(pos);
                list(pos, "Found " + find, readLines(file, lines));
            }
        }
        if (tail) {
            long pos = length - 100L * lines;
            ArrayList<String> list = null;
            while (pos > 0) {
                file.seek(pos);
                list = readLines(file, Integer.MAX_VALUE);
                if (list.size() > lines) {
                    break;
                }
                pos -= 100L * lines;
 
View Full Code Here

        return buff.toString();
    }

    private static void writeFile(File file, byte[] data) {
        try {
            RandomAccessFile ra = new RandomAccessFile(file, "rw");
            ra.write(data);
            ra.setLength(data.length);
            ra.close();
        } catch (IOException e) {
            throw new RuntimeException("Error writing to file " + file, e);
        }
    }
View Full Code Here

        FileSystem fs = FileSystem.getInstance(fsBase);
        String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false);
        File file = new File(TestBase.BASE_TEST_DIR + "/tmp");
        file.getParentFile().mkdirs();
        file.delete();
        RandomAccessFile ra = new RandomAccessFile(file, "rw");
        fs.delete(s);
        FileObject f = fs.openFileObject(s, "rw");
        assertThrows(EOFException.class, f).readFully(new byte[1], 0, 1);
        f.sync();
        Random random = new Random(seed);
        int size = getSize(100, 500);
        try {
            for (int i = 0; i < size; i++) {
                trace("op " + i);
                int pos = random.nextInt(10000);
                switch(random.nextInt(7)) {
                case 0: {
                    pos = (int) Math.min(pos, ra.length());
                    trace("seek " + pos);
                    buff.append("seek " + pos + "\n");
                    f.seek(pos);
                    ra.seek(pos);
                    break;
                }
                case 1: {
                    byte[] buffer = new byte[random.nextInt(1000)];
                    random.nextBytes(buffer);
                    trace("write " + buffer.length);
                    buff.append("write " + buffer.length + "\n");
                    f.write(buffer, 0, buffer.length);
                    ra.write(buffer, 0, buffer.length);
                    break;
                }
                case 2: {
                    trace("setLength " + pos);
                    f.setFileLength(pos);
                    ra.setLength(pos);
                    if (ra.getFilePointer() > pos) {
                        f.seek(0);
                        ra.seek(0);
                    }
                    buff.append("setLength " + pos + "\n");
                    break;
                }
                case 3: {
                    int len = random.nextInt(1000);
                    len = (int) Math.min(len, ra.length() - ra.getFilePointer());
                    byte[] b1 = new byte[len];
                    byte[] b2 = new byte[len];
                    trace("readFully " + len);
                    ra.readFully(b1, 0, len);
                    f.readFully(b2, 0, len);
                    buff.append("readFully " + len + "\n");
                    assertEquals(b1, b2);
                    break;
                }
                case 4: {
                    trace("getFilePointer");
                    buff.append("getFilePointer\n");
                    assertEquals(ra.getFilePointer(), f.getFilePointer());
                    break;
                }
                case 5: {
                    trace("length " + ra.length());
                    buff.append("length " + ra.length() + "\n");
                    assertEquals(ra.length(), f.length());
                    break;
                }
                case 6: {
                    trace("reopen");
                    buff.append("reopen\n");
                    f.close();
                    ra.close();
                    ra = new RandomAccessFile(file, "rw");
                    f = fs.openFileObject(s, "rw");
                    assertEquals(ra.length(), f.length());
                    break;
                }
                default:
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
            fail("Exception: " + e + "\n"+ buff.toString());
        } finally {
            f.close();
            ra.close();
            file.delete();
            fs.delete(s);
        }
    }
View Full Code Here

TOP

Related Classes of java.io.RandomAccessFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.