Example usage for java.io RandomAccessFile seek

List of usage examples for java.io RandomAccessFile seek

Introduction

In this page you can find the example usage for java.io RandomAccessFile seek.

Prototype

public void seek(long pos) throws IOException 

Source Link

Document

Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Usage

From source file:edu.usc.qufd.Main.java

/**
 * The main method./*ww  w .j av  a 2  s .c  om*/
 *
 * @param args the arguments
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void main(String[] args) throws IOException {
    if (parseInputs(args) == false) {
        System.exit(-1); //The input files do not exist
    }

    /*
     * Parsing inputs: fabric & qasm file
     */
    PrintWriter outputFile;
    RandomAccessFile raf = null;
    String latencyPlaceHolder;
    if (RuntimeConfig.OUTPUT_TO_FILE) {
        latencyPlaceHolder = "Total Latency: " + Long.MAX_VALUE + " us" + System.lineSeparator();
        raf = new RandomAccessFile(outputFileAddr, "rws");
        //removing the old values in the file
        raf.setLength(0);
        //writing a place holder for the total latency
        raf.writeBytes(latencyPlaceHolder);
        raf.close();

        outputFile = new PrintWriter(new BufferedWriter(new FileWriter(outputFileAddr, true)), true);
    } else { //writing to stdout
        outputFile = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), true);
    }
    /* parsing the input*/
    layout = LayoutParser.parse(pmdFileAddr);
    qasm = QASMParser.QASMParser(qasmFileAddr, layout);

    long totalLatency = qufd(outputFile);

    if (RuntimeConfig.OUTPUT_TO_FILE) {
        outputFile.close();
        //Over writing the place holder with the actual latency
        String latencyActual = "Total Latency: " + totalLatency + " " + layout.getTimeUnit();
        latencyActual = StringUtils.rightPad(latencyActual,
                latencyPlaceHolder.length() - System.lineSeparator().length());
        raf = new RandomAccessFile(outputFileAddr, "rws");
        //Writing to the top of a file
        raf.seek(0);
        //writing the actual total latency in the at the top of the output file
        raf.writeBytes(latencyActual + System.lineSeparator());
        raf.close();
    } else {
        outputFile.flush();
        System.out.println("Total Latency: " + totalLatency + " " + layout.getTimeUnit());
    }

    if (RuntimeConfig.VERBOSE) {
        System.out.println("Done.");
    }
    outputFile.close();
}

From source file:Main.java

public static void append(String fileName, String text) throws Exception {
    File f = new File(fileName);
    long fileLength = f.length();
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    raf.seek(fileLength);
    raf.writeBytes(text);//www  .  ja  v a2  s.c  om
    raf.close();
}

From source file:Main.java

public static void append(String fileName, byte[] bytes) throws Exception {
    File f = new File(fileName);
    long fileLength = f.length();
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    raf.seek(fileLength);
    raf.write(bytes);/* w ww .  j  a va 2s.  c  o m*/
    raf.close();
}

From source file:Main.java

/**
 * RandomAccessFile seek and readFully/* w  ww.  j  a  va2  s. co  m*/
 *
 * @param raf
 * @param index
 * @param buffer
 * @throws IOException
 */
private static void readFully(RandomAccessFile raf, long index, byte[] buffer) throws IOException {
    raf.seek(index);
    raf.readFully(buffer);
}

From source file:Main.java

public static void incrementReadCounter(RandomAccessFile raf) throws IOException {
    long currentPosition = raf.getFilePointer();
    raf.seek(0);
    int counter = raf.readInt();
    counter++;//from ww w. j a va 2 s. c  om
    raf.seek(0);
    raf.writeInt(counter);
    raf.seek(currentPosition);
}

From source file:Main.java

public static void readStreamToFile(InputStream inStream, String filePath) throws Exception {
    File file = new File(filePath + ".wei");
    RandomAccessFile outStream = new RandomAccessFile(file, "rw");
    outStream.seek(0);
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }/*w  w  w .  j  a  v a2  s  .c o m*/
    outStream.close();
    inStream.close();
    file.renameTo(new File(filePath));
    return;
}

From source file:Main.java

public static void WriteTxtFile(Context context, String strcontent) {

    String strContent = strcontent + "\n";
    try {/*from   w  w  w . jav  a 2s.  c  om*/
        File file = new File(context.getCacheDir() + File.separator + "wanjia.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(file.length());
        raf.write(strContent.getBytes());
        raf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean appendFile(String filename, byte[] data, int datapos, int datalength) {
    try {//from ww  w.  j a  v a2s.c  o m

        createFile(filename);

        RandomAccessFile rf = new RandomAccessFile(filename, "rw");
        rf.seek(rf.length());
        rf.write(data, datapos, datalength);
        if (rf != null) {
            rf.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return true;
}

From source file:com.feilong.tools.net.ZClientTest.java

/**
 * Gets the files./*from  w ww .  j  a  v a  2  s . c  o m*/
 * 
 * @param ftp
 *            the ftp
 * @param localDir
 *            the local dir
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private static void testGetFiles(FTPClient ftp, File localDir) throws IOException {
    String[] names = ftp.listNames();
    for (String name : names) {
        File file = new File(localDir.getPath() + File.separator + name);
        if (!file.exists()) {
            file.createNewFile();
        }
        long pos = file.length();
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(pos);
        ftp.setRestartOffset(pos);
        InputStream is = ftp.retrieveFileStream(name);
        if (is == null) {
            log.info("no such file:" + name);
        } else {
            log.info("start getting file:" + name);
            int b;
            while ((b = is.read()) != -1) {
                raf.write(b);
            }
            is.close();
            if (ftp.completePendingCommand()) {
                log.info("done!");
            } else {
                log.info("can't get file:" + name);
            }
        }
        raf.close();
    }
}

From source file:Main.java

public static short[] fromAudioFile(RandomAccessFile f) {
    short[] samples = null;

    try {/*  www  .  j a va 2 s  . c o  m*/
        samples = new short[(int) (f.length() / 2)];
        f.seek(0); //Seek to start point of file

        for (int i = 0; i < f.length() / 2; i++) {
            samples[i] = f.readShort();
        }
        f.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return samples;
}