2008年9月11日星期四

RandomAccessFile notes

/**
* Write contents to txt file using "RandomAccessFile "
*
*/

String filepath = "E:\\wwwroot\\tmp\\testRandomAccreeFile.txt";
RandomAccessFile f = new RandomAccessFile(filepath,"rw");
f.seek(7 * 7 - 2);
String toWrite = "Helloo";

// not work, it will output one white space after one char.
//f.writeChars( toWrite );

// not work, it will output one white space and a new line
// before toWrite after writing
// f.writeUTF( toWrite );

// This is the right solution
f.write(toWrite.getBytes());

f.close();

没有评论: