本文整理汇总了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);
}
示例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);
}
示例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;
}
示例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();
}
}
示例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;
}
}
}
}
示例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
}
}
示例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();
}
示例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();
}
示例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 > to), this means we simply delete the portion of the file beginning at "to" and up to but excluding "from".
* <p> If (from < 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 < 0) || (to < 0) || (from >= 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);
}
示例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();
}
}
示例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();
}
}
示例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);
}
}
示例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);
}
}
示例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();
}
示例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);
}
}