當前位置: 首頁>>代碼示例>>Java>>正文


Java FileOutputStream.getChannel方法代碼示例

本文整理匯總了Java中java.io.FileOutputStream.getChannel方法的典型用法代碼示例。如果您正苦於以下問題:Java FileOutputStream.getChannel方法的具體用法?Java FileOutputStream.getChannel怎麽用?Java FileOutputStream.getChannel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.io.FileOutputStream的用法示例。


在下文中一共展示了FileOutputStream.getChannel方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createMovie

import java.io.FileOutputStream; //導入方法依賴的package包/類
public MP4Builder createMovie(Mp4Movie mp4Movie) throws Exception {
    currentMp4Movie = mp4Movie;

    fos = new FileOutputStream(mp4Movie.getCacheFile());
    fc = fos.getChannel();

    FileTypeBox fileTypeBox = createFileTypeBox();
    fileTypeBox.getBox(fc);
    dataOffset += fileTypeBox.getSize();
    writedSinceLastMdat += dataOffset;

    mdat = new InterleaveChunkMdat();

    sizeBuffer = ByteBuffer.allocateDirect(4);

    return this;
}
 
開發者ID:fishwjy,項目名稱:VideoCompressor,代碼行數:18,代碼來源:MP4Builder.java

示例2: moveFile

import java.io.FileOutputStream; //導入方法依賴的package包/類
/**
 * Moves a file to another location
 * @param src source
 * @param dst destination
 * @throws IOException
 */
public static void moveFile(File src, File dst) throws IOException {
    if (!dst.exists()) {
        Log.i("New File", "" + dst.createNewFile());
    }

    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    if (!src.delete()){
        Log.e("moveFile", "Failed to delete file");
    }

    inStream.close();
    outStream.close();
}
 
開發者ID:feup-infolab,項目名稱:labtablet,代碼行數:24,代碼來源:FileMgr.java

示例3: copyFile

import java.io.FileOutputStream; //導入方法依賴的package包/類
static void copyFile(File sourceFile, FileOutputStream outputStream) throws IOException {

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = outputStream.getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
 
開發者ID:h4h13,項目名稱:RetroMusicPlayer,代碼行數:19,代碼來源:TaggerUtils.java

示例4: copyFile

import java.io.FileOutputStream; //導入方法依賴的package包/類
/**
 * Creates the specified <code>toFile</code> as a byte for byte copy of the
 * <code>fromFile</code>. If <code>toFile</code> already exists, then it
 * will be replaced with a copy of <code>fromFile</code>. The name and path
 * of <code>toFile</code> will be that of <code>toFile</code>.<br/>
 * <br/>
 * <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by
 * this function.</i>
 *
 * @param fromFile
 *            - FileInputStream for the file to copy from.
 * @param toFile
 *            - FileInputStream for the file to copy to.
 */
public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
    FileChannel fromChannel = null;
    FileChannel toChannel = null;
    try {
        fromChannel = fromFile.getChannel();
        toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        try {
            if (fromChannel != null) {
                fromChannel.close();
            }
        } finally {
            if (toChannel != null) {
                toChannel.close();
            }
        }
    }
}
 
開發者ID:abicelis,項目名稱:Remindy,代碼行數:34,代碼來源:FileUtil.java

示例5: copyFile

import java.io.FileOutputStream; //導入方法依賴的package包/類
public static void copyFile(File sourceFile, FileOutputStream outputStream) throws IOException {

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = outputStream.getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
 
開發者ID:reyanshmishra,項目名稱:Rey-MusicPlayer,代碼行數:19,代碼來源:FileUtils.java

示例6: onTouch

import java.io.FileOutputStream; //導入方法依賴的package包/類
@Override
public boolean onTouch(View v, MotionEvent ev) {

    if (ev.getAction() == MotionEvent.ACTION_UP)
    {
        mAdapter.add("" + ev.getAxisValue(0) + ", " + ev.getAxisValue(1));

        try {
            FileOutputStream fos = openFileOutput ("MousePos.txt", Context.MODE_APPEND);
            FileChannel outChannel = fos.getChannel();

            float[] pos = new float[2];
            pos[0] = ev.getAxisValue(0);
            pos[1] = ev.getAxisValue(1);

            ByteBuffer buff = ByteBuffer.allocate(4*2);
            buff.clear();
            buff.asFloatBuffer().put(pos);

            outChannel.write(buff);
            outChannel.close();
            fos.close();

            FileInputStream fis = openFileInput ("MousePos.txt");
            int a = fis.available();
            int b = 0;

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    return false;
}
 
開發者ID:jjuiddong,項目名稱:Android-Practice,代碼行數:35,代碼來源:MainActivity.java

示例7: tryLockInternal

import java.io.FileOutputStream; //導入方法依賴的package包/類
private static ProcessLock tryLockInternal(final String lockName, final String hash, final boolean writeMode) {
    synchronized (LOCK_MAP) {

        ConcurrentHashMap<Integer, ProcessLock> locks = LOCK_MAP.get(lockName);
        if (locks != null && !locks.isEmpty()) {
            Iterator<Map.Entry<Integer, ProcessLock>> itr = locks.entrySet().iterator();
            while (itr.hasNext()) {
                Map.Entry<Integer, ProcessLock> entry = itr.next();
                ProcessLock value = entry.getValue();
                if (value != null) {
                    if (!value.isValid()) {
                        itr.remove();
                    } else if (writeMode) {
                        return null;
                    } else if (value.mWriteMode) {
                        return null;
                    }
                } else {
                    itr.remove();
                }
            }
        }

        FileChannel channel = null;
        Closeable stream = null;
        try {
            File file = new File(
                    x.app().getDir(LOCK_FILE_DIR, Context.MODE_PRIVATE),
                    hash);
            if (file.exists() || file.createNewFile()) {

                if (writeMode) {
                    FileOutputStream out = new FileOutputStream(file, false);
                    channel = out.getChannel();
                    stream = out;
                } else {
                    FileInputStream in = new FileInputStream(file);
                    channel = in.getChannel();
                    stream = in;
                }
                if (channel != null) {
                    FileLock fileLock = channel.tryLock(0L, Long.MAX_VALUE, !writeMode);
                    if (isValid(fileLock)) {
                        ProcessLock result = new ProcessLock(lockName, file, fileLock, stream, writeMode);
                        LOCK_MAP.put(lockName, fileLock.hashCode(), result);
                        return result;
                    } else {
                        release(lockName, fileLock, file, stream);
                    }
                } else {
                    throw new IOException("can not get file channel:" + file.getAbsolutePath());
                }
            }
        } catch (Throwable ignored) {
            LogUtil.d("tryLock: " + lockName + ", " + ignored.getMessage());
            IOUtil.closeQuietly(stream);
            IOUtil.closeQuietly(channel);
        }
    }

    return null;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:63,代碼來源:ProcessLock.java

示例8: truncateFileToConsistentSize

import java.io.FileOutputStream; //導入方法依賴的package包/類
static void truncateFileToConsistentSize(FileOutputStream fos, long size) {
    try {
        FileChannel fch = fos.getChannel();
        fch.truncate(size);
        fch.force(true);
    } catch (IOException ex) {
        Logger.getLogger(DataConsistentFileOutputStream.class.getName()).log(
                Level.INFO,
                "Not able to truncate file to the data consistent size of "+size+" bytes.",
                ex);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:13,代碼來源:DataConsistentFileOutputStream.java

示例9: testRecoveryMode

import java.io.FileOutputStream; //導入方法依賴的package包/類
@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();
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:32,代碼來源:TestOfflineEditsViewer.java

示例10: save

import java.io.FileOutputStream; //導入方法依賴的package包/類
void save(File file, FSImageCompression compression) throws IOException {
  FileOutputStream fout = new FileOutputStream(file);
  fileChannel = fout.getChannel();
  try {
    saveInternal(fout, compression, file.getAbsolutePath());
  } finally {
    fout.close();
  }
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:10,代碼來源:FSImageFormatProtobuf.java

示例11: testConcurrentReadWrite

import java.io.FileOutputStream; //導入方法依賴的package包/類
public void testConcurrentReadWrite() throws Exception
{
    // open file for a read
    FileInputStream isRead = new FileInputStream(file);
    // open file for write
    FileOutputStream osWrite = new FileOutputStream(file);
    
    // get channels
    FileChannel channelRead = isRead.getChannel();
    FileChannel channelWrite = osWrite.getChannel();
    
    // buffers
    ByteBuffer bufferRead = ByteBuffer.allocate(26);
    ByteBuffer bufferWrite = ByteBuffer.wrap(TEST_CONTENT.getBytes());
    
    // read - nothing will be read
    int countRead = channelRead.read(bufferRead);
    assertEquals("Expected nothing to be read", -1, countRead);
    // write
    int countWrite = channelWrite.write(bufferWrite);
    assertEquals("Not all characters written", 26, countWrite);
    
    // close the write side
    channelWrite.close();
    
    // reread
    countRead = channelRead.read(bufferRead);
    assertEquals("Expected full read", 26, countRead);
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:30,代碼來源:FileIOTest.java

示例12: create

import java.io.FileOutputStream; //導入方法依賴的package包/類
/**
 * Initialize a new file, write the empty structure to disk and return a new
 * accessor to it
 * 
 * @param file
 *            The file to create. This file must not exists.
 * @param startDate
 *            the start date of the file
 * @param endDate
 *            the end date of the file
 * @return a newly created file accessor
 * @throws Exception
 *             if anything goes wrong
 */
public static DataFileAccessorImpl create ( final File file, final Date startDate, final Date endDate ) throws Exception
{
    logger.debug ( "Creating new file: {}", file );

    if ( !file.createNewFile () )
    {
        throw new IllegalStateException ( String.format ( "Unable to create file %s, already exists", file ) );
    }

    final FileOutputStream out = new FileOutputStream ( file );

    try
    {
        final FileChannel channel = out.getChannel ();

        final ByteBuffer buffer = ByteBuffer.allocate ( 100 );
        buffer.putInt ( 0x1202 ); // magic marker
        buffer.putInt ( 0x0101 ); // version
        buffer.putLong ( startDate.getTime () ); // start timestamp
        buffer.putLong ( endDate.getTime () ); // end timestamp

        buffer.flip ();

        while ( buffer.hasRemaining () )
        {
            final int rc = channel.write ( buffer );
            logger.debug ( "Header written - {} bytes", rc );
        }

        return new DataFileAccessorImpl ( file );
    }
    finally
    {
        out.close ();
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:51,代碼來源:DataFileAccessorImpl.java

示例13: createMovie

import java.io.FileOutputStream; //導入方法依賴的package包/類
private void createMovie(File outputFile) throws IOException {
    fos = new FileOutputStream(outputFile);
    fc = fos.getChannel();
    mdat = new InterleaveChunkMdat();
    mdatOffset = 0;

    FileTypeBox fileTypeBox = createFileTypeBox();
    fileTypeBox.getBox(fc);
    recFileSize += fileTypeBox.getSize();
}
 
開發者ID:lisnstatic,項目名稱:live_master,代碼行數:11,代碼來源:SrsMp4Muxer.java

示例14: copyFile

import java.io.FileOutputStream; //導入方法依賴的package包/類
private static void copyFile(File source, File destination) throws IOException {
    FileInputStream inStream = new FileInputStream(source);
    FileOutputStream outStream = new FileOutputStream(destination);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        inStream.close();
        outStream.close();
    }

}
 
開發者ID:fekracomputers,項目名稱:IslamicLibraryAndroid,代碼行數:14,代碼來源:StorageUtils.java

示例15: getXMLFileFromServer

import java.io.FileOutputStream; //導入方法依賴的package包/類
public void getXMLFileFromServer()
{

    if(PropertiesHandler.getProperties("disable3dsdbxml") != null)
    {
        if(PropertiesHandler.getProperties("disable3dsdbxml").equals("no"))
        {
            try
            {
                DebugLogger.log("INIT: Downloading 3dsdb newest XML database...", Level.INFO);
                File tempFile = File.createTempFile("3dsdb", Long.toString(System.nanoTime()));
                ReadableByteChannel in = Channels.newChannel(new URL("http://3dsdb.com/xml.php").openStream());
                FileOutputStream fos = new FileOutputStream(tempFile);
                FileChannel out = fos.getChannel();

                out.transferFrom(in, 0L, Long.MAX_VALUE);
                in.close();
                out.close();
                fos.close();

                this.pathToDB = tempFile.getPath();
                DebugLogger.log("INIT: Download completed!", Level.INFO);
            }
            catch (Exception e)
            {
                DebugLogger.log("INIT: Download failed! Using local file", Level.WARNING);
                getXMLFileFromJar();
            }
        }
    }
}
 
開發者ID:Ptrk25,項目名稱:CDN-FX-2.2,代碼行數:32,代碼來源:XMLHandler.java


注:本文中的java.io.FileOutputStream.getChannel方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。