Example usage for java.nio.channels FileChannel truncate

List of usage examples for java.nio.channels FileChannel truncate

Introduction

In this page you can find the example usage for java.nio.channels FileChannel truncate.

Prototype

public abstract FileChannel truncate(long size) throws IOException;

Source Link

Document

Truncates this channel's file to the given size.

Usage

From source file:Main.java

public static long truncateFile(String fileName, long size) throws FileNotFoundException, IOException {
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");

    if (raf.length() >= size) {
        FileChannel channel = raf.getChannel();
        channel.truncate(size);
        return size;
    }//from  www. java  2 s. c o  m

    return raf.length();
}

From source file:com.github.neoio.nio.util.NIOUtils.java

public static void resetFileChannel(FileChannel channel) throws NetIOException {
    try {/*w  w w  .j  a v  a  2 s  . com*/
        channel.position(0);
        channel.truncate(0);
    } catch (IOException e) {
        throw new NetIOException(e);
    }
}

From source file:com.sakadream.sql.SQLConfigJson.java

/**
 * Save SQLConfig to config.json/*from  w  w w.j  a v  a2 s .c  om*/
 * @param sqlConfig SQLConfig object
 * @param encrypt Are you want to encrypt config.json?
 * @throws Exception
 */
public static void save(SQLConfig sqlConfig, Boolean encrypt) throws Exception {
    File file = new File(path);
    if (!file.exists()) {
        file.createNewFile();
    } else {
        FileChannel outChan = new FileOutputStream(file, true).getChannel();
        outChan.truncate(0);
        outChan.close();
    }
    FileWriter writer = new FileWriter(file);
    String json = gson.toJson(sqlConfig, type);
    if (encrypt) {
        Security security = new Security();
        writer.write(security.encrypt(json));
    } else {
        writer.write(json);
    }
    writer.close();
}

From source file:com.sakadream.sql.SQLConfigJson.java

/**
 * Save SQLConfig to custom file name/*from   w  w  w  .  ja v  a  2 s .c  om*/
 * @param fileName SQLConfig file path
 * @param sqlConfig SQLConfig object
 * @param encrypt Are you want to encrypt config.json?
 * @throws Exception
 */
public static void save(String fileName, SQLConfig sqlConfig, Boolean encrypt) throws Exception {
    fileName = Utilis.changeFileName(fileName, "json");
    File file = new File(fileName);
    if (!file.exists()) {
        file.createNewFile();
    } else {
        FileChannel outChan = new FileOutputStream(file, true).getChannel();
        outChan.truncate(0);
        outChan.close();
    }
    FileWriter writer = new FileWriter(file);
    String json = gson.toJson(sqlConfig, type);
    if (encrypt) {
        Security security = new Security();
        writer.write(security.encrypt(json));
    } else {
        writer.write(json);
    }
    writer.close();
}

From source file:graphene.util.fs.FileUtils.java

public static void truncateFile(final FileChannel fileChannel, final long position) throws IOException {
        int count = 0;
        boolean success = false;
        do {/*from  w  ww  .  j a va  2s. c  o  m*/
            count++;
            try {
                fileChannel.truncate(position);
                success = true;
            } catch (final IOException e) {
            }

        } while (!success && (count <= WINDOWS_RETRY_COUNT));
        if (!success) {
            throw new IOException("Failure to truncateFile " + fileChannel.toString());
        }
    }

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ?/*from w w  w  .j  av a2 s .  co m*/
 *
 * @param filePath
 *            ?
 * @param size
 *            size?
 * @return 
 */
public static long truncateFile(String fileName, long size) throws FileNotFoundException, IOException {
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");

    if (raf.length() >= size) {
        FileChannel channel = raf.getChannel();
        channel.truncate(size);
        return size;
    }

    return raf.length();
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestDirectoryScannerInlineFiles.java

private void truncateFile(File f) throws IOException {
    assertTrue(f.exists() && f.length() != 0);
    FileOutputStream s = new FileOutputStream(f);
    FileChannel channel = s.getChannel();
    channel.truncate(0);
    LOG.info("Truncated block file " + f.getAbsolutePath());
    s.close();//ww  w .  j a v  a 2  s.c  o m
}

From source file:org.apache.usergrid.chop.runner.drivers.ResultsLog.java

@Override
public void truncate() throws IOException {
    if (isOpen.get()) {
        throw new IOException("Cannot truncate while log is open for writing. Close the log then truncate.");
    }// w  w w.j av a2 s.co m

    // Synchronize on isOpen to prevent re-opening while truncating (rare)
    synchronized (isOpen) {
        File results = new File(resultsFile.get());
        FileChannel channel = new FileOutputStream(results, true).getChannel();
        channel.truncate(0);
        channel.close();
        resultCount.set(0);
    }
}

From source file:org.apache.hadoop.hdfs.tools.offlineEditsViewer.TestOfflineEditsViewer.java

@Test
public void testRecoveryMode() throws IOException {
    // edits generated by nnHelper (MiniDFSCluster), should have all op codes
    // binary, XML, reparsed binary
    String edits = nnHelper.generateEdits();
    FileOutputStream os = new FileOutputStream(edits, true);
    // Corrupt the file by truncating the end
    FileChannel editsFile = os.getChannel();
    editsFile.truncate(editsFile.size() - 5);

    String editsParsedXml = folder.newFile("editsRecoveredParsed.xml").getAbsolutePath();
    String editsReparsed = folder.newFile("editsRecoveredReparsed").getAbsolutePath();
    String editsParsedXml2 = folder.newFile("editsRecoveredParsed2.xml").getAbsolutePath();

    // Can't read the corrupted file without recovery mode
    assertEquals(-1, runOev(edits, editsParsedXml, "xml", false));

    // parse to XML then back to binary
    assertEquals(0, runOev(edits, editsParsedXml, "xml", true));
    assertEquals(0, runOev(editsParsedXml, editsReparsed, "binary", false));
    assertEquals(0, runOev(editsReparsed, editsParsedXml2, "xml", false));

    // judgment time
    assertTrue("Test round trip",
            FileUtils.contentEqualsIgnoreEOL(new File(editsParsedXml), new File(editsParsedXml2), "UTF-8"));

    os.close();/*from ww  w . ja  va2 s  . co  m*/
}

From source file:gov.nih.nci.firebird.selenium2.framework.AbstractFirebirdWebDriverTest.java

private void truncateJbossLog() throws IOException {
    FileChannel outChan = new FileOutputStream(serverLog, true).getChannel();
    outChan.truncate(0);
    outChan.close();/*from   ww  w.j a v a  2s  .  co m*/
}