Java Learning 22nd Day--io Comprehensive exercises Collection 3

Source: Internet
Author: User

the Long data and its native array are converted to each other/* * The Long data and its native array are converted to each other */public class Demo {
public static void Main (string[] args) {Long n = long.max_value; SYSTEM.OUT.PRINTLN (n);
byte[] buf = Long2bytes (n); Long res = Bytes2long (BUF); System.out.println (RES);
for (byte b:buf) {//System.out.println (b);//127//}
}
Byte[]--> long: High in the former public static long Bytes2long (byte[] buf) {Long l0 = (buf[0] & 0xFFL) << long L1 = (Buf[1] & 0xFFL) << 48; Long L2 = (buf[2] & 0xFFL) << 40; Long L3 = (Buf[3] & 0xFFL) << 32; Long L4 = (Buf[4] & 0xFFL) << 24; Long L5 = (Buf[5] & 0xFFL) << 16; Long L6 = (buf[6] & 0xFFL) << 8; Long L7 = (buf[7] & 0xFFL) << 0;
return l0 + L1 + L2 + L3 + L4 + l5 + L6 + L7; }
Long--> byte[]: High level in front public static byte[] Long2bytes (long N) {byte[] buf = new byte[8]; buf[0] = (byte) (n >> 56 ); BUF[1] = (byte) (n >> 48); BUF[2] = (byte) (n >> 40); BUF[3] = (byte) (n >> 32); BUF[4] = (byte) (n >> 24); BUF[5] = (byte) (n >> 16); BUF[6] = (byte) (n >> 8); BUF[7] = (byte) (n >> 0);//127
return buf; }
}
Random Access file: RandomaccessfileInstances of this class support the behavior of reading and writing random access files to random access files similar to a large byte array stored in the file system, there is a cursor or index pointing to the suppressed array. called a file pointer; The input operation reads bytes from the file pointer and moves the file pointer forward as the byte is read. Construct method: Randomaccessfile (file file, string mode) Randomaccessfile (string name, string mode) Read-write method: can read and write a variety of common types of data special methods: void SE EK (Long POS)
random access to files (simple understanding only)
ImportJava.io.RandomAccessFile; * * Randomaccessfile (File file,string mode) * Randomaccessfile (String name,string mode) * * Public classRandomaccessfiledemo { Public Static voidMain (string[] args) throwsException {Randomaccessfile RAF = NewRandomaccessfile ("Raf.txt", "RW");       RAF. Writeint (200); Move the file pointer to the file header RAF. Seek (0); inti = RAF. ReadInt (); System. out. println (i);    RAF. Close (); } }
ImportJava.io.RandomAccessFile;  * * Random Access file parent class is Object.  * Can read and write two operations.  * Specify the mode of the file operation in the constructor: * General use: "RW", which means that both can read and write. * Randomaccessfile (File file,string mode) * Randomaccessfile (String name,string mode) * * Public classRandomAccessFileDemo2 { Public Static voidMain (string[] args) throwsException {//create object Randomaccessfile RAF = NewRandomaccessfile ("Raf.txt", "RW");       Write Data RAF. Write (97); System. out. println (RAF. Getfilepointer ());       RAF. Writeint (200); System. out. println (RAF. Getfilepointer ());       RAF. writeUTF ("Hello"); System. out. println (RAF. Getfilepointer ());       RAF. writeUTF ("World"); System. out. println (RAF. Getfilepointer ()); Navigate to the string offset//Raf.seek (12); //Read a string that reads the method to match the true data type that will be read//String s = Raf.readutf ();             System.out.println (s); Read the specified string multiple times for( inti = 0; I <5;          i + +) {RAF. Seek (12); System. out. println (RAF. readUTF ());    //Release Resource RAF. Close (); } }


there is a string in the random Access file, how do I read the string multiple times?

Properties classis not part of the IO package, but because some operations and IO flow related, so put it here in Io, as a subclass of MAP use: Mainly add elements and traversal-specific methods: setProperty//Set configuration content GetProperty//Get configuration content Stringpropertynames

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.