List of usage examples for java.io RandomAccessFile length
public native long length() throws IOException;
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 {/*ww w . ja va2s . c o m*/ 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:SenBench.java
public static void main(String[] args) { try {// ww w . j av a 2 s. c o m if (args.length == 0) { System.out.println("usage: java SenBench file [file ..]"); System.exit(2); } StringTagger tagger = StringTagger.getInstance(Locale.JAPANESE); long processed = 0; long nbytes = 0; long nchars = 0; long start = System.currentTimeMillis(); for (int a = 0; a < args.length; a++) { String text = ""; try { RandomAccessFile raf = new RandomAccessFile(args[a], "r"); byte[] buf = new byte[(int) raf.length()]; raf.readFully(buf); raf.close(); text = new String(buf, encoding); nbytes += buf.length; nchars += text.length(); } catch (IOException ioe) { log.error(ioe); continue; } long s_start = System.currentTimeMillis(); for (int c = 0; c < repeat; c++) doWork(tagger, text); long s_end = System.currentTimeMillis(); processed += (s_end - s_start); } long end = System.currentTimeMillis(); System.out.println("number of files: " + args.length); System.out.println("number of repeat: " + repeat); System.out.println("number of bytes: " + nbytes); System.out.println("number of chars: " + nchars); System.out.println("total time elapsed: " + (end - start) + " msec."); System.out.println("analysis time: " + (processed) + " msec."); } catch (Exception e) { e.printStackTrace(System.err); System.exit(1); } }
From source file:MeCabBench.java
public static void main(String[] args) { try {//from w w w . ja va 2 s .co m if (args.length == 0) { System.out.println("usage: java MeCabBench file [file ..]"); System.exit(2); } Tagger tagger = new Tagger(new String[] { "java", "-d", dicPath }); long processed = 0; long nbytes = 0; long nchars = 0; long start = System.currentTimeMillis(); for (int a = 0; a < args.length; a++) { String text = ""; try { RandomAccessFile raf = new RandomAccessFile(args[a], "r"); byte[] buf = new byte[(int) raf.length()]; raf.readFully(buf); raf.close(); text = new String(buf, encoding); nbytes += buf.length; nchars += text.length(); } catch (IOException ioe) { log.error(ioe); continue; } long s_start = System.currentTimeMillis(); for (int c = 0; c < repeat; c++) doWork(tagger, text); long s_end = System.currentTimeMillis(); processed += (s_end - s_start); } long end = System.currentTimeMillis(); System.out.println("number of files: " + args.length); System.out.println("number of repeat: " + repeat); System.out.println("number of bytes: " + nbytes); System.out.println("number of chars: " + nchars); System.out.println("total time elapsed: " + (end - start) + " msec."); System.out.println("analysis time: " + (processed) + " msec."); } catch (Exception e) { e.printStackTrace(System.err); 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 w w w .j av a2s. c o m // 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:Main.java
public static void main(String[] args) { try {//from www . j av a 2 s .c om String s = "Hello world from java2s.com"; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF(s); // set the file pointer at 0 position raf.seek(0); // create an array equal to the length of raf byte[] arr = new byte[(int) raf.length()]; // read the file raf.readFully(arr); // create a new string based on arr String s2 = new String(arr); System.out.println(s2); raf.close(); } catch (IOException ex) { ex.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]);//from www .j a v a 2 s . c o m } 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
public static void main(String[] args) { try {//from ww w . java2 s.c o m RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeUTF("Hello World from java2s.com"); // set the file pointer at 0 position raf.seek(0); // print the string System.out.println(raf.readUTF()); // print current length System.out.println(raf.length()); // set the file length to 30 raf.setLength(30); System.out.println(raf.length()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {//from w w w . j ava 2 s .com RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF("java2s.com Hello World"); // set the file pointer at 0 position raf.seek(0); // read and print the contents of the file System.out.println(raf.readUTF()); // print the length of the file System.out.println(raf.length()); // write something more in the file raf.writeUTF("This is an example"); // print the length of the file System.out.println(raf.length()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Diff.java
public static void main(String args[]) { RandomAccessFile fh1 = null; RandomAccessFile fh2 = null;/*from w w w . j av a 2 s .c om*/ int bufsize; // size of smallest file long filesize1 = -1; long filesize2 = -1; byte buffer1[]; // the two file caches byte buffer2[]; // check what you get as command-line arguments if (args.length == 0 || args[0].equals("?")) { System.err.println("USAGE: java Diff <file1> <file2> | ?"); System.exit(0); } // open file ONE for reading try { fh1 = new RandomAccessFile(args[0], "r"); filesize1 = fh1.length(); } catch (IOException ioErr) { System.err.println("Could not find " + args[0]); System.err.println(ioErr); System.exit(100); } // open file TWO for reading try { fh2 = new RandomAccessFile(args[1], "r"); filesize2 = fh2.length(); } catch (IOException ioErr) { System.err.println("Could not find " + args[1]); System.err.println(ioErr); System.exit(100); } if (filesize1 != filesize2) { System.out.println("Files differ in size !"); System.out.println("'" + args[0] + "' is " + filesize1 + " bytes"); System.out.println("'" + args[1] + "' is " + filesize2 + " bytes"); } // allocate two buffers large enough to hold entire files bufsize = (int) Math.min(filesize1, filesize2); buffer1 = new byte[bufsize]; buffer2 = new byte[bufsize]; try { fh1.readFully(buffer1, 0, bufsize); fh2.readFully(buffer2, 0, bufsize); for (int i = 0; i < bufsize; i++) { if (buffer1[i] != buffer2[i]) { System.out.println("Files differ at offset " + i); break; } } } catch (IOException ioErr) { System.err.println("ERROR: An exception occurred while processing the files"); System.err.println(ioErr); } finally { try { fh1.close(); fh2.close(); } catch (IOException ignored) { } } }
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 ava2s . c o m*/ 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()); } }