当前位置: 首页>>代码示例>>Java>>正文


Java RandomAccessFile.write方法代码示例

本文整理汇总了Java中java.io.RandomAccessFile.write方法的典型用法代码示例。如果您正苦于以下问题:Java RandomAccessFile.write方法的具体用法?Java RandomAccessFile.write怎么用?Java RandomAccessFile.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.io.RandomAccessFile的用法示例。


在下文中一共展示了RandomAccessFile.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: insertContent

import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static void insertContent(String path, long offset, byte[] content) throws IOException {
  RandomAccessFile sourceRaf = new RandomAccessFile(path, "rw");
  File tempFile = FileUtil.createFile(path + ".tmp");
  RandomAccessFile tempRaf = new RandomAccessFile(tempFile, "rw");
  long fileSize = sourceRaf.length();
  FileChannel sourceChannel = sourceRaf.getChannel();
  FileChannel targetChannel = tempRaf.getChannel();
  long remaining = fileSize - offset;
  long position = offset;
  while (remaining > 0) {
    long transferred = sourceChannel.transferTo(position, remaining, targetChannel);
    remaining -= transferred;
    position += transferred;
  }
  sourceChannel.truncate(offset);
  sourceRaf.seek(offset);
  sourceRaf.write(content);
  long newOffset = sourceRaf.getFilePointer();
  targetChannel.position(0);
  sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
  sourceChannel.close();
  targetChannel.close();
  FileUtil.deleteIfExists(tempFile);
}
 
开发者ID:monkeyWie,项目名称:proxyee-down,代码行数:25,代码来源:ByteUtil.java

示例2: testCorruptCheckpointCompleteMarkerMostSignificant4Bytes

import java.io.RandomAccessFile; //导入方法依赖的package包/类
@Test
public void testCorruptCheckpointCompleteMarkerMostSignificant4Bytes() throws Exception {
  Map<String, String> overrides = Maps.newHashMap();
  channel = createFileChannel(overrides);
  channel.start();
  Assert.assertTrue(channel.isOpen());
  Set<String> in = putEvents(channel, "restart", 10, 100);
  Assert.assertEquals(100, in.size());
  forceCheckpoint(channel);
  channel.stop();
  File checkpoint = new File(checkpointDir, "checkpoint");
  RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
  writer.seek(EventQueueBackingStoreFile.INDEX_CHECKPOINT_MARKER *
              Serialization.SIZE_OF_LONG);
  writer.write(new byte[] { (byte) 1, (byte) 5 });
  writer.getFD().sync();
  writer.close();
  channel = createFileChannel(overrides);
  channel.start();
  Assert.assertTrue(channel.isOpen());
  Set<String> out = consumeChannel(channel);
  Assert.assertTrue(channel.didFullReplayDueToBadCheckpointException());
  compareInputAndOut(in, out);
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:25,代码来源:TestFileChannelRestart.java

示例3: corruptBlock

import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static boolean corruptBlock(File blockFile) throws IOException {
  if (blockFile == null || !blockFile.exists()) {
    return false;
  }
  // Corrupt replica by writing random bytes into replica
  Random random = new Random();
  RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw");
  FileChannel channel = raFile.getChannel();
  String badString = "BADBAD";
  int rand = random.nextInt((int)channel.size()/2);
  raFile.seek(rand);
  raFile.write(badString.getBytes());
  raFile.close();
  LOG.warn("Corrupting the block " + blockFile);
  return true;
}
 
开发者ID:naver,项目名称:hadoop,代码行数:17,代码来源:MiniDFSCluster.java

示例4: writeTxtToFile

import java.io.RandomAccessFile; //导入方法依赖的package包/类
public void writeTxtToFile(String strcontent, String filePath, String fileName) {
    // �����ļ���֮���������ļ�����Ȼ�����
    makeFilePath(filePath, fileName);
	 
    String strFilePath = filePath + fileName;
    // ÿ��д��ʱ��������д
    String strContent = strcontent + "\r\n";
    try {
        File file = new File(strFilePath);
        if (!file.exists()) {
            Log.d("TestFile", "Create the file:" + strFilePath);
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rwd");
        raf.seek(file.length());
        raf.write(strContent.getBytes());
        raf.close();
	 
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:Edward7Zhang,项目名称:oneKey2Alarm,代码行数:24,代码来源:aid.java

示例5: allocate

import java.io.RandomAccessFile; //导入方法依赖的package包/类
protected static void allocate(File file, long totalBytes) throws IOException {
  RandomAccessFile checkpointFile = new RandomAccessFile(file, "rw");
  boolean success = false;
  try {
    if (totalBytes <= MAX_ALLOC_BUFFER_SIZE) {
      /*
       * totalBytes <= MAX_ALLOC_BUFFER_SIZE, so this can be cast to int
       * without a problem.
       */
      checkpointFile.write(new byte[(int) totalBytes]);
    } else {
      byte[] initBuffer = new byte[MAX_ALLOC_BUFFER_SIZE];
      long remainingBytes = totalBytes;
      while (remainingBytes >= MAX_ALLOC_BUFFER_SIZE) {
        checkpointFile.write(initBuffer);
        remainingBytes -= MAX_ALLOC_BUFFER_SIZE;
      }
      /*
       * At this point, remainingBytes is < MAX_ALLOC_BUFFER_SIZE,
       * so casting to int is fine.
       */
      if (remainingBytes > 0) {
        checkpointFile.write(initBuffer, 0, (int) remainingBytes);
      }
    }
    success = true;
  } finally {
    try {
      checkpointFile.close();
    } catch (IOException e) {
      if (success) {
        throw e;
      }
    }
  }
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:37,代码来源:EventQueueBackingStoreFile.java

示例6: writeNewMAC

import java.io.RandomAccessFile; //导入方法依赖的package包/类
private void writeNewMAC(String str) throws IOException {
    File fl = new File(MainPage.dataPath);
    RandomAccessFile raf = new RandomAccessFile(fl, "rw");
    try {
        raf.seek(4); // offset = 4 ( Начало MAC )
        raf.write(hexStringToByteArray(str)); // Пишем байты из hexString в файл
    } finally {
        raf.close(); // flush + close
    }
}
 
开发者ID:olegsvs,项目名称:ru.olegsvs.mediatek_mac_changer,代码行数:11,代码来源:MacTools.java

示例7: createFile

import java.io.RandomAccessFile; //导入方法依赖的package包/类
private void createFile(File newFile, int size) throws IOException {
  // write random data so that filesystems with compression enabled (e.g., ZFS)
  // can't compress the file
  Random random = new Random();
  byte[] data = new byte[size];
  random.nextBytes(data);

  newFile.createNewFile();
  RandomAccessFile file = new RandomAccessFile(newFile, "rws");

  file.write(data);
    
  file.getFD().sync();
  file.close();
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:16,代码来源:TestDU.java

示例8: corrupt

import java.io.RandomAccessFile; //导入方法依赖的package包/类
@Override
public void corrupt(File editFile) throws IOException {
  // Add junk to the end of the file
  RandomAccessFile rwf = new RandomAccessFile(editFile, "rw");
  rwf.seek(editFile.length());
  rwf.write((byte)-1);
  for (int i = 0; i < 1024; i++) {
    rwf.write(padByte);
  }
  rwf.close();
}
 
开发者ID:naver,项目名称:hadoop,代码行数:12,代码来源:TestNameNodeRecovery.java

示例9: shift

import java.io.RandomAccessFile; //导入方法依赖的package包/类
/** Copy file.content[from...f.length-1] into file.content[to...], then truncate the file after that point.
 * <p> If (from &gt; to), this means we simply delete the portion of the file beginning at "to" and up to but excluding "from".
 * <p> If (from &lt; to), this means we insert (to-from) number of ARBITRARY bytes into the "from" location
 *     and shift the original file content accordingly.
 * <p> Note: after this operation, the file's current position will be moved to the start of the file.
 * @throws IOException if (from &lt; 0) || (to &lt; 0) || (from &gt;= file.length())
 */
public static void shift (RandomAccessFile file, long from, long to) throws IOException {
   long total = file.length();
   if (from<0 || from>=total || to<0) throw new IOException(); else if (from==to) {file.seek(0); return;}
   final byte buf[] = new byte[4096];
   int res;
   if (from>to) {
      while(true) {
         file.seek(from);
         if ((res=file.read(buf)) <= 0) { file.setLength(to); file.seek(0); return; }
         file.seek(to); file.write(buf, 0, res); from=from+res; to=to+res;
      }
   } else {
      file.seek(total);
      for(long todo=to-from; todo>0;) {
         if (todo >= buf.length) {file.write(buf); todo = todo - buf.length;} else {file.write(buf, 0, (int)todo); break;}
      }
      for(long todo=total-from; todo>0; total=total-res, todo=todo-res) {
         if (todo > buf.length) res=buf.length; else res=(int)todo;
         file.seek(total - res);
         for(int done=0; done<res;) { int r=file.read(buf, done, res-done); if (r<=0) throw new IOException(); else done += r; }
         file.seek(total - res + (to - from));
         file.write(buf, 0, res);
      }
   }
   file.seek(0);
}
 
开发者ID:ModelWriter,项目名称:Tarski,代码行数:34,代码来源:Util.java

示例10: saveFileUsingRandomAccessFile

import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
 * save file
 *
 * @param destFile
 * @param fileData
 */
private void saveFileUsingRandomAccessFile(File destFile, byte[] fileData) {
    try {
        RandomAccessFile randomAccessFile = new RandomAccessFile(destFile, "rw");
        randomAccessFile.write(fileData);
        randomAccessFile.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
开发者ID:alibaba,项目名称:LuaViewPlayground,代码行数:16,代码来源:ScriptBundleUltimateLoadTask.java

示例11: writeByteInFile

import java.io.RandomAccessFile; //导入方法依赖的package包/类
private synchronized void writeByteInFile(byte[] b, long pos) {
	try {
		RandomAccessFile raf = new RandomAccessFile(out, "rw");
		try {
			raf.seek(pos);
			raf.write(b);
		} finally {
			raf.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
开发者ID:BenjaminAlbrecht84,项目名称:DAA_Converter,代码行数:14,代码来源:DAA_Writer.java

示例12: write

import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
 * Write the contents of this datatype to the file at the position it is
 * currently at.
 *
 * @param file destination file
 * @throws IOException on any I/O error
 */
public void write(RandomAccessFile file) throws IOException {
    //Write the various fields to file in order
    byte[] buffer;
    AbstractDataType object;
    Iterator<AbstractDataType> iterator = objectList.listIterator();
    while (iterator.hasNext()) {
        object = iterator.next();
        buffer = object.writeByteArray();
        file.write(buffer);
    }
}
 
开发者ID:openaudible,项目名称:openaudible,代码行数:19,代码来源:AbstractLyrics3v2FieldFrameBody.java

示例13: write

import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
 * @param file
 * @throws java.io.IOException
 */
public void write(RandomAccessFile file) throws java.io.IOException
{
    int size;
    int offset = 0;
    byte[] buffer = new byte[5];
    String str;

    size = getSize();
    str = Integer.toString(size);

    for (int i = 0; i < (5 - str.length()); i++)
    {
        buffer[i] = (byte) '0';
    }

    offset += (5 - str.length());

    for (int i = 0; i < str.length(); i++)
    {
        buffer[i + offset] = (byte) str.charAt(i);
    }

    offset += str.length();
    file.write(buffer, 0, 5);

    if (size > 0)
    {
        str = writeString();
        buffer = new byte[str.length()];

        for (int i = 0; i < str.length(); i++)
        {
            buffer[i] = (byte) str.charAt(i);
        }

        file.write(buffer);
    }
}
 
开发者ID:GlennioTech,项目名称:MetadataEditor,代码行数:43,代码来源:FieldFrameBodyLYR.java

示例14: corruptFile

import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
 * corrupt a file by writing m at 500 b
 * offset
 * @param file the file to be corrupted
 * @throws IOException
 */
private void corruptFile(File file) throws IOException {
    // corrupt the logfile
    RandomAccessFile raf  = new RandomAccessFile(file, "rw");
    byte[] b = "mahadev".getBytes();
    long writeLen = 500L;
    raf.seek(writeLen);
    //corrupting the data
    raf.write(b);
    raf.close();
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:17,代码来源:CRCTest.java

示例15: write

import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
 * Write the contents of this datatype to the file at the position it is
 * currently at.
 *
 * @param file destination file
 * @throws IOException on any I/O error
 */
public void write(RandomAccessFile file) throws IOException
{
    //Write the various fields to file in order
    byte[] buffer;
    AbstractDataType object;
    Iterator<AbstractDataType> iterator = objectList.listIterator();
    while (iterator.hasNext())
    {
        object = iterator.next();
        buffer = object.writeByteArray();
        file.write(buffer);
    }
}
 
开发者ID:GlennioTech,项目名称:MetadataEditor,代码行数:21,代码来源:AbstractLyrics3v2FieldFrameBody.java


注:本文中的java.io.RandomAccessFile.write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。