1번:RandomAccessFile 이용해서 파일쓰기(Hello World!!추가하기)
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;
public class RandomAccess {
public static void main(String[] args) {
String filename = "C:\\Documents and Settings\\mina\\test\\Random\\src\\abcd.txt";
FileChannel outputChannel = null;
try {
String s = "Hello World!!";
byte[] by = s.getBytes();
ByteBuffer buf = ByteBuffer.allocate(by.length);
buf.put(by);
buf.clear();
RandomAccessFile f = new RandomAccessFile(filename,"rw");
outputChannel = f.getChannel();
outputChannel.position(f.length());
outputChannel.write(buf);
outputChannel.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
No comments:
Post a Comment