Java by API/java.io/RandomAccessFile — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
- 1 new RandomAccessFile(String fileName, String mode)
- 2 RandomAccessFile: close()
- 3 RandomAccessFile: getChannel()
- 4 RandomAccessFile: getFilePointer()
- 5 RandomAccessFile: length()
- 6 RandomAccessFile: readBoolean()
- 7 RandomAccessFile: readByte()
- 8 RandomAccessFile: read(byte[] b)
- 9 RandomAccessFile: readChar()
- 10 RandomAccessFile: readDouble()
- 11 RandomAccessFile: readInt()
- 12 RandomAccessFile: readLine()
- 13 RandomAccessFile: seek(long pos)
- 14 RandomAccessFile: writeBoolean(boolean v)
- 15 RandomAccessFile: write(byte[] b)
- 16 RandomAccessFile: writeBytes(String s)
- 17 RandomAccessFile: writeChar(int v)
- 18 RandomAccessFile: writeChars(String s)
- 19 RandomAccessFile: writeDouble(double v)
- 20 RandomAccessFile: writeInt(int v)
- 21 RandomAccessFile: writeUTF(String str)
new RandomAccessFile(String fileName, String mode)
/*
* Output:
*
*/
import java.io.RandomAccessFile;
public class MainClass {
public static void main(String args[]) {
try {
RandomAccessFile raf = new RandomAccessFile(args[0], "r");
long position = raf.length();
while (position > 0) {
position -= 1;
raf.seek(position);
byte b = raf.readByte();
System.out.print((char) b);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
RandomAccessFile: close()
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) throws Exception{
ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream("filename"));
ZipEntry zipentry = zipinputstream.getNextEntry();
while (zipentry != null) {
String entryName = zipentry.getName();
File newFile = new File(entryName);
String directory = newFile.getParent();
if (directory == null) {
if (newFile.isDirectory())
break;
}
RandomAccessFile rf = new RandomAccessFile(entryName, "r");
String line;
if ((line = rf.readLine()) != null) {
System.out.println(line);
}
rf.close();
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
}
zipinputstream.close();
}
}
RandomAccessFile: getChannel()
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);
}
}
}
RandomAccessFile: getFilePointer()
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());
}
}
RandomAccessFile: length()
/*
* Output:
*
*/
import java.io.RandomAccessFile;
public class MainClass {
public static void main(String args[]) {
try {
RandomAccessFile raf = new RandomAccessFile(args[0], "r");
long position = raf.length();
while (position > 0) {
position -= 1;
raf.seek(position);
byte b = raf.readByte();
System.out.print((char) b);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
RandomAccessFile: readBoolean()
import java.io.IOException;
import java.io.RandomAccessFile;
public class Main {
public static void main(String args[]) throws IOException {
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeChar("j");
file.writeDouble(1234.56);
file.seek(1);
System.out.println(file.readInt());
System.out.println(file.readChar());
System.out.println(file.readDouble());
file.seek(0);
System.out.println(file.readBoolean());
file.close();
}
}
RandomAccessFile: readByte()
/*
* Output:
*
*/
import java.io.RandomAccessFile;
public class MainClass {
public static void main(String args[]) {
try {
RandomAccessFile raf = new RandomAccessFile(args[0], "r");
long position = raf.length();
while (position > 0) {
position -= 1;
raf.seek(position);
byte b = raf.readByte();
System.out.print((char) b);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
RandomAccessFile: read(byte[] b)
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = null;
String line1 = "line\n";
String line2 = "asdf1234\n";
// read / write permissions
randomAccessFile = new RandomAccessFile("yourFile.dat", "rw");
randomAccessFile.writeBytes(line1);
randomAccessFile.writeBytes(line2);
// Place the file pointer at the end of the first line
randomAccessFile.seek(line1.length());
byte[] buffer = new byte[line2.length()];
randomAccessFile.read(buffer);
System.out.println(new String(buffer));
randomAccessFile.close();
}
}
RandomAccessFile: readChar()
import java.io.File;
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] argv) throws Exception {
File f = new File("filename");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
// Read a character
char ch = raf.readChar();
// Seek to end of file
raf.seek(f.length());
// Append to the end
raf.writeChars("aString");
raf.close();
}
}
RandomAccessFile: readDouble()
import java.io.IOException;
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) throws IOException {
RandomAccessFile rf = new RandomAccessFile("test.dat", "rw");
for (int i = 0; i < 10; i++)
rf.writeDouble(i * 1.414);
rf.close();
rf = new RandomAccessFile("test.dat", "rw");
rf.seek(5 * 8);
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile("test.dat", "r");
for (int i = 0; i < 10; i++)
System.out.println("Value " + i + ": " + rf.readDouble());
rf.close();
}
}
RandomAccessFile: readInt()
import java.io.IOException;
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) {
try {
RandomAccessFile raf = new RandomAccessFile("c:\\temp\\RAFsample.txt", "rw");
raf.writeInt(10);
raf.writeInt(43);
raf.writeInt(88);
raf.writeInt(455);
raf.seek((3 - 1) * 4);
raf.writeInt(99);
raf.seek(0);
int i = raf.readInt();
while (i != -1) {
System.out.println(i);
i = raf.readInt();
}
raf.close();
} catch (IOException e) {
}
}
}
RandomAccessFile: readLine()
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) throws Exception{
ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream("filename"));
ZipEntry zipentry = zipinputstream.getNextEntry();
while (zipentry != null) {
String entryName = zipentry.getName();
File newFile = new File(entryName);
String directory = newFile.getParent();
if (directory == null) {
if (newFile.isDirectory())
break;
}
RandomAccessFile rf = new RandomAccessFile(entryName, "r");
String line;
if ((line = rf.readLine()) != null) {
System.out.println(line);
}
rf.close();
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
}
zipinputstream.close();
}
}
RandomAccessFile: seek(long pos)
/*
* Output:
*
*/
import java.io.RandomAccessFile;
public class MainClass {
public static void main(String args[]) {
try {
RandomAccessFile raf = new RandomAccessFile(args[0], "r");
long position = raf.length();
while (position > 0) {
position -= 1;
raf.seek(position);
byte b = raf.readByte();
System.out.print((char) b);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
RandomAccessFile: writeBoolean(boolean v)
import java.io.IOException;
import java.io.RandomAccessFile;
public class Main {
public static void main(String args[]) throws IOException {
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeChar("j");
file.writeDouble(1234.56);
file.seek(1);
System.out.println(file.readInt());
System.out.println(file.readChar());
System.out.println(file.readDouble());
file.seek(0);
System.out.println(file.readBoolean());
file.close();
}
}
RandomAccessFile: write(byte[] b)
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] argv) throws Exception {
RandomAccessFile file = new RandomAccessFile("scores.html", "rw");
for (int i = 1; i <= 6; i++) {
System.out.println(file.readLine());
}
long current = file.getFilePointer();
file.seek(current + 6);
file.write("34".getBytes());
for (int i = 1; i <= 5; i++) {
System.out.println(file.readLine());
}
current = file.getFilePointer();
file.seek(current + 6);
file.write("27".getBytes());
file.close();
}
}
RandomAccessFile: writeBytes(String s)
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = null;
String line1 = "line\n";
String line2 = "asdf1234\n";
// read / write permissions
randomAccessFile = new RandomAccessFile("yourFile.dat", "rw");
randomAccessFile.writeBytes(line1);
randomAccessFile.writeBytes(line2);
// Place the file pointer at the end of the first line
randomAccessFile.seek(line1.length());
byte[] buffer = new byte[line2.length()];
randomAccessFile.read(buffer);
System.out.println(new String(buffer));
randomAccessFile.close();
}
}
RandomAccessFile: writeChar(int v)
import java.io.IOException;
import java.io.RandomAccessFile;
public class Main {
public static void main(String args[]) throws IOException {
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeChar("j");
file.writeDouble(1234.56);
file.seek(1);
System.out.println(file.readInt());
System.out.println(file.readChar());
System.out.println(file.readDouble());
file.seek(0);
System.out.println(file.readBoolean());
file.close();
}
}
RandomAccessFile: writeChars(String s)
import java.io.File;
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] argv) throws Exception {
File f = new File("filename");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
// Read a character
char ch = raf.readChar();
// Seek to end of file
raf.seek(f.length());
// Append to the end
raf.writeChars("aString");
raf.close();
}
}
RandomAccessFile: writeDouble(double v)
import java.io.IOException;
import java.io.RandomAccessFile;
public class Main {
public static void main(String args[]) throws IOException {
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeChar("j");
file.writeDouble(1234.56);
file.seek(1);
System.out.println(file.readInt());
System.out.println(file.readChar());
System.out.println(file.readDouble());
file.seek(0);
System.out.println(file.readBoolean());
file.close();
}
}
RandomAccessFile: writeInt(int v)
import java.io.IOException;
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) {
try {
RandomAccessFile raf = new RandomAccessFile("c:\\temp\\RAFsample.txt", "rw");
raf.writeInt(10);
raf.writeInt(43);
raf.writeInt(88);
raf.writeInt(455);
raf.seek((3 - 1) * 4);
raf.writeInt(99);
raf.seek(0);
int i = raf.readInt();
while (i != -1) {
System.out.println(i);
i = raf.readInt();
}
raf.close();
} catch (IOException e) {
}
}
}
RandomAccessFile: writeUTF(String str)
import java.io.IOException;
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw");
raf.writeUTF("J");
raf.writeUTF("S");
raf.writeDouble(4.0);
raf.seek(0L);
String fname = raf.readUTF();
String lname = raf.readUTF();
double salary = raf.readDouble();
System.out.println("First name = " + fname);
System.out.println("Last name = " + lname);
System.out.println("Salary = " + salary);
raf.close();
}
}