List of usage examples for java.io RandomAccessFile seek
public void seek(long pos) throws IOException
From source file:Main.java
public static void main(String[] args) { try {/*from w ww . j av a 2 s . co m*/ RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF("Hello World from java2s.com"); // set the file pointer at 0 position raf.seek(0); // read byte System.out.println(raf.readByte()); // set the file pointer at 0 position raf.seek(0); // write 0 at the start raf.write(0); // read byte System.out.println(raf.readByte()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {/*from w w w . java 2 s . c o m*/ RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF("Hello World from java2s.com"); // set the file pointer at 0 position raf.seek(0); // read boolean System.out.println(raf.readBoolean()); // set the file pointer at 0 position raf.seek(0); // write 0 at the start raf.write(0); // read boolean System.out.println(raf.readBoolean()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:MainClass.java
public static void main(String[] args) { try {/*ww w. j a v a2 s . c om*/ RandomAccessFile raf = new RandomAccessFile("c:\\temp\\RAFsample.txt", "rw"); raf.writeInt(10); raf.writeInt(43); raf.writeInt(88); raf.writeInt(455); // change the 3rd integer from 88 to 99 raf.seek((3 - 1) * 4); raf.writeInt(99); raf.seek(0); // go to the first integer int i = raf.readInt(); while (i != -1) { System.out.println(i); i = raf.readInt(); } raf.close(); } catch (IOException e) { } }
From source file:org.commoncrawl.service.queryserver.master.S3Helper.java
public static void main(String[] args) { File arcFile = new File(args[0]); long offset = Long.parseLong(args[1]); long contentSize = Long.parseLong(args[2]); try {/* ww w . j a v a 2 s. c o m*/ RandomAccessFile fileHandle = new RandomAccessFile(arcFile, "r"); fileHandle.seek(Math.max(offset - 10, 0)); byte data[] = new byte[(int) contentSize + 10]; fileHandle.readFully(data); ByteBuffer buffer = ByteBuffer.wrap(data); buffer.position(0); int position = scanForGZIPHeader(buffer.slice()); buffer.position(position); StreamingArcFileReader reader = new StreamingArcFileReader(false); reader.available(buffer); ArcFileItem nextItem = reader.getNextItem(); System.out.println(nextItem.getUri()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { RandomAccessFile raf = new RandomAccessFile("books.dat", "rw"); String books[] = new String[5]; books[0] = "A"; books[1] = "B"; books[2] = "C"; books[3] = "D"; books[4] = "E"; for (int i = 0; i < books.length; i++) { raf.writeUTF(books[i]);// w ww.j a va 2 s.com } raf.seek(raf.length()); raf.writeUTF("Servlet & JSP Programming"); raf.seek(0); while (raf.getFilePointer() < raf.length()) { System.out.println(raf.readUTF()); } }
From source file:Main.java
License:asdf
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);//w w w . j a v a2 s. c o m 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(); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFile = null; String line1 = "java2s.com\n"; String line2 = "asdf1234\n"; // read / write permissions randomAccessFile = new RandomAccessFile("yourFile.dat", "rw"); randomAccessFile.writeBytes(line1);/*from w w w. j a va 2 s . c o m*/ 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(); }
From source file:RandomFileTest.java
public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000); staff[1] = new Employee("Carl Cracker", 75000); staff[2] = new Employee("Tony Tester", 38000); try {/*from w w w. ja v a2 s. c om*/ DataOutputStream out = new DataOutputStream(new FileOutputStream("employee.dat")); for (int i = 0; i < staff.length; i++) staff[i].writeData(out); out.close(); } catch (IOException e) { System.out.print("Error: " + e); System.exit(1); } try { RandomAccessFile in = new RandomAccessFile("employee.dat", "r"); int count = (int) (in.length() / Employee.RECORD_SIZE); Employee[] newStaff = new Employee[count]; for (int i = count - 1; i >= 0; i--) { newStaff[i] = new Employee(); in.seek(i * Employee.RECORD_SIZE); newStaff[i].readData(in); } for (int i = 0; i < newStaff.length; i++) newStaff[i].print(); } catch (IOException e) { System.out.print("Error: " + e); System.exit(1); } }
From source file:RandomFileTest.java
public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15); staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15); try {/*from ww w . j a va 2 s .c om*/ // save all employee records to the file employee.dat DataOutputStream out = new DataOutputStream(new FileOutputStream("employee.dat")); for (Employee e : staff) e.writeData(out); out.close(); // retrieve all records into a new array RandomAccessFile in = new RandomAccessFile("employee.dat", "r"); // compute the array size int n = (int) (in.length() / Employee.RECORD_SIZE); Employee[] newStaff = new Employee[n]; // read employees in reverse order for (int i = n - 1; i >= 0; i--) { newStaff[i] = new Employee(); in.seek(i * Employee.RECORD_SIZE); newStaff[i].readData(in); } in.close(); // print the newly read employee records for (Employee e : newStaff) System.out.println(e); } catch (IOException e) { e.printStackTrace(); } }
From source file:CreateEmployeeFile.java
public static void main(String[] args) throws Exception { String[] fnames = { "A", "B", "C" }; String[] lnames = { "a", "b", "c" }; String[] addresses = { "Box 100", "55 Street", "6 Lane" }; byte[] ages = { 46, 59, 32 }; double[] salaries = { 5.0, 6.0, 3.0 }; RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw"); EmployeeRecord er = new EmployeeRecord(); for (int i = 0; i < fnames.length; i++) { er.setFirstName(fnames[i]);/*w w w . j a v a2 s . com*/ er.setLastName(lnames[i]); er.setAddress(addresses[i]); er.setAge(ages[i]); er.setSalary(salaries[i]); er.write(raf); } raf = new RandomAccessFile("employee.dat", "rw"); er = new EmployeeRecord(); int numRecords = (int) raf.length() / er.size(); for (int i = 0; i < numRecords; i++) { er.read(raf); System.out.print(er.getFirstName() + " "); System.out.print(er.getLastName() + " "); System.out.print(er.getAddress() + " "); System.out.print(er.getAge() + " "); System.out.println(er.getSalary()); } raf.seek(0); for (int i = 0; i < numRecords; i++) { er.read(raf); if (er.getAge() >= 55) { er.setSalary(0.0); raf.seek(raf.getFilePointer() - er.size()); er.write(raf); raf.seek(raf.getFilePointer() - er.size()); er.read(raf); } System.out.print(er.getFirstName() + " "); System.out.print(er.getLastName() + " "); System.out.print(er.getAddress() + " "); System.out.print(er.getAge() + " "); System.out.println(er.getSalary()); } }