Example usage for java.io RandomAccessFile setLength

List of usage examples for java.io RandomAccessFile setLength

Introduction

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

Prototype

public native void setLength(long newLength) throws IOException;

Source Link

Document

Sets the length of this file.

Usage

From source file:BridgeBasics.java

public static void main(String args[]) throws Exception {
    RServices rs = DirectJNI.getInstance().getRServices();
    rs.consoleSubmit("k=function(x){x*5}");

    System.out// ww w  . j av a 2s . co m
            .println("////////////////////////////////" + rs.callAndConvert(new RFunctionObjectName("k"), 45));

    //rs.putAndAssign(new RUnknown(o.getValue()), "b");
    //System.out.println("-------------------------------"+rs.print("h"));

    /*
            
    RServices rs =
       ServerManager.createR("toto"); 
            
       RObject ro1 = 
    rs.getObject("structure(list(1,2), caption = \"foo\")");
       RObject ro2 = 
    rs.getReference("structure(list(1,2), caption = \"foo\")");
       System.out.println(rs.callAndConvert("str", ro1));
       System.out.println(rs.callAndConvert("str", ro2));
            
            
    rs.die();
            
    if (true) return;
    */

    rs = DirectJNI.getInstance().getRServices();
    GDDevice device = rs.newDevice(400, 400);
    rs.consoleSubmit("plot(pressure)");
    System.out.println(rs.getStatus());
    byte[] buffer = device.getPng();
    RandomAccessFile raf = new RandomAccessFile("c:/te.png", "rw");
    raf.setLength(0);
    raf.write(buffer);
    raf.close();

    //rs.evaluate("x=2;y=8",2);

    /*
    RS3 s3=(RS3)rs.getReference("packageDescription('stats')");
    System.out.println("s="+Arrays.toString(s3.getClassAttribute()));
    s3.setClassAttribute(new String[] {s3.getClassAttribute()[0], "aaa"});
    rs.assignReference("f",s3);
    //rs.call("print",new RObjectName("f"));
    //System.out.println("log=" + rs.getStatus());
            
    rs.consoleSubmit("print(class(f))");
    System.out.println("log=" + rs.getStatus());
            
    RChar s = (RChar) rs.call("paste", new RChar("str1"), new RChar("str2"), new RNamedArgument("sep", new RChar(
    "--")));
    System.out.println("s=" + s);
    */

    System.exit(0);

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   ww  w  .  jav a 2s  . c  om*/
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeUTF("Hello World from java2s.com");

        // set the file pointer at 0 position
        raf.seek(0);

        // print the string
        System.out.println(raf.readUTF());

        // print current length
        System.out.println(raf.length());

        // set the file length to 30
        raf.setLength(30);

        System.out.println(raf.length());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

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

/**
 * The main method.// ww  w  . j ava2s . 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 boolean createEmptyFile(String path, long size) throws IOException {
    File file = new File(path);
    File parent = file.getParentFile();
    parent.mkdirs();//from ww  w  . j  a v a  2  s. com
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    raf.setLength(size);
    raf.close();
    return true;
}

From source file:Main.java

public static boolean createEmptyFile(String path, long size) throws IOException {
    File file = new File(path);
    File parent = file.getParentFile();
    parent.mkdirs();// ww  w .  ja va2  s.c  o  m
    RandomAccessFile raf = null;
    raf = new RandomAccessFile(file, "rw");
    raf.setLength(size);
    raf.close();
    return true;
}

From source file:org.stem.db.FatFileAllocator.java

public static void allocateFile(String filePath, long sizeInMB, boolean mark) throws IOException {
    long started = System.currentTimeMillis();

    Closer closer = Closer.create();//from w w  w.  ja  v  a 2 s  .co m
    try {
        File file = new File(filePath);
        if (file.exists())
            throw new IOException(String.format("File already exists: %s", filePath));

        RandomAccessFile rw = closer.register(new RandomAccessFile(file, "rw"));
        rw.setLength(sizeInMB * 1024 * 1024);
        if (mark) {
            rw.seek(0);
            rw.writeByte(FatFile.MARKER_BLANK);
            rw.seek(rw.length() - 1);
            rw.writeByte(FatFile.MARKER_BLANK);
        }
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
        logger.debug("{} was allocated in {} ms", filePath, System.currentTimeMillis() - started);
    }
}

From source file:net.timewalker.ffmq4.storage.data.impl.BlockBasedDataStoreTools.java

private static void initDataFile(File dataFile, int blockCount, int blockSize, boolean forceSync)
        throws DataStoreException {
    log.debug("Creating an empty map file (size=" + blockCount + "x" + blockSize + ") ...");

    // Create an empty file
    try {/*from   w  w w. j  a  va2  s .c  o  m*/
        RandomAccessFile dataFileMap = new RandomAccessFile(dataFile, "rw");
        dataFileMap.setLength((long) blockSize * blockCount);
        if (forceSync)
            dataFileMap.getFD().sync();
        dataFileMap.close();
    } catch (IOException e) {
        throw new DataStoreException("Cannot initialize map file " + dataFile.getAbsolutePath(), e);
    }
}

From source file:com.abiquo.appliancemanager.ApplianceManagerAsserts.java

protected static File createUploadTempFile() throws IOException {
    Random rnd = new Random(System.currentTimeMillis());
    final String fileName = String.valueOf(rnd.nextLong());
    File file = File.createTempFile(fileName, ".uploadTest");

    RandomAccessFile f = new RandomAccessFile(file, "rw");
    f.setLength(UPLOAD_FILE_SIZE_BYTES);

    file.deleteOnExit();//from   www  .  jav  a 2  s.co  m

    return file;
}

From source file:org.srlutils.Files.java

/** write txt to filename */
public static void writetofile(String txt, String filename) {
    try {//from w w w  .j  a  v a2  s . c  om
        String mode = "rw";
        RandomAccessFile fid = new RandomAccessFile(filename, mode);
        fid.setLength(0);
        fid.write(txt.getBytes());
        fid.close();
    } catch (Exception ex) {
        throw rte(ex, "failed to write string: %s, to file: %s", Text.summary(txt), filename);
    }
}

From source file:org.finra.herd.core.AbstractCoreTest.java

/**
 * Creates a file of the specified size relative to the base directory.
 *
 * @param baseDirectory the local parent directory path, relative to which we want our file to be created
 * @param file the file path (including file name) relative to the base directory for the file to be created
 * @param size the file size in bytes//from   w ww. j  av a  2 s  .c o  m
 *
 * @return the created file
 */
public static File createLocalFile(String baseDirectory, String file, long size) throws IOException {
    Path filePath = Paths.get(baseDirectory, file);
    // We don't check the "mkdirs" response because the directory may already exist which would return false.
    // But we want to create sub-directories if they don't yet exist which is why we're calling "mkdirs" in the first place.
    // If an actual directory couldn't be created, then the new file below will throw an exception anyway.
    filePath.toFile().getParentFile().mkdirs();
    RandomAccessFile randomAccessFile = new RandomAccessFile(filePath.toString(), "rw");
    randomAccessFile.setLength(size);
    randomAccessFile.close();
    return filePath.toFile();
}