当前位置: 首页>>代码示例>>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;未经允许,请勿转载。