Java by API/java.io/RandomAccessFile
Содержание
- 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)
<source lang="java">
/*
* 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(); } }
}
</source>
RandomAccessFile: close()
<source lang="java">
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(); }
}
</source>
RandomAccessFile: getChannel()
<source lang="java">
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); } }
}
</source>
RandomAccessFile: getFilePointer()
<source lang="java">
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()); }
}
</source>
RandomAccessFile: length()
<source lang="java">
/*
* 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(); } }
}
</source>
RandomAccessFile: readBoolean()
<source lang="java">
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(); }
}
</source>
RandomAccessFile: readByte()
<source lang="java">
/*
* 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(); } }
}
</source>
RandomAccessFile: read(byte[] b)
<source lang="java">
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(); }
}
</source>
RandomAccessFile: readChar()
<source lang="java">
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(); }
}
</source>
RandomAccessFile: readDouble()
<source lang="java">
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(); }
}
</source>
RandomAccessFile: readInt()
<source lang="java">
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) { } }
}
</source>
RandomAccessFile: readLine()
<source lang="java">
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(); }
}
</source>
RandomAccessFile: seek(long pos)
<source lang="java">
/*
* 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(); } }
}
</source>
RandomAccessFile: writeBoolean(boolean v)
<source lang="java">
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(); }
}
</source>
RandomAccessFile: write(byte[] b)
<source lang="java">
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(); }
}
</source>
RandomAccessFile: writeBytes(String s)
<source lang="java">
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(); }
}
</source>
RandomAccessFile: writeChar(int v)
<source lang="java">
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(); }
}
</source>
RandomAccessFile: writeChars(String s)
<source lang="java">
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(); }
}
</source>
RandomAccessFile: writeDouble(double v)
<source lang="java">
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(); }
}
</source>
RandomAccessFile: writeInt(int v)
<source lang="java">
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) { } }
}
</source>
RandomAccessFile: writeUTF(String str)
<source lang="java">
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(); }
}
</source>