Java by API/java.nio.channels/FileChannel
Содержание
- 1 FileChannel: close()
- 2 FileChannel: lock(long position, long size, boolean shared)
- 3 FileChannel: map(MapMode mode, long position, long size)
- 4 FileChannel.MapMode.READ_ONLY
- 5 FileChannel.MapMode.READ_WRITE
- 6 FileChannel: position()
- 7 FileChannel: position(long newPosition)
- 8 FileChannel: read(ByteBuffer dst)
- 9 FileChannel: size()
- 10 FileChannel: transferFrom(ReadableByteChannel src, long position, long count)
- 11 FileChannel: transferTo(long position, long count, WritableByteChannel target)
- 12 FileChannel: tryLock()
- 13 FileChannel: write(ByteBuffer buffer)
- 14 FileChannel: write(MappedByteBuffer buffer)
FileChannel: close()
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String args[]) {
RandomAccessFile randomAccessFile;
FileChannel fileChannel;
ByteBuffer byteBuffer;
try {
randomAccessFile = new RandomAccessFile("test.txt", "rw");
fileChannel = randomAccessFile.getChannel();
byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 26);
for (int i = 0; i < 10; i++)
byteBuffer.put((byte) ("A" + i));
fileChannel.close();
randomAccessFile.close();
} catch (IOException exc) {
System.out.println(exc);
System.exit(1);
}
}
}
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class Main {
public static void main(String[] argv) throws Exception {
String filename = "test.dat";
RandomAccessFile raf1 = new RandomAccessFile(filename, "rw");
FileChannel fc1 = raf1.getChannel();
RandomAccessFile raf2 = new RandomAccessFile(filename, "rw");
FileChannel fc2 = raf2.getChannel();
System.out.println("Grabbing first lock");
FileLock lock1 = fc1.lock(0L, Integer.MAX_VALUE, false);
System.out.println("Grabbing second lock");
FileLock lock2 = fc2.lock(5, 10, false);
System.out.println("Exiting");
}
}
FileChannel: map(MapMode mode, long position, long size)
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) throws IOException {
FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
ib.put(0);
for (int i = 1; i < 10; i++)
ib.put(ib.get(i - 1));
fc.close();
}
}
FileChannel.MapMode.READ_ONLY
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String args[]) {
FileInputStream fIn;
FileOutputStream fOut;
FileChannel fIChan, fOChan;
long fSize;
MappedByteBuffer mBuf;
try {
fIn = new FileInputStream(args[0]);
fOut = new FileOutputStream(args[1]);
fIChan = fIn.getChannel();
fOChan = fOut.getChannel();
fSize = fIChan.size();
mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
fOChan.write(mBuf); // this copies the file
fIChan.close();
fIn.close();
fOChan.close();
fOut.close();
} catch (IOException exc) {
System.out.println(exc);
System.exit(1);
} catch (ArrayIndexOutOfBoundsException exc) {
System.out.println("Usage: Copy from to");
System.exit(1);
}
}
}
FileChannel.MapMode.READ_WRITE
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) throws IOException {
FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
ib.put(0);
for (int i = 1; i < 10; i++)
ib.put(ib.get(i - 1));
fc.close();
}
}
FileChannel: position()
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] argv) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("test.dat", "r");
randomAccessFile.seek(1000);
FileChannel fileChannel = randomAccessFile.getChannel();
// This will print "1000"
System.out.println("file pos: " + fileChannel.position());
randomAccessFile.seek(500);
// This will print "500"
System.out.println("file pos: " + fileChannel.position());
fileChannel.position(200);
// This will print "200"
System.out.println("file pos: " + randomAccessFile.getFilePointer());
}
}
FileChannel: position(long newPosition)
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
private static final int BSIZE = 1024;
public static void main(String[] args) throws Exception {
FileChannel fc = new RandomAccessFile("data.txt", "rw").getChannel();
fc.position(fc.size());
fc.write(ByteBuffer.wrap("Some more".getBytes()));
fc.close();
}
}
FileChannel: read(ByteBuffer dst)
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
private static final int BSIZE = 1024;
public static void main(String[] args) throws Exception {
FileChannel fc = new FileInputStream("data.txt").getChannel();
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
fc.read(buff);
buff.flip();
while (buff.hasRemaining())
System.out.print((char) buff.get());
}
}
FileChannel: size()
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String args[]) {
FileInputStream fIn;
FileChannel fChan;
long fSize;
ByteBuffer mBuf;
try {
fIn = new FileInputStream("test.txt");
fChan = fIn.getChannel();
fSize = fChan.size();
mBuf = ByteBuffer.allocate((int) fSize);
fChan.read(mBuf);
mBuf.rewind();
for (int i = 0; i < fSize; i++)
System.out.print((char) mBuf.get());
fChan.close();
fIn.close();
} catch (IOException exc) {
System.out.println(exc);
}
}
}
FileChannel: transferFrom(ReadableByteChannel src, long position, long count)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) {
FileChannel in = null;
FileChannel out = null;
if (args.length < 2) {
System.out.println("Usage: java Copy <from> <to>");
System.exit(1);
}
try {
in = new FileInputStream(args[0]).getChannel();
out = new FileOutputStream(args[1]).getChannel();
out.transferFrom(in, 0L, (int) in.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}
FileChannel: transferTo(long position, long count, WritableByteChannel target)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String[] args) throws Exception{
String fromFileName = args[0];
String toFileName = args[1];
FileChannel in = new FileInputStream(fromFileName).getChannel();
FileChannel out = new FileOutputStream(toFileName).getChannel();
in.transferTo(0, (int) in.size(), out);
in.close();
out.close();
}
}
FileChannel: tryLock()
import java.io.FileOutputStream;
import java.nio.channels.FileLock;
public class Main {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("file.txt");
FileLock fl = fos.getChannel().tryLock();
if (fl != null) {
System.out.println("Locked File");
Thread.sleep(100);
fl.release();
System.out.println("Released Lock");
}
fos.close();
}
}
FileChannel: write(ByteBuffer buffer)
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String args[]) {
FileOutputStream fileOutputStream;
FileChannel fileChannel;
ByteBuffer byteBuffer;
try {
fileOutputStream = new FileOutputStream("test.txt");
fileChannel = fileOutputStream.getChannel();
byteBuffer = ByteBuffer.allocateDirect(26);
for (int i = 0; i < 26; i++)
byteBuffer.put((byte) ("A" + i));
byteBuffer.rewind();
fileChannel.write(byteBuffer);
fileChannel.close();
fileOutputStream.close();
} catch (IOException exc) {
System.out.println(exc);
}
}
}
FileChannel: write(MappedByteBuffer buffer)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String args[]) {
FileInputStream fIn;
FileOutputStream fOut;
FileChannel fIChan, fOChan;
long fSize;
MappedByteBuffer mBuf;
try {
fIn = new FileInputStream(args[0]);
fOut = new FileOutputStream(args[1]);
fIChan = fIn.getChannel();
fOChan = fOut.getChannel();
fSize = fIChan.size();
mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
fOChan.write(mBuf); // this copies the file
fIChan.close();
fIn.close();
fOChan.close();
fOut.close();
} catch (IOException exc) {
System.out.println(exc);
System.exit(1);
} catch (ArrayIndexOutOfBoundsException exc) {
System.out.println("Usage: Copy from to");
System.exit(1);
}
}
}