List of usage examples for java.io RandomAccessFile writeUTF
public final void writeUTF(String str) throws IOException
From source file:Main.java
public static void main(String[] args) { try {/*from w ww . j ava2s . 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); // print the byte System.out.println(raf.readUnsignedByte()); // set the file pointer at 7 position raf.seek(7); System.out.println(raf.readUnsignedByte()); 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("java2s.com Hello World"); // set the file pointer at 0 position raf.seek(0); // read the first byte and print it System.out.println(raf.read()); // set the file pointer at 4rth position raf.seek(4); // read the first byte and print it System.out.println(raf.read()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {/* w w w. j av a2 s. c o m*/ byte[] b1 = { 1, 2, 3 }; byte[] b2 = { 1, 2, 3, 4, 5, 6, 7, 8 }; 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 the first 8 bytes and print the number of bytes read System.out.println(raf.read(b1)); // set the file pointer at 0 position raf.seek(0); // read the first 8 bytes and print the number of bytes read System.out.println(raf.read(b2)); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {//from w ww . j av a 2 s . c om RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF("Hello World"); // set the file pointer at 0 position raf.seek(0); // print the line System.out.println(raf.readLine()); // set the file pointer at 0 position raf.seek(0); raf.writeUTF("This is an example \n Hello World"); raf.seek(0); // print the line System.out.println(raf.readLine()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {//from ww w . ja v a 2s .c o m byte[] b1 = { 1, 2, 3 }; byte[] b2 = { 1, 2, 3, 4, 5, 6, 7, 8 }; 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 2 bytes, starting from 1 System.out.println(raf.read(b1, 1, 2)); // set the file pointer at 0 position raf.seek(0); // read 3 bytes, starting from 4rth System.out.println(raf.read(b2, 4, 3)); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {/*from w ww .j ava 2 s. c om*/ 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 ww w . ja v a 2s .c o m*/ 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) { try {/*from w w w. j a v a 2s . c om*/ 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:Main.java
public static void main(String[] args) { try {/*from www . j a va 2 s .c om*/ 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:Main.java
public static void main(String[] args) { try {//from w w w . j ava 2 s .c o m 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[10]; // read the file raf.readFully(arr, 3, 7); // create a new string based on arr String s2 = new String(arr); // print it System.out.println(s2); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }