Think in java (12) -io

xiaoxiao2021-03-05  52

A Directory List

//c12: dirlist.java

// Displays Directory Listing Using Regular Expressions, Regular Expressions by Dirfilter Class.

//Mplements the interface filenamefilter.

Public interface filenamefilter {

Public Boolean Accept (File Dir, String Name);

}

File.list () CAN "Call Back" Accept () to Datermine Which File Names Should Be Included in The List.

List () Return String []

Accept () Uses a regular expression Watcher Object to see ethers the regular expression regex matches the name of the file.

Boolean B = Pattern.matches (Regex, Sequense Chars) Equals

{

Pattern P = Pattern.Compile (regex);

Matcher m = p.matcher (sequense char);

Boolean b = m.matchers ();

}

The parameters of an anonymous internal class must be Final.

Input and Output

InputStream

|

| --- ByterrayinputStream Allows A Buffer in Memory To BE Used As An InputStream

|

| --- StringBufferinputStream Converts a string to inputstream

|

| --- FileInputStream Reading Information from a file

|

| --- PipedinputStream Products The Data That Being Written to The Associated PiredOutputstream

|

| --- SequenceInputStream Coverts Two or More InputStream Object Into a Single InputStream

|

| ___FilterInputStream Abstract Class

Outputstream

|

| --- ByteaRrayOutputstream Creates A Buffer in Memory Call The Data The you send the stres placed in this buffer

|

| --- FileOutputStream for sending information to the file

|

| --PIPEDOUTPUTSTREAM Any Information You Write to this Automatic or Ends Up As Input Fro The Associated PipedInputStream

|

| --Filteroutputstream Abstract Class That Is An Interface for Characters That Provide Useful Functionality To The Other Output

Reader and Writer

THERE ARE TIMES WHEN You Must Use Classes Form The 'Byte' Hierarchy In Combination with Classes in The 'Character' Hierarchy. To Accomplish this there is 'adapter' Class: InputStreamReader and

OutputStreamwriter.

// C12:

IostreamDemo.java

// reading input by line (Reading Character files)

BufferedReader BufferedReader = New BufferedReader (New FileReader (File File);

BufferedReader.readline ();

// Reading Standard Input (Reading from Byte Street To Character Stream)

BufferedReader BufferedReader = New BufferedReader (NEW InputStreamReader

BufferedReader.readline ();

// Input from Memory (Reading Character)

StringReader StringReader = New StringBuffer (String STR);

INT C;

While (c = stringreader.read ())! = - 1)

System.out.print (CHAR) C);

// Formated Menory Input (Reading Bytes)

DataInputStream DataInputStream = New DataInputStream (New ByteArrayInputStream (Str.getbytes ());

(char) DataInputStream.readbyte ();

// file Output

PrintWriter PrintWriter = New PRINTWRITER (New FileWriter (New FileWriter (File File));

PrintWriter.println (String);

PrintWriter.Close ();

// Storing and Recovering Data

// storing

DataOutputStream DataOutputStream = New DataOutputStream (New Bufferedputstream (New FileoutputStream (File File));

DataOutputStream.writeutf (String Str);

// UTF-8 Representation of a unicode Character. String Encoded in Java: ASCII IN A Single Byte; Non Ascii in Two or Three Byte.

// Recover

DataInputStream DataInputStream = New DataInputStream (New BufferedInputStream (New FileInputStream (File File));

DataInputStream.readutf ();

// reading / Writing Random Access File

Randomaccessfile randomaccessfile = new randomaccessfile (file file);

// reading randomaccessfile.readfloat (); etc.//writing randomaccessfile.writefloat (float v);

// seek randomaccessfile.seek;

File Reading & Writing Utilities

// static method forreading and write text files as a single string, and treating a file as arraylist

//c12:TextFile.java

Standard I / O

One Program's Standard Output Can Become The Standard Input for Another Program.

System.in Must Be Wrapped Before You Can Read from It.

// Change System.out to a printwriter

PrintWriter PrintWriter = New PrintWriter (System.out, True);

PrintWriter.println (String Str);

// Second Parameter "True" in Order To Enable Automatic Flushing.

Redirecting Standard I / O

System.setin (InputStream)

System.setOut (PrintStream)

System.seterr (PrintStream)

//c12:Redirecting.java

BufferedInputStream BufferedInputStream = New BufferedInputStream (New FileInputStream (File File);

System.setin (BufferedInputStream);

BufferedReader BufferDreader = New BufferedReader (NEW InputStreamReader

BufferedReader.readline ();

NEW I / O

Three of the classes have been modified so what chemoduce A FileChannel: FileInputStream, FileOutputstream, RandomaccessFile.

// FileInputStream, FileOutputstream, RandomaccessFile Get FileChannel

FileChannel FileChannel = New FileInputStream (File File) .getchannel ();

FileChannel.write (Bytebuffer [] SRC);

FileChannel FileChannel = New FileOutputStream (File File) .getChannel ();

Filechannel FileChannel = New RandomaccessFile (File, "RW"). GetChannel ();

For Read-Only Access (FileInputStream), You Must Explicitly Allocate A Bytebuffer, Using The Static Allocate () Method.

//c12:ChannelCopy.java

FileChannel.read (Bytebuffer Buffer);

Buffer.flip ();

// Flips this buffer. The limit is set to the current position and then the position is set to zero.out.write (buffer);

Buffer.clear ();

//c12:Transferto.java

Filechannel in = new fileinputstream (file file) .getchannel ();

FileChannel Out = New FileOutputStream (File File) .getchannel ();

In.Transferto (0, in .size (), out);

// OR

Out.TransferFrom (in, 0, in.size ());

Converting data

The Bytebuffer Contains Plain Bytes, Java.Nio.charset.Charset Provides Tools for Encoding Into Many Different Type of Character Sets

//c12:Buffertotext.java

String encoding = system.getProperty ("file.encoding");

Charset charset = charset.Forname (Encoding);

Bytebuffer bytebuffer = charset.encode (string);

Charbuffer charbuffer = charset.decode (bytebuffer);

Although A Bytebuffer Only Holds Bytes Methods To Produce Each of Different Types of Primitive Values ​​from the bytes it Contains.

//c12:Getdata.java

Bytebuffer bb = bytebuffer.allocate (BSIZE);

Bb.ascharbuffer (). PUT (String STR);

Char C;

While ((c = bb.getChar ())! = 0) {...}

View buffers

//c12:IntbufferDemo.java

// Here's a Example That Manipulates Int in a bytebuffer via a Intbuffer

Bytebuffer bb = bytebuffer.allocate (BSIZE);

INTBUFFER IB = bb.asintbuffer ();

Ib.put (new int in] {11, 42, 47, 99, 143, 811, 1016});

Ib.put (3,1811); // ib's position is 7

Ib.rewind (); // ib's position is 0

While (ib.hasremaining ()) {...}

Note That Sests Aress Are Available for Primitive Types by Talking Directly to a bytebuffer, as well.

//c12:ViewBuffers.java

// Interprets The Same Sequence of Bytes As Short, Int Float, Long and Double Producing Different View Buffers on The Same Bytebuffer

Bytebuffer bb = bytebuffer.wrap (new byte [] {0, 0, 0, 0, 0, 0, 0, 'a'}); // Byte Buffer

While (bb.haasremaining ())

System.out.println (bb.position () "->" bb.get ());

// char buffer

Charbuffer CB = (Bytebuffer) bb.rewind ()). Ascharbuffer ();

While (cb.haasremaining ())

System.out.println (Cb.Position () "->" CB.Get ());

......

// Floatbuffer, intBuffer, Longbuffer, Shortbuffer, Doublebuffer, etc.

Endians

Different machines may use different byte-ordering approaches to store data. "Big endian" places the most significant byte in the lowest memory address, and "Little endian" places the most significant byte in the highest memory address.

A ByteBuffer stores data in big endian form, and data sent over a network always uses big endian order. You can change the endian-ness of a ByteBuffer using order () with an argument of ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIAN.

//c12:ENDIANS.JAVA

Bytebuffer bb = bytebuffer.wrap (New byte [12]);

Bb.ascharbuffer (). PUT ("abcdef");

bb.rewind ();

bb.order (byteorder.big_endian);

bb.rewind ();

bb.order (byteorder.little_endian);

Data manipulation with buffers

If you wish to write a byte array to a file, then you wrap the byte array using the ByteBuffer.wrap () method, open a channel on the FileOutputStream using the getChannel () method, and then write data into FileChannel form this ByteBuffer.

Note that ByteBuffer is the only way to move data in and out of channels, and that you can only create a standalone primitive-typed buffer, or get one from a ByteBuffer using an "as" method. That is, you can not convert a primitive -typed buffer to a bytebuffer. However, Since You are able to move primitive data intebuffy

A Buffer Consists of Data and Four Indexes of Access And Manipulate this Data EfficIntly: Mark, Position, Limit and Capacity.

Capacity () - RETURns the buffer's capacity

Clear () ----- Clears the Buffer, Sets The position to zero, and limit to capacity. You call this method to overwrite an existing buffer.

FLIP () ----- sets limit to position and position to zero. this method is used to prepare The Buffer a Read after Data Has Been Written Into IT.

Limit () ----- Returns the value of limited

LIMIT (INT LIM) --- sets the value of limited

Mark () ----- sets mark at position

Position ---- Returns the value of position

Position (int POS) --- sets the value of position

Remaining () - RETURns (Limit-position)

Hasremaining () --- Returns True if The are Any Elements Between Position and Limit

Example Uses a Very Simple Algorithm To Scramble and UnscRamble Characters in A Charbuffer

//c12:Usingbuffers.java

Private static void symmetricscRamble (charbuffer buffer) {

While (buffer.hasremaining ()) {

Buffer.mark ();

Char c1 = buffer.get ();

(buffer.hasremaining () {

Char C2 = buffer.get ();

Buffer.reset ();

Buffer.put (c2) .put (c1);

}

}

}

Memory-mapped files

.......................

//c12: markMappedfiles.java

// CREANG A VERY LARGE FILE Using Mapping.Static Int Length = 0x8ffff;

// 128MB

Mappedbytebuffer out = new randomaccessfile ("Test.dat", "RW"). GetChannel (). Map (filechannel.mapmode.read_write, 0, length);

For (INT i = 0; I

Out.put (Byte) 'x');

Note this.

Mappedbytebuffer is inherited from bytebuffer, That Has Put (), get (), ascharbuffer () Methods, ETC.

THIS WAY A VERY LARGE FILE (UP To 2GB) Can Easily Be Modified.

Performance

Althought The Performance of "Old" stream I / O Has Been Improved by Implementing It With Nio, Mapped File Asscess TENDS to Be DramAtical Faster.

//c12:Mappedio.java

FILE LOCKING

File Locking, Introducesd in JDK1.4, Allows You to Synchronize Access To a file as a shared resource.

//c12:filelocking.java

FileOutputStream Fos = New FileoutputStream (File File);

FiLeLock fl = fos.getChannel (). Trylock (); // locked file

IF (fl! = null) {

Thread.sleep (100);

Fl.release (); // Releaseed Lock

}

Fos.close ();

You get a filelock on the entire file by calling each

Trylock () or

lock () on a FileChannel (SocketChannel, DatagramChannel, and ServerSocketChannel do not need locking since they are inherently single-process entities;. you do not generally share a network socket between two processes).

Trylock () is nonn-blocking. it Tries To Grab The Lock, But if it cannot, IT Simply Returns from the method.

Lock () blocks until the lock is acquired, or the thread there.

A Lock is released using filelock.release (). It is also picture to lock a part of the file by using

Trylock (Long Position, Long Size, Boolean Shared) or Lock (Long Position, Long Size, Boolean Shared)

The Zero-Argument Locking Methods Lock The Entire File, Even IT GROWS.

LOCKING PORTIONS OF A Mapped File

//c12:lockingmappedfiles.java

.

Compression

Compression ClassFunctionCheckedInputStream

GetChecksum () Products Checksum for Any InputStream

NOT JUST DECOMPRESSION.

CheckedOutputstream

GetChecksum () Products Checksum for any OutputStream

NOT JUST DECOMPRESSION.

DeflaterOutputStreamBase class for compression classes.ZipOutputStreamA DeflaterOutputStream that compresses data into the ZIP file format.GZIPOutputStreamA DeflaterOutputStream that compresses data into the GZIP file format.InflaterInputStream

An InflaterInputStream That Decompresses Data That Has Been Stored

In the zip file format.

ZipinputStream

An InflaterInputStream That Decompresses Data That Has Been Stored

In the zip file format.

GzipinputStream

An InflaterInputStream That Decompresses Data That Has Been Stored

IN the gzip file format.

These classes are derived form InputStream and OutputStream. This is because the compression library works with bytes, not characters. However, you might sometimes be forced to mix the two types of streams. (Remember

InputStreamReader and

OutputStreamWriter

Simple compression with gzip

//c12:Gzipcompress.java

//Args: promzipcompress.java

//rclean: Test.gz

BufferedReader IN = New BufferedReader (NEW FileReader (File File);

BufferedoutputStream out = new bufferedoutputstream (New Fileoutputstream (New FileoutputStream (File File)); int C;

While ((c = in.read ())! = - 1)

Out.write (c);

In.Close ();

Out.close ();

// read from GzipinputStream

BufferedReader Reader = New BufferedReader (New INPUTSTREADER (New FileInputStream (New FileInputStream (File File)))

String S;

While ((s = reader.readline ())! = null)

System.out.println (s);

Reader.Close ();

Multifile Storage with zip

//c12:Zipcompress.java

FileOutputStream F = New FileoutputStream ("Test.zip");

CheckedoutputStream Csum = New CheckedOutputStream (f, new adler32 ());

ZipOutputStream

ZOS = New ZipOutputStream (CSUM);

BufferedoutputStream out = new bufferedoutputstream

ZOS);

Zos.Setcomment ("a test of java zipping");

For (int i = 0; i

System.out.println ("Writing File" Args [I]);

BufferedReader in =

New BufferedReader (New FileReader (Args [i]));

ZOS.putNextentry (New Zipentry);

INT C;

While ((c = in.read ())! = -1)

Out.write (c);

In.Close ();

}

Out.close ();

// Checksum Valid Only After The File Has Been CLOSED!

System.out.println (

"CHECKSUM:" Csum.getChecksum (). GetValue ());

FileInputStream Fi =

New fileInputstream

Test.zip ");

CheckedInputStream Csumi =

New CheckedInputStream (Fi,

New adler32 ());

ZipinputStream IN2 =

New ZipinputStream (CSUMI);

BufferedInputStream Bis =

New BufferedInputStream (in2);

ZiPENTRY ZE;

While ((ZE = in2.getnextentry ())! =

NULL) {

System.out.println (

"Reading file" ze);

INT X;

While (x = bis.read ())! = -1)

System.write (x);

}

System.out.println (

"Checksum:" csumi.getChecksum (). GetValue ()); bis.close ();

// Alternative Way to Open and read zip files:

Zipfile zf =

NEW zipfile

Test.zip ");

ENUMERATION E = zf.entries ();

While (E.haASMoreElements ()) {

ZIPENTRY ZE2 = (ZIPENTRY) E.NEXTELEMENT ();

System.out.println (

File: " ZE2);

// ... and extract the data as before

}

Java Archives (JARS)

Each of these files was uncompressed. By combining all of the files for a particular applet into a single JAR file, only one server request is necessary and the transfer is faster because of compression. And each entry in a JAR file can be digitally signed for Security (see Chapter 14 for an Example of Signing).

A Jar File Concents of a Single File Containing a Collection of Zipped Files Along With A "Manifest" That Describes Them. (You Create Your Own Manifest File; Otherwise, The Jar Program Will Do It for you.)

Jar [options] destination [manifest] InputFile (s)

Creates A New Or Empty Archive. T Lists the Table of Contents. Xextracts All Files.x FileExtracts The named file.fsays: "i'm going to give you the name of the file." if you don't use this, jar assumes that its input will come from standard input, or, if it is creating a file, its output will go to standard output.mSays that the first argument will be the name of the user-created manifest file.vGenerates verbose output describing what JAR is doing.0only store the file; doesn't compress the files (use to create a jar file "you can put in your classpath). Mx't Automatical Create a manifest file.

EXAMPLES:

Jar cf myjarfile.jar * .class

THIS CREATES A JAR File Called Myjarfile.jar That Contains All of the Class Files in The Current Directory, Along With AUTOMAAATILEGENERATED Manifest file.jar cmf myjarfile.jar mymanifestfile.mf * .class

Like the Previous Example, But Adding a User-created Manifest File Called mymanifestfile.mf.

Jar tf myjarfile.jar

Products a Table of Contents of the files in myjarfile.jar

Jar tvf myjarfile.jar

Adds The "verbose" flag to give more detailed information about the files in myjarfile.jar.

If You Create a Jar File Using The Zero Option, That File Can Be Placed in Your ClassPath:

ClassPath = "lib1.jar; lib2.jar;"

.

Object serialization

Java's object serialization allows you to take any object that implements the Serializable interface and turn it into a sequence of bytes that can later be fully restored to regenerate the original object. This is even true across a netowrk, which means that the serialization mechanism automatically compensates for deifferences in operating systems. That is, you can create an object on a Windows machine, serialize it, and send it across the network to a Unix machine, where it will be correctly reconstructed. you do not have to worry about the data REPRESENTATIONS on The Different Machines, The Byte Ordering, or Any Other Details.

Remember That Persistence Means That An Object's Lifetime Is Not Determined by WHETHER A Program Is Executing; The Object Lives In Between Invocations of the Program.

Object serialization is called "lightweight" is that you can not simply define an object using some kind of "prersistent" keyword and let the system take care of the details (although this might happen in the future). Instead, you must explicitly serialize and deserialize the objects in your program. If you need a more serious persistence mechanism, consider Java Data Object (JDO) or a tool like Hibernate (http://hibernate.sourceforge.net). For details, see Thinking in Enterprise Java, Downloadable Form

Www.bruceeckel.com

Object serialization was added to the language to support two major features:.. Java's Remote Method Invocation (RMI); JavaBeans RMI allows objects that live on other machines to behave as if they live on your machine When sending messages to remote objects, object serialization is necessary to transport the arguments and return values. RMI is discussed in Thinking in Enterprise Java. JavaBeans described in Chapter 14, when a Bean is used, its state information is generally configured at design time. This state information must be stored and later recovered When the program is start; Object Serialization Performs this task.

Serializing An Object Is Quite Simple As Long As The Object Implements The Object Implements

Sikiş si

The Wrappers for the Primitive Types, All of Of

The Container Classes, And Many Others. Even

Class Object Can Be Serialized.

//c12:Worm.java

To make things intending, the array of

Data Objects INSIDE

WORM area in itized with random number. (This Way you don't suked) Eachworm Segment is labeled with a

Char That's Automatically Generated In The Process of Recursively (Recurns) Generating The Linked List of

Worms. When you create a

Worm, you tell the constructor how long you want it to be. To make the

Next Reference, IT Calls the

Worm Constructor with a Length of One Less, etc. The final

Next Reference Is Left As

NULL, INDICATING THE END

Worm.

Object serialization is byte-oriented, and thus buys the inputstream and outputstream hierarchies.

Finding the class

An object to be recovered from its serialized state, suppose you serialize an object and send it as a file or through a network to another machine, then you could on the other machine reconstruct the object using only the contents of the file.

//c12:Alien.java

// a Serializable Class

//c12:freezealien.java

// CREATE A serialized output file.

//c12:Thawalien.java

// Try to recover a serialized file without the class of object what's stored in trata.

Controlling Serialization

You Can Control The Process of Serialization by Implementing The

Externalizable Interface Instead of The

Serializable Interface. The

Externalizable Interface Extends the

Serializable Interface and Adds Two Methods,

Writeexteranl () ()

The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values ​​or calling the writeObject method of ObjectOutput for objects, strings, and arrays.) And

readExternal () (The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays.), that are automatically called for your object during serialization and deserialization so that you can perform Your Special Operations.

//c12: blips.java

// Simple Use of Externalizable & a Pitfall.

// clean: blips.out

When B1 is Recovered, The Blip1 Default Constructionor

(public) IS Called, this is Different Form

Recovering a Serializable Object, in which the object is constructed entirely form its store,

With no constructor calls.

WITH AN EXTERNALIZABLE Object, All The Normal Default Construction Behavior Occurs (Including The Initializations At the point of field definition, and then readexternal () is Called.

Young To Be aware of this - in Particular, The Fact That All The Default Construction Always Takes Place-To Produce The Correct Behavior in Your Externalizable Objects.

//c12:Blip3.java

// Fully Store and Retrieve An Externalizable Object

The Fields

S AND

I are initialized only in the second constructor, but not in the default constructor. this means what if you don't initialize s and i in readexternal (),

S Will Be Null and

i will be zero (since the storage for the object gets wiped to zero in the first step of object creation). If you comment ou the two lines of code following the phrases "You must do this" and run the program, you'll See That When the Object Is Recovered,

s null and

I is zero.

If you are inheriting from an Externalizable object, you'll typically call the base-class versions of writeExternal () and readExternal () to provide proper storage and retrieval of the base-class components.The transient keyword

One Way To Prevent Sensitive Parts of Your Object from Being Serialized Is To Implement your class as

Externalizable, As Shown Previously. Then Nothing is Automatical Serialized, You CAN Explicity Serialize Only the nextsary Parts Inside

Writeexternal ().

If You're Working With a Serializable Object, However, All Serialization Happens AutomaticL. To Control this, you can turn off serialization on a field-by-field basis using the

TRANSIENT Keyword.

For example, consider a Login object that keeps information about a particular login session. Support that, once you verify the login, you want to store the data, but without the password. The easiest way to do this is by implementing Serializable and marking the Password Field as Transient.

//c12:logon.java

PRIVATE TRANSIENT STRING Password;

Since

Externalizable Objects Do Not Store Any of Their Fields by Default, THE

Transient Keyword Is for use with

Serializable Objects ONLY.

An Alternative to Externalizable

If you're not keen on implementing the Externalizable interface, there's another approach. You can implement the Serializable interface and add (notice "add" and not "override" or "implement") methods called writeObject () and readObject () that will Automatically Be Called and Deserialized, Respectively. That IS, IF You Provide There Two Methods, They Will Be Used Instead of The Default Serialization.

Private Void WriteObject (ObjectOutputStream Stream) Throws oException; Private Void ReadObject (ObjectInputStream Stream) throws oews oewception, ClassNotFoundException;

There's one other twist. Inside your writeObject (), you can choose to perform the default writeObject () action by calling defaultWriteObject (). Likewise, inside readObject () you can call defaultReadObject (). Here is a simple example that demonstrates how you CAN Control The Storage and Retrieval of a Serializable Object:

//c12:Serialctl.java

PRIVATE STRING A;

PRIVATE TRANSIENT STRING B;

Private void writeObject (ObjectOutputStream Stream) throws oException {

stream.defaultwriteObject ();

Street.writeObject (b);

}

Private Void ReadObject (ObjectInputStream Stream) throws oException, classnotfoundexception {

Stream.defaultReadObject ();

B = (string) stream.readObject ();

}

In this Example,

Non-Transient Field Is Saved by The DefaultWriteObject () Method and The Transient Field Is Saved and Restored Explicity.

If you are going to use the default mechanism to write the non-transient parts of you object, you must call defaultWriteObject () as the first operation in writeObject (), and defaultReadObject () as the first operation in readObject ().

Versioning

It's possible that you might want to change the version of a serializable class (objects of the original class might be stored in a database, for example). This is supported, but you'll probably do it only in special cases, and it requires An Extra Depth of Understanding That We will not attempt to achieve here. The JDK Documents Downloadable from

Java.sun.com Cover this Topic Quite Thoroughly.

Warning: Serialized Objects of this Class Will Not Be Compatible with Future Swing Releases. The Current Serialization Support Is Appropriate for Short Term Storage or RMI Between Applications ... Using Persistence

Deep Copy: Means That You're Duplicating The Entire Web of Objects, Rather Than Just The Basic Object And ITS References.

What happens if you serialize Two Objects That Both Have a reason to a think Object?

WHEN You Restore Those Two Objects from Their Serialized State, Do You get Only ONE OCCURRENCE OF THIRD OBIT?

What if you serialize your Two Objects to Separate Files and DESERIALIZE THEM IN DefFerent Parts of Your Code?

//c12: MYWORLD.JAVA

// shows the quberm

Results:

Animals: [Bosco the dog [c12.animal@26e431], c12.house@14f8dab

Ralph the hamster [c12.animal@1ddebc3], c12.house@14f8dab

, Frumpk the cat [c12.animal@a18aa2], c12.house@14f8dab

]

Animals1: [BOSCO the dog [c12.animal@10ef90c], c12.house@a32b

Ralph the Hamster [C12.animal@1d8957f], c12.house@a32b

, Frumpk the cat [c12.animal@3ee284], c12.house@a32b

]

Animals2: [bosco the dog [c12.animal@10ef90c], c12.house@a32b

Ralph the Hamster [C12.animal@1d8957f], c12.house@a32b

, Frumpk the cat [c12.animal@3ee284], c12.house@a32b

]

Animals3: [BOSCO the dog [c12.animal@8965fb], c12.house@867e89

Ralph the Hamster [c12.animal@1dd7056], c12.house@867e89

, Fronk the cat [c12.animal@fa3ac1], c12.house@867e89

]

Notice that in animals1 and animals2, the same addresses appear, including the references to the House object that both share. On the other hand, when animals3 is recovered, the system has no way of knowing that the objects in this other stream are aliases of the objects in the first stream, so it makes a completely different web of objects.As long as you're serializing everything to a single stream, you'll be able to recover the same web of objects that you wrote, with no accidental duplication . of objects of course, you can change the state of your objects in between the time you write the first and the last ,, but that's your responsibility; the objects will be written in whatever state they are in (and with whatever connections they have TO Other Objects) at the time you serialize.

THE SAFEST Thing to do if you want to save the state of a system is to serialize as an "atomic" Operation. If you Serialize Some Things, Do Some Other, And Serialize Some More, Etc., Then You Will Not Be Storing the system safely. Instead, put all the objects that comprise the state of your system in a single container and simply write that container out in one operation. Then you can restore it with a single method call as well.

SERIALIZABLE CLASS SHOULDS BY SIMPLY SERIALIZING The Class Object.

//c12:CadState.java

// Saving and Restoring The State of a Pretennd CAD SYSTEM.

// Clean: Cadstate.out

Class Line Extends Shape {

Private static int color = red;

public static void serializeStaticState (ObjectOutputStream os) throws IOException {os.writeInt (color);} public static void deserializeStaticState (ObjectInputStream os) throws IOException {color = os.readInt ();} public Line (int xVal, int yVal, int dim ) {Super (XVAL, YVAL, DIM);

}

Public void setColor (int newcolor) {color = newcolor;

Public int getColor () {Return Color;}

}

The Shape class implements Serializable, so anything that is inherited from Shape is automatically Serielizable as well. Each Shape contains data, and each derived Shape class contains a static field that determines the color of all of those types of Shapes. (Placing a static field in the base class would result in only one field) Methods in the base class can be overridden to set the color for the various types (static methods are not dynamically bound, so these are normal methods). The randomFactory () method creates a different Shape Each Time You Call IT, Using Random Values ​​for the Shape Data.

Circle and Square Are StraightForward Extensions of Shape.

In main (), one ArrayList is used to hold the Class objects and the other to hold the shapes. If you do not provide a command-line argument, the shapeTypes ArrayList is created and the Class objects are added, and then the shapes Arraylist is created and shape objects are added. Next, all the static color color values ​​are set to green, and everything is serialized to the file cadstate.out.

If You Provide A Command-Line Argument (PRESUMABLY CADSTATE.OUT), That File Is Oppened and used to restore The State of Program. In Both Situations, The Resulting ArrayList of Shapes Is Prograde.

$ Java Cadstate

[

Class Circlecolor [3] XPOS [71] YPOS [82] DIM [44]

,

Class Squarecolor [3] XPOS [98] YPOS [21] DIM [49],

Class Linecolor [3] XPOS [16] YPOS [80] DIM [37]

,

Class Circlecolor [3] XPOS [51] YPOS [74] DIM [7]

, Class SquareColor [3] XPOS [7] YPOS [78] DIM [98], Class LineColor [3] XPOS [38] YPOS [79] DIM [93], Class CircleColor [3] XPOS [84] YPOS [12] DIM [62], Class Squarecolor [3] XPOS [16] YPOS [51] DIM [94], Class LineColor [3] XPOS [51] YPOS [0] DIM [73], Class Circlecolor [3] XPOS [47] YPOS [6] DIM [49]] $ JAVA CADSTATE CADSTATATE.OUT [Class CircoleColor [1] XPOS [71] YPOS [82] DIM [44], Class Squarecolor [0] XPOS [98] YPOS [21] DIM [49 ], Class LineColor [3] XPOS [16] YPOS [80] DIM [37], Class CircoleColor [1] XPOS [51] YPOS [74] DIM [7], Class SquareColor [0] XPOS [7] YPOS [78 ] DIM [98], Class LineColor [3] XPOS [38] YPOS [79] DIM [93], Class CircoleColor [1] XPOS [84] YPOS [12] DIM [62], Class SquareColor [0] XPOS [16 ] YPOS [51] DIM [94], Class LineColor [3] XPOS [51] YPOS [0] DIM [73], Class Circlecolor [1] XPOS [47] YPOS [6] DIM [49]

]

You can see that the values ​​of xPos, yPos, and dim were all stored and recovered successfully, but there's something wrong with the retrieval of the static information. It's all "3" going in, but it does not come out that way. Circles have a value of 1 (RED, which is the definition), and Squares have a value of 0 (remember, they are initialized in the constructor). It's as if the statics did not get serialized at all! That's right-even Though Class Class Is Serializable, IT Doesn't Do What You Expect. so if you want to seialize statics, you must do it yourself.

This is what the serializestaticstate () and deserializestaticState () static methods in line area.thus to make cadstate.java run correctly, you must:

Add a serializeStaticState () and deserializeStaticState () to the shapes. Remove the ArrayList shapeTypes and all code related to it. Add calls to the new serialize and deserialize static methods in the shapes.Preferences

JDK1.4 introduced the Preferences API, which is much closer to persistence than object serialization because it automatically stores and retrieves your information. However, its use is restricted to small and limited data sets-you can only hold primitives and Strings, and the length of each stored String can not be longer than 8K (not tiny, but you do not want to build anything serious with it, either). As the name suggests, the preferences API IS designed to store and retrieve user preferences and program- Configuration settings.

//c12:PreferenceSdemo.java

Here, userNodeForPackage () is used, but you could also choose systemNodeForPackage (); the choice is somewhat arbitrary, but the idea is that "user" is for individual user preferences, and "system" is for general installation configuration Since main (. ) is static, PreferencesDemo.class is used to identify the node, but inside a non-static method, you'll usually use getClass (). you do not need to use the current class as the node identifier, but that's the usual Pratice.

Once you create the node, it's available for either loading or reading data. This example loads the node with various types of items and then gets the keys (). These come back as a String [], which you might not expect if you ' re used to keys () in collections library. Here, they're converted to a List that is used to produce an Iterator for printing keys and values. Notice the second argument to get (). This is the default value that is produced if there is not any entry for that key value While iterating through a set of keys, you always know there's an entry, so using null as the default is safe, but normally you'll be fetching a named key, as in:. prefs .Getint ("Companions", 0);

In The Normal Case, You'll Want To Provide a Reasonable Default Value. In Fact, A Typical IDiom (habits) IS Seen in The Lines:

INT usagecount = prefs.getint ("usagecount", 0);

UsageCount ;

Prefs.putint ("usagecount", usagecount;

This Way, The First Time You Run The Program, The USAgeCount Will Be Zero, But on Subsequent Invocations It Will Be Nonzero.

When you run PreferencesDemo.java you'll see that the UsageCount does indeed increment every time you run the program, but where is the data stored? There's no local file that appears after the program is run the first time. The Preferences API uses appropriate system resources to accomplish its task, and these will vary depending on the OS. In Windows, the registry is used. But the whole point is that the information is magically stored for you so that you do not have to worry about how it works Form One System To Another.

Regular Expressions

Technically, Rugular Expressions Are String Manipulation Tools.

Regular expressions are powerful and flexible text-processing tools. They allow you to specify, programmatically, complex patterns of text that can be discovered in an input string. Once you discover these patterns, you can then react to them any way you want. Although the syntax of regular expressions can be initmidating at first, they provide a compact and dynamic language that can be employed to solve all sorts of string processing, matching and selection, editing, and verification problems in a completely general way.Creating regular expressions

You can begin learning regular expressions with a useful subset of the possible constructs. A complete list of constructs for building regular expressions can be found in the javadocs for the Pattern class for package java.util.regex.

Charactersbthe Specific Character B / XHHCHARACTER with HEX VALUE 0xHH / UHHHHHHE Unicode Character with Hex REPRESENTATION 0XHHHH / TTAB / NNEWLINE / RCARRIAGE RETURN / FFORM feed / Eescape

The Power of Regular Expressions Begins To Appear When Defining Character Classes. Here Area Typical Ways To Create Character Classes: And Some Predefined Classes:

Character class.represents any character [abc] Any of the character, b, or c (Same as a | b | c) [^ ABC] Any Character Except A, B, AND C (Negation) [A-ZA-Z ] Any Character a Through z OR A THROUGH Z (RANGE) [ABC [Hij]] Any Of A, B, C, H, I, J (Same AS A | B | C | H | i | J) (Union) [A-Z && [Hij]] Either H, I, OR J (Intersection) / SA Whitespace Character (Space, Tab, Newline, Formfeed, Carriage Return) / SA Non-Whitespace Character ([^ / S]) / Da Numeric Digit [0-9] / da Non-Digit [^ 0-9] / WA Word Character [A-ZA-Z_0-9] / WA Non-Word Character [^ / W]

In Java, "//" means "I'm inserting a regular expression backslash, so the following character has special meaning." For example, if you want to indicate one or more word characters, your regular expression string will be '// W '. if you want to insert a literal backslash, you say' '. However, Things Like Newlines and Tabs Just Use A Single Backslash: "/ N / T".

Logical Operatorsxyx Followed by YX | YX or Y (x) a capturing group. You can Refer to the ith captured Group Later in The Expression with / i

Boundary matcher ^ beginning of a line $ END of a line / bword boundary / bnon-word boundary / gend of the presentary match

AS An Example, Each of the Following Repesent Valid Regular Expressions, And All Will Success, Match The Character Sequence "Rudolph":

Rudolph

[rr] udolph

[rr] [aeiou] [a-z] ip *

R. *

Quantifiers

A Quantifier Describes The Way That A Pattern Absorbs Input Text:

Greedy: Quantifiers are greedy unless otherwise altered A greedy expression finds as many possible matchers for the pattern as possible A typical cause of problems is to assume that your pattern will only match the first possible group of characters, when it's actually greedy and will.. keep going Reluctant:. Specified with a question mark, this quantifier matches the minimum necessary number of characters to satisfy the pattern Also called lazy, minimal matching, non-greedy, or ungreedy Possessive:.. Currently only available in Java, and it is more advanced, so you probably will not use it right away. As a regular expression is applied to a string, it generates many states so that it can backtrack if the match fails. Possessive quantifiers do not keey those intermediate states, and thus prevent backtracking. They can be used to prevent a regular expression from running away and also to make it execute more efficiently.GreedyReluctantPossessiveMatchesX? X ?? X? X, one or noneX * X *? X * X, zero or m OREX X ? X X, One or morex {n} x {n}? x {n} x, exactly n Timesx {n,} x {n,}? x {n,} x, at Least N Timesx {n, m} x {n, m}? x {n, m} x, at Least N But Not More Than M Times

For example:

ABC

Might seem like it would match the sequence 'abc' one or more times, and if you apple it to the input string 'abcabcabc', you will in fact get three matches. However, the expression actually says "match 'ab' followed by One or more oscurrence of 'c' ". To match the entire string 'abc' One or more Times, You Must Say:

(ABC)

Charsequence

THE STRING, STRINGBUFFER, AND Charbuffer Classes Have Been Modified to Implement This New Charsequence Interface. Many Regular Expression Operations Take Charsequence Arguments.pattern and Matcher

//c12:TestRegularExpression.java

// allow you to easly try out regular expressions.

// args: abcabcabcdefabc "abc " "(abc) " (abc) {2,} "

Regular expressions are implemented in Java throuth the Pattern and Matcher classes in the package java.util.regex. A Pattern object represents a compiled version of a regular expression. The static compile () method compiles a regular expression string into a Pattern object. As .,....................

Pattern P = Pattern.Compile ("a * b");

Matcher m = p.matcher ("aaaaab");

Boolean b = m.matches ();

oral

Boolean B = Pattern.matches ("A * B", "AAAAAAb");

For Quickly Discerning IF Regex Can Be Found in Input () Method That Produces An Arby of String That Has Been Broken Around Matches of The Regex.

. A Matcher object is generated by calling Pattern.matcher () with the input string as an argument The Matcher object is then used to access the results, using methods to evaluate the success or failure of different types of matches:

Boolean matches (); boolean lookingat (); boolean find (); boolean find (int start)

Find ()

.

//c12:FindDemo.java

The pattern '// w ' indicates "one or more word characters", so it will simple split up the input into words. Find () is like an iterator., Moving forward through the input string. However, the second version of find .

Groups

.

Group Zero Indicates The Whole Expression Match, Group One Is The First Parenthesized Group, Etc. Thus in INE

A (B (c)) D - There Are Three Groups: Group 0 IS ABCD, GROUP 1 IS BC, Group 2 IS C

The Matcher Object Has Methods To Give You Information About Groups:

Public int groupcount () Returns the number of groups in this matcher pattern. Group Zero is not inclined in this count.

Public String Group () Returns Group Zero (The Entire Match) from The Previous Match Operation (FOR).

public String group (int i) returns the given group number during the previous match operation. If the match was successful, but the group specified failed to match any part of the input string, then null is returned.

Public int Start (Int Group) Returns The Start Index of The Group Found in The Previous Match Operation.

Public int end and the place character, Plus ONE, of The Group Found in The Previous Match Operation.

//c12:Groups.java

THE REGULAR Expressions ("(? M) (// s ) // s (// s ) // s (// s )) $") Pattern Has Four Parentheses So That It Has Five Groups. (/ S ) means consisting of any number of non-whitespace characters; (/ s ) means consisting of any number of witespace characters; '$' means the end of a line; (? m) explicitly tell the regular expression to pay attention to newlines within the INPUT.START () and end ()

Following a successful matching operation, start () returns the start index of the previous match, and end () returns the index of the last character matched, plus one. Invoking either start () or end () following an unsuccessful matching operation produces an IllegalStateException. The Following Program Also DemonStrates Matches () and loops ():

//c12:Startend.java

Notice that find () will locate the regular expression anywhere in the input, but lookingAt () and matches () only succeed if the regular expression starts matching at the very (true) beginning of the input. While matches () only succeeds if .............

Pattern Flags

An alternative compile () Method Accepts Flags That Affect The Behavior of Regular Expression Matching:

Pattern Pattern.Compile (String Regex, INT FLAG)

Compile FlagEffectPattern.CANON_EQTwo characters will be considered to match if, and only if, their full canonical decompositions match. The expression "a / u030A", for example, will match the string "?" When this flag is specified. By default, matching does not take canonical equivalence into account.Pattern.CASE_INSENSITIVE (? i) By default, case-insensitive matching assumes that only characters in the US-ASCII character set are being matched. This flag allows your pattern to match without regard to case (upper or lower). Unicode-aware case-insensitive matching can be enabled by specifying the UNICODE_CASE flag in conjunction with this flag.Pattern.COMMENTS (? x) in this mode, whitespace is ignored, and embedded comments starting with # are ignored until the end of a line. Unix lines mode can also be enabled via the embedded flag expression.Pattern.DOTALL (? s) In dotall mode, the expression '.' matches any character, including a line terminator. By default, the '.' Expression Does Not Match Li ne terminators.Pattern.MULTILINE (? m) In multiline mode, the expressions '^' and '$' match the beginning and ending of a line, respectively. '^' also matches the beginning of the input string, and '$' also matches the end of the input string. by default, these expressions only match at the beginning and the end of the entire input string.Pattern.UNICODE_CASE (? u) When this flag is specified, case-insensitive matching, when enabled by the CASE_INSENSITIVE flag, is done in a manner consistent with the Unicode Standard. By default, case-insensitive matching assumes that only characters in the US-ASCII character set are being matched.Pattern.UNIX_LINES (?

d) In this mode, only the '/ n' line terminator is recognized in the behavior of '.', '^', and '$'. Particularly useful among these flags are Pattern.CASE_INSENSITIVE, Pattern.MULTILINE, and Pattern. COMMENTS (which is helpful for clarity and / or documentation). Note that the behavior of most of the flags can also be obtained by inserting the parenthesized characters, shown in the table beneath the flags, into you regular expression preceding the place where you want The Mode To Take Effect.

You can combine the effect of these and other flaggs through an "ory" ("|") Operation:

//c12:Reflags.java

Pattern P = Pattern.Compile ("^ java", pattern.case_insensitive | pattern.multiline);

That Creates A Pattern That Will Match Lines Starting with "Java", "Java", "Java", etc .. Note That the group () method Only produces the match.

Split ()

Splitting Divieds An Input String Into An Array of String Objects, Delimited by The Regular Expression.

String [] split (charsequence charseq)

String [] split (charsequence charseq, int limit)

//c12:splitDemo.java

String input =

"This !! unusual use !! of exclamation !! points";

System.out.println (arrays.aslist)

Pattern.Compile

"!!"). Split (input));

// only do the first three:

System.out.println (arrays.aslist)

Pattern.Compile

"!!"). Split (Input, 3)))))

System.out.println (arrays.aslist)

"AHA! STRING HAS A Split () Built in!". Split

"")));

Results:

"[This, unusual use, of exclamation, points],

"[This, unusual use, of exclamation !! Points",

"[AHA!, STRING, HAS, A, SPLIT (), Built, In!]"

Notice that regular expressions are so valuable that some operations have also been added to the String class, including split (), matches (), replaceFirst (), and replaceAll (). These behave llike their Pattern and Matcher counterparts.Replace operations

Regular Expressions Become Especially Useful When You Begin Replacing Text.

Replacefirst (String Replacement) Replace The First Matching Part of The Input String with PART OF THE INPUT STRING WITH

Replacement.

Replaceall (String Replacement) Replace Every Matching Part of The Input String with the INPUT STRING WITH

Replacement.

AppendReplacement (StringBuffer Sbuf, String Replacement) Performs Step-by-Step Replacements Into

SBUF, Rather Than Replacing Only The First One or All of Them, AS in

REPLACEFIRST () AND

ReplaceAll (), respectively. this is a

Very Important Method, Because It Allows you to call methods and perform Other processing in Order to Productuce

Replacement

REPLACEFIRST () AND

Replaceall () Are Only Able to Put in fixed strings. with this method, you can programatically pick apart the groups and create Powerful Replacements.

Appendtail (StringBuffer Sbuf, String Replacement) Is Invoked After One or More Invocations of To

AppendReplacement () Method in Order to Copy The Remainder of The Input String.

RESET ()

An EXISTING MATCHER Object Can Be Applied to a New Character Sequence Using The Reset () Method:

//c12:Resetting.java

Matcher M = Pattern.Compile ("[FRB] [AIU] [GX]"). Matcher ("Fix The Rug With Bags");

M.Reset ("FIX THE RIG WITH RAGS");

RESULT:

FIX "

"rug"

"BAG"

FIX "

"rig"

"rag"

RESET () WITHOUT Any Arguments Sets The Matcher To The Beginning Of The Current Sequence.

Regular Expressions and Java I / O

Most of the Expressions Applied to Static Strings. The Following Example Shows One Way to Apply Regular Expressions to Search for matches in a file.java

// args: jgrep.java // b [SSCT] // w

The Test Arguments Open The JGrep.java File to Read As Input, And Search for Words Starting with [SSCT].

Is StringTokenizer Needed?

The new capabilities provided with regular expressions might prompt you to wonder whether the original StringTokenizer class is still necessary. Before JDK1.4, the way to split a string into parts was to "tokenize" it with StringTokenizer. But now it's much easier and more Succinct to the Same Thin with regular expression:

//c12:ReplacingStringTokenizer.java

String infut = "But I'm not dead yet! I feel happy!";

StringTokenize Stoke = New StringTokenize (Input);

While (stoke.hasmreeelements ())

System.out.println (stoke.nextToken ());

转载请注明原文地址:https://www.9cbs.com/read-36611.html

New Post(0)