Java/File Input Output/New IO
Содержание
Copy a file using NIO
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: map(FileChannel.MapMode mode,long position,long size)
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String args[]) {
FileInputStream fileInputStream;
FileChannel fileChannel;
long fileSize;
MappedByteBuffer mBuf;
try {
fileInputStream = new FileInputStream("test.txt");
fileChannel = fileInputStream.getChannel();
fileSize = fileChannel.size();
mBuf = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
for (int i = 0; i < fileSize; i++)
System.out.print((char) mBuf.get());
fileChannel.close();
fileInputStream.close();
} catch (IOException exc) {
System.out.println(exc);
System.exit(1);
}
}
}
Use a mapped file to read a text file
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String args[]) {
FileInputStream fileInputStream;
FileChannel fileChannel;
long fileSize;
MappedByteBuffer mBuf;
try {
fileInputStream = new FileInputStream("test.txt");
fileChannel = fileInputStream.getChannel();
fileSize = fileChannel.size();
mBuf = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
for (int i = 0; i < fileSize; i++)
System.out.print((char) mBuf.get());
fileChannel.close();
fileInputStream.close();
} catch (IOException exc) {
System.out.println(exc);
System.exit(1);
}
}
}
Use New Java IO to write string into a file
/*
* Output:
File stream created successfully.
New buffer: position = 0 Limit = 1024 capacity = 1024
Buffer after loading: position = 30 Limit = 1024 capacity = 1024
Buffer after flip: position = 0 Limit = 30 capacity = 1024
Buffer contents written to file.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String[] args) {
String phrase = new String("www.jexp.ru\n");
File aFile = new File("test.txt");
FileOutputStream outputFile = null;
try {
outputFile = new FileOutputStream(aFile, true);
System.out.println("File stream created successfully.");
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
}
FileChannel outChannel = outputFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
System.out.println("New buffer: position = " + buf.position()
+ "\tLimit = " + buf.limit() + "\tcapacity = "
+ buf.capacity());
// Load the data into the buffer
for (char ch : phrase.toCharArray()) {
buf.putChar(ch);
}
System.out.println("Buffer after loading: position = " + buf.position()
+ "\tLimit = " + buf.limit() + "\tcapacity = "
+ buf.capacity());
buf.flip();
System.out.println("Buffer after flip: position = " + buf.position()
+ "\tLimit = " + buf.limit() + "\tcapacity = "
+ buf.capacity());
try {
outChannel.write(buf);
outputFile.close();
System.out.println("Buffer contents written to file.");
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
}
Write A String As Bytes
/*
* Output:
*/
import java.io.File;
import java.io.FileNotFoundException;
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) {
String phrase = new String("www.jexp.ru API \n");
File aFile = new File("test.txt");
FileOutputStream file = null;
try {
file = new FileOutputStream(aFile, true);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
}
FileChannel outChannel = file.getChannel();
ByteBuffer buf = ByteBuffer.allocate(phrase.length());
byte[] bytes = phrase.getBytes();
buf.put(bytes);
buf.flip();
try {
outChannel.write(buf);
file.close();
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
}
Write to a file using the new I/O
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);
}
}
}
Write to a mapped file
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);
}
}
}