锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲激情视频在线观看,亚洲日本va午夜中文字幕久久,亚洲视频在线一区二区http://www.tkk7.com/yanchengxiaotubao/category/52682.htmlOracle 鍦ㄨ礬涓?..zh-cnWed, 21 Nov 2012 19:48:44 GMTWed, 21 Nov 2012 19:48:44 GMT60Concurrency - sleeping priorityhttp://www.tkk7.com/yanchengxiaotubao/articles/391687.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Wed, 21 Nov 2012 02:56:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/391687.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/391687.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/391687.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/391687.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/391687.htmlSleeping
A simple way to affect the behavior of your tasks is by calling sleep( ) to cease (block) the execution of that task for a given time.
If you must control the order of execution of tasks, your best bet is to use synchronization controls.
public class SleepingTask extends LiftOff{
    
public void run(){
        
try {
            
while(countDown-- > 0){
                System.out.print(status());
                
// Old-style:
                
// Thread.sleep(100);
                
// Java SE5/6-style
                TimeUnit.MICROSECONDS.sleep(100);
            }
        } 
catch (InterruptedException e) {
            
// TODO: handle exception
        }
    }
    
    
public static void main(String[] args)  {
        ExecutorService exec 
= Executors.newCachedThreadPool();
        
for(int i=0; i<5; i++)
            exec.execute(
new SleepingTask());
        exec.shutdown();
    }
}


Priority
The priority of a thread conveys the importance of a thread to the scheduler.
However, this doesn’t mean that threads with lower priority aren’t run (so you can’t get deadlocked because of priorities).
Lower-priority threads just tend to run less often.
Trying to manipulate thread priorities is usually a mistake.

Although the JDK has 10 priority levels, this doesn’t map well to many operating systems.
public class SimplePriorities implements Runnable {
    
private int countDown = 5;
    
private volatile double d; //No optimization
    private int priority;
    
public SimplePriorities(int priority){
        
this.priority = priority;
    }
    
    
public String toString(){
        
return Thread.currentThread() + "" + countDown;
    }

    @Override
    
public void run() {
        Thread.currentThread().setPriority(priority);
        
while(true){
            
// An expensive, interruptable operation
            for(int i=1; i<10000; i++){
                d 
+= (Math.PI + Math.E)/(double)i;
                
if(i%1000 == 0)
                    Thread.yield();
            }
            System.out.println(
this);
            
if(--countDown ==0)
                
return;
        }
    }

    
public static void main(String[] args) {
        ExecutorService exec 
= Executors.newCachedThreadPool();
        
for(int i=0; i<5; i++)
            exec.execute(
new SimplePriorities(Thread.MIN_PRIORITY));
        exec.execute(
new SimplePriorities(Thread.MAX_PRIORITY));
        exec.shutdown();
    }
}


















]]>
Concurrency - why use & many faceshttp://www.tkk7.com/yanchengxiaotubao/articles/391457.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Fri, 16 Nov 2012 07:43:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/391457.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/391457.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/391457.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/391457.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/391457.htmlIf you ignore it, you're likely be get bitten.

The many faces of concurrency:
The problems that you solve with concurrency can be roughly classified as "speed" and "design manageability."
  1. Faster execution -If you want a program to run faster, break it into pieces and run each piece on a separate processor.
  2. Utilize multiple-precessor system

To define a task, simply implement Runnable and write a run( ) method to make the task do your bidding.
/**
 * Display the countdown before liftoff.
 * Demonstration of the Runnable interface
 * 
@author WPeng
 *
 * 
@since 2012-11-19
 
*/
public class LiftOff implements Runnable{
    
protected int countDown = 10//Default
    private static int taskCount = 0;
    
private final int id = taskCount++;
    
    
public LiftOff(){}
    
    
public LiftOff(int countDown){
        
this.countDown = countDown;
    }
    
    
public String status(){
        
return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff"+ ") ,";
    }

    @Override
    
public void run() {
        
while(countDown-- >0){
            System.out.println(status());
            
//I’ve done the important parts of my cycle 
            
//and this would be a good time to switch to another task for a while.
            Thread.yield();
        }
    }
    
    
public static void main(String[] args){
        LiftOff launch 
= new LiftOff();
        launch.run();
    }
}
/**
#0(9) ,
#0(8) ,
#0(7) ,
#0(6) ,
#0(5) ,
#0(4) ,
#0(3) ,
#0(2) ,
#0(1) ,
#0(LiftOff) ,
 
*/

 




A Thread constructor only needs a Runnable object
Calling a Thread object’s start( ) will perform the necessary initialization for the thread and then call that Runnable’s run( ) method to start the task in the new thread.

/**
 * The most basic use of Thread class
 * 
@author WPeng
 *
 * 
@since 2012-11-20
 
*/
public class BasicThreads {

    
public static void main(String[] args) {
        Thread t 
= new Thread(new LiftOff());
        t.start();
        System.out.println(
"Waiting for LiftOff");
    }
}
/**
 *     Waiting for LiftOff
 *    #0(9) ,#0(8) ,#0(7) ,#0(6) ,#0(5) ,#0(4) ,#0(3) ,#0(2) ,#0(1) ,#0(LiftOff) ,
 * 
*/

 



 

Executors are the preferred method for starting tasks in Java SE5/6.
We can use an Executor instead of explicitly creating Thread objects.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CashedThreadPool {

    
public static void main(String[] args) {
        ExecutorService exec 
= Executors.newCachedThreadPool();
        
for(int i=0; i<5; i++)
            exec.execute(
new LiftOff());
        exec.shutdown();
    }
}
Very often, a single Executor can be used to create and manage all the tasks in your system.
The current thread (in this case, the one driving main( )) will continue to run all tasks submitted before shutdown( ) was called. The program will exit as soon as all the tasks in the Executor finish.


 


 

Note that in any of the thread pools, existing threads are automatically reused when possible.


A Runnable is a separate task that performs work, but it doesn’t return a value. If you want the task to produce a value when it’s done, you can implement the Callable interface rather than the Runnable interface.
class TaskWithResult implements Callable<String>{
    
private int id;
    
public TaskWithResult(int id){
        
this.id = id;
    }
    
    @Override
    
public String call() {
        
return "result of TaskWithResult " + id;
    }
}

public class CallableDemo  {

    
public static void main(String[] args) {
        ExecutorService exec 
= Executors.newCachedThreadPool();
        ArrayList
<Future<String>> results = new ArrayList<Future<String>>();
        
for(int i=0; i<10; i++)
            
//with a type parameter representing the return value from the method call( ) 
            
//(instead of run( )), and must be invoked using an ExecutorService submit( ) method.
            results.add(exec.submit(new TaskWithResult(i)));
        
for(Future<String> fs : results){
            
try {
                
// get() blocks until completion:
                System.out.println(fs.get());
            } 
catch (Exception e) {
                
// TODO: handle exception
            } finally{
                exec.shutdown();
            }
        }
    }
}

 





]]>
I/O Object serializationhttp://www.tkk7.com/yanchengxiaotubao/articles/391000.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Thu, 08 Nov 2012 02:09:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/391000.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/391000.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/391000.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/391000.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/391000.htmlWhen you create an object, it exists for as long as you need it, but under no circumstances does it exist when the program terminates.
there are situations in which it would be incredibly useful if an object could exist and hold its information even while the program wasn’t running.
the next time you started the program, the object would be there and it would have the same information it had the previous time the program was running.
Of course, you can get a similar effect by writing the information to a file or to a database, but in the spirit of making everything an object, it would be quite convenient to declare an object to be "persistent," and have all the details taken care of for you.

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 network, which means that the serialization mechanism automatically compensates for differences 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 don’t have to worry about the data representations on the different machines, the byte ordering, or any other details.

Object serialization was added to the language to support two major features.
  • Java’s Remote Method Invocation (RMI) allows objects that live on other machines to behave as if they live on your machine.
    When messages are sent to remote objects, object serialization is necessary to transport the arguments and return values.
  • Object serialization is also necessary for JavaBeans.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 started; object serialization performs this task.

Serializing an object is quite simple as long as the object implements the Serializable interface.

 

When serialization was added to the language, many standard library classes were changed to make them serializable, including all of the wrappers for the primitive types, all of the container classes, and many others. Even Class objects can be serialized.

 


To serialize an object, you create some sort of OutputStream object and then wrap it inside an ObjectOutputStream object. At this point you need only call writeObject( ), and your object is serialized and sent to the OutputStream (object serialization is byte-oriented, and thus uses the InputStream and OutputStream hierarchies). To reverse the process, you wrap an InputStream inside an ObjectlnputStream and call readObject( ). What comes back is, as usual, a reference to an upcast Object, so you must downcast to set things straight.


Externalizable
The Externalizable interface extends the Serializable interface and adds two methods, writeExternal( ) and readExternal( ), that are automatically called for your object during serialization and deserialization so that you can perform your special operations.
This is different from recovering a Serializable object, in which the object is constructed entirely from its stored bits, with no constructor calls.
the fact that: all the default construction always takes place—to produce the correct behavior in your Externalizable objects.

Here’s an example that shows what you must do to fully store and retrieve an Externalizable object:
//: io/Blip3.java
// Reconstructing an externalizable object.
import java.io.*;
import static net.mindview.util.Print.*;

public
 class Blip3 implements Externalizable {
    private int i;
    private String s; // No initialization
   
    public Blip3() {
        print("Blip3 Constructor");
        // s, i not initialized
    }

    public Blip3(String x, int a) {
        print("Blip3(String x, int a)");
        = x;
        = a;
        // s & i initialized only in non-default constructor.
    }

    public String toString() { return s + i; }

    public void writeExternal(ObjectOutput out) throws IOException {
        print("Blip3.writeExternal");
        // You must do this:
        out.writeObject(s);
        out.writeInt(i);
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        print("Blip3.readExternal");
        // You must do this:
        = (String)in.readObject();
        = in.readInt();
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        print("Constructing objects:");
        Blip3 b3 = new Blip3("A String "47);
        print(b3);
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blip3.out"));
        print("Saving object:");
        o.writeObject(b3);
        o.close();
        // Now get it back:
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blip3.out"));
        print("Recovering b3:");
        b3 = (Blip3)in.readObject();
        print(b3);
    }
}
If you comment out 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 is 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.

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 automatically serialized, and you can explicitly serialize only the necessary parts inside writeExternal( ).


transient
If you’re working with a Serializable object, however, all serialization happens automatically. To control this, you can turn off serialization on a field-by-field basis using the transient keyword, which says, "Don’t bother saving or restoring this—I’ll take care of it."

For example, consider a Logon object that keeps information about a particular login session. Suppose 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.
Here’s what it looks like:
//: io/Logon.java
// Demonstrates the "transient" keyword.
import java.util.concurrent.*;
import java.io.*;
import java.util.*;
import static net.mindview.util.Print.*;

public class Logon implements Serializable {
    
private Date date = new Date();
    
private String username;
    
private transient String password;

    
public Logon(String name, String pwd) {
        username 
= name;
        password 
= pwd;
    }

    
public String toString() {
        
return "logon info: \n username: " + username + "\n date: " + date
                
+ "\n password: " + password;
    }

    
public static void main(String[] args) throws Exception {
        Logon a 
= new Logon("Hulk""myLittlePony");
        print(
"logon a = " + a);
        ObjectOutputStream o 
= new ObjectOutputStream(new FileOutputStream(
                
"Logon.out"));
        o.writeObject(a);
        o.close();
        TimeUnit.SECONDS.sleep(
1); // Delay
        
// Now get them back:
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                
"Logon.out"));
        print(
"Recovering object at " + new Date());
        a 
= (Logon) in.readObject();
        print(
"logon a = " + a);
    }
/*
 * Output: (Sample) 
 * logon a = logon info: 
 *         username: Hulk 
 *         date: Sat Nov 19 15:03:26 MST 2005 
 *         password: myLittlePony 
 * Recovering object at Sat Nov 19 15:03:28 MST 2005 
 * logon a = logon info: 
 *         username: Hulk 
 *         date: Sat Nov 1915:03:26 MST 2005 
 *         password: null
 
*/// :~


Anything defined in an interface is automatically public, so if writeObject( ) and readObject( ) must be private, then they can’t be part of an interface. Since you must follow the signatures exactly, the effect is the same as if you’re implementing an interface.
It would appear that when you call ObjectOutputStream.writeObject( ), the Serializable object that you pass it to is interrogated (using reflection, no doubt) to see if it implements its own writeObject( ). If so, the normal serialization process is skipped and the custom writeObject( ) is called. The same situation exists for readObject( ).


It’s quite appealing to use serialization technology to store some of the state of your program so that you can easily restore the program to the current state later.

But before you can do this, some questions must be answered. What happens if you serialize two objects that both have a reference to a third object? When you restore those two objects from their serialized state, do you get only one occurrence of the third object? What if you serialize your two objects to separate files and deserialize them in different parts of your code?

//: io/MyWorld.java
import java.io.*;
import java.util.*;
import static net.mindview.util.Print.*;

class House implements Serializable {
}

class Animal implements Serializable {
    
private String name;
    
private House preferredHouse;

    Animal(String nm, House h) {
        name 
= nm;
        preferredHouse 
= h;
    }

    
public String toString() {
        
return name + "[" + super.toString() + "], " + preferredHouse + "\n";
    }
}

public class MyWorld {
    
public static void main(String[] args) throws IOException,
            ClassNotFoundException {
        House house 
= new House();
        List
<Animal> animals = new ArrayList<Animal>();
        animals.add(
new Animal("Bosco the dog", house));
        animals.add(
new Animal("Ralph the hamster", house));
        animals.add(
new Animal("Molly the cat", house));
        print(
"animals: " + animals);
        ByteArrayOutputStream buf1 
= new ByteArrayOutputStream();
        ObjectOutputStream o1 
= new ObjectOutputStream(buf1);
        o1.writeObject(animals);
        o1.writeObject(animals); 
// Write a 2nd set
        
// Write to a different stream:
        ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
        ObjectOutputStream o2 
= new ObjectOutputStream(buf2);
        o2.writeObject(animals);
        
// Now get them back:
        ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(
                buf1.toByteArray()));
        ObjectInputStream in2 
= new ObjectInputStream(new ByteArrayInputStream(
                buf2.toByteArray()));
        List animals1 
= (List) in1.readObject(), animals2 = (List) in1
                .readObject(), animals3 
= (List) in2.readObject();
        print(
"animals1: " + animals1);
        print(
"animals2: " + animals2);
        print(
"animals3: " + animals3);
    }
}
/* Output: (Sample)
animals: [Bosco the dog[Animal@addbf1], House@42e816
, Ralph the hamster[Animal@9304b1], House@42e816
, Molly the cat[Animal@190d11], House@42e816
]
animals1: [Bosco the dog[Animal@de6f34], House@156ee8e
, Ralph the hamster[Animal@47b480], House@156ee8e
, Molly the cat[Animal@19b49e6], House@156ee8e
]
animals2: [Bosco the dog[Animal@de6f34], House@156ee8e
, Ralph the hamster[Animal@47b480], House@156ee8e
, Molly the cat[Animal@19b49e6], House@156ee8e
]
animals3: [Bosco the dog[Animal@10d448], House@e0e1c6
, Ralph the hamster[Animal@6ca1c], House@e0e1c6
, Molly the cat[Animal@1bf216a], House@e0e1c6
]
*///:~
Animal objects contain fields of type House. In main( ), a List of these Animals is created and it is serialized twice to one stream and then again to a separate stream. When these are deserialized and printed, you see the output shown for one run (the objects will be in different memory locations each run).

One thing that’s interesting here is that it’s possible to use object serialization to and from a byte array as a way of doing a "deep copy" of any object that’s Serializable. (A deep copy means that you’re duplicating the entire web of objects, rather than just the basic object and its references.) Object copying is covered in depth in the online supplements for this book.














]]>
I/O Compressionhttp://www.tkk7.com/yanchengxiaotubao/articles/390996.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Thu, 08 Nov 2012 01:30:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390996.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390996.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390996.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390996.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390996.htmlpublic class GZIPcompress {

    
public static void main(String[] args) throws IOException {

        BufferedReader in 
= new BufferedReader(
                
new FileReader("test.txt"));
        BufferedOutputStream out 
= new BufferedOutputStream(
                
new GZIPOutputStream(new FileOutputStream("test.gz")));
        System.out.println(
"Writing file");
        
int c;
        
while((c = in.read())!=-1){
            out.write(c);
        }
        in.close();
        out.close();
        
        System.out.println(
"Reading file");
        BufferedReader in2 
= new BufferedReader(
                
new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz"))));
        String s;
        
while((s=in2.readLine())!=null){
            System.out.println(s);
        }
    }
}
The use of the compression classes is straightforward; you simply wrap your output stream in a GZIPOutputStream or ZipOutputStream, and your input stream in a GZIPInputStream or ZipInputStream. All else is ordinary I/O reading and writing. This is an example of mixing the char-oriented streams with the byte-oriented streams; in uses the Reader classes, whereas GZIPOutputStream’s constructor can accept only an OutputStream object, not a Writer object. When the file is opened, the GZIPInputStream is converted to a Reader.



Multifile storage with Zip
it shows the use of the Checksum classes to calculate and verify the checksum for the file. There are two Checksum types: Adler32 (which is faster) and CRC32 (which is slower but slightly more accurate).

/**
 * Uses ZIP compression to compress any number of files given on the command line.
 * 
@author WPeng
 *
 * 
@since 2012-11-8
 
*/
public class ZipCompress {

    
public static void main(String[] args) throws IOException{
        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");
        
        
// No corresponding getComment(), though
        for(String arg : args){
            System.out.println(
"Wrting file " + arg);
            BufferedReader in 
= new BufferedReader(new FileReader(arg));
            zos.putNextEntry(
new ZipEntry(arg));
            
int c;
            
while((c = in.read())!=-1)
                out.write(c);
            in.close();
            out.flush();
        }
        
// Checksum valid only after the file has been closed!
        System.out.println("Checksum: " + csum.getChecksum().getValue());
        
        
// Now extract the files:
        System.out.println("Reading file");
        FileInputStream fin 
= new FileInputStream("test.zip");
        CheckedInputStream csumi 
= new CheckedInputStream(fin, 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.out.println(x);
        }
        
        
if(args.length==1){
            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.hasMoreElements()){
            ZipEntry ze2 
= (ZipEntry)e.nextElement();
            System.out.println(
"File: "+ ze2);
            
// and extract the data as before
        }        
        
    }
}
  • For each file to add to the archive, you must call putNextEntry( ) and pass it a ZipEntry object.
  • The ZipEntry object contains an extensive interface that allows you to get and set all the data available on that particular entry in your Zip file: name, compressed and uncompressed sizes, date, CRC checksum, extra field data, comment, compression method, and whether it’s a directory entry.
  • CheckedInputStream and CheckedOutputStream support both Adler32 and CRC32 checksums, the ZipEntry class supports only an interface for CRC
  • To extract files, ZipInputStream has a getNextEntry( ) method that returns the next ZipEntry if there is one


JAR files are cross-platform, so you don’t need to worry about platform issues. You can also include audio and image files as well as class files.
JAR files are particularly helpful when you deal with the Internet.
jar [options] destination [manifest] inputfile(s)


  1. Here are some typical ways to invoke jar. The following command creates a JAR file called myJarFile.jar that contains all of the class files in the current directory, along with an automatically generated manifest file:
    jar cf myJarFile.jar *.class
  2. The next command is like the previous example, but it adds a user-created manifest file called myManifestFile.mf:
    jar cmf myJarFile.jar myManifestFile.mf *.class
  3. This produces a table of contents of the files in myJarFile.jar:
    jar tf myJarFile.jar
  4. This adds the "verbose" flag to give more detailed information about the files in myJarFile.jar:
    jar tvf myJarFile.jar
  5. Assuming audio, classes, and image are subdirectories, this combines all of the subdirectories into the file myApp.jar. The "verbose" flag is also included to give extra feedback while the jar program is working:
    jar cvf myApp.jar audio classes image
a JAR file created on one platform will be transparently readable by the jar tool on any other platform (a problem that sometimes plagues Zip utilities).












]]>
I/O Performancehttp://www.tkk7.com/yanchengxiaotubao/articles/390969.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Wed, 07 Nov 2012 08:00:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390969.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390969.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390969.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390969.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390969.htmlmapped file access tends to be dramatically faster, this program does a  simple performance comparison:
package think.in.java.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

public class MappedIO {
    
private static int numOfInts = 4000000;
    
private static int numOfUbuffInts = 200000;
    
    
private abstract static class Tester{
        
private String name;
        
public Tester(String name){
            
this.name = name;
        }
        
        
public void runTest(){
            System.out.print(name 
+ "");
            
try {
                
long start = System.nanoTime();
                test();
                
double duration = System.nanoTime() - start;
                System.out.format(
"%.2f\n", duration/1.0e9);
            } 
catch (IOException e) {
                
throw new RuntimeException(e);
            }
        }
        
        
public abstract void test() throws IOException;
    }
    
    
private static Tester[] tests = {
        
new Tester("Stream Write") {
            @Override
            
public void test() throws IOException {
                DataOutputStream dos 
= new DataOutputStream(
                        
new BufferedOutputStream(
                                
new FileOutputStream(new File("temp.tmp"))));
                
for(int i=0; i<numOfInts; i++){
                    dos.writeInt(i);
                }
                dos.close();
            }
        },
        
new Tester("Mapped Write") {
            @Override
            
public void test() throws IOException {
                FileChannel fc 
= new RandomAccessFile("temp.tmp""rw").getChannel();
                IntBuffer ib 
= fc.map(
                        FileChannel.MapMode.READ_WRITE, 
0, fc.size()).asIntBuffer();
                
for(int i=0; i<numOfInts; i++){
                    ib.put(i);
                }
                fc.close();
            }
        },
        
new Tester("Stream Read"){
            @Override
            
public void test() throws IOException {
                DataInputStream dis 
= new DataInputStream(
                        
new BufferedInputStream(
                        
new FileInputStream(new File("temp.tmp"))));
                
for(int i=0; i<numOfInts; i++){
                    dis.readInt();
                }
                dis.close();
            }
        },
        
new Tester("Mapped Read") {
            @Override
            
public void test() throws IOException {
                FileChannel fc 
= new FileInputStream(new File("temp.tmp")).getChannel();
                IntBuffer ib 
= fc.map(
                        FileChannel.MapMode.READ_ONLY, 
0, fc.size()).asIntBuffer();
                
while(ib.hasRemaining()){
                    ib.get();
                }
                fc.close();
            }
        }
    };

    
/**
     * 
@param args
     
*/
    
public static void main(String[] args) {
        
for(Tester test : tests){
            test.runTest();
        }
    }

}

// Output:
Stream Write: 0.73
Mapped Write: 
0.06
Stream Read: 
0.70
Mapped Read: 
0.06


]]>
I/O Memory-mapped fileshttp://www.tkk7.com/yanchengxiaotubao/articles/390959.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Wed, 07 Nov 2012 06:59:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390959.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390959.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390959.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390959.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390959.htmlWith a memory-mapped file, you can pretend that the entire file is in memory and that you can access it by simply treating it as a very large array.
/**
 * Creating a very large file using mapping
 * 
@author WPeng
 *
 * 
@since 2012-11-7
 
*/
public class LargeMappedFiles {
    
static int length = 0x8ffffff//128M
    public static void main(String[] args) throws FileNotFoundException, IOException {
        MappedByteBuffer out 
= new RandomAccessFile("test.dat""rw").getChannel().map(
                FileChannel.MapMode.READ_WRITE, 
0, length);
        
        
for(int i=0; i< length; i++){
            out.put((
byte)'x');
        }
        System.out.println(
"Finished Writing");
        
        
for(int i=length/2; i<length/2 + 6; i++){
            System.out.println((
char)out.get(i));
        }
    }

}

To do both writing and reading, we start with a RandomAccessFile, get a channel for that file.
And then call map() to produce a MppedByteBuffer, which is a particular kind of direct buffer.
Note that you must specify the starting point and the length of  the region that you want to map in the file;
this means that you have the option to map samller regions of a large file.

The file createed with the preceding program is 128M long.
which is probably larger than your OS will allow in memory at one time.
the file appears to be accessible all at once because only portions of it are brought into memory.
and other parts are swapped out.
this way a very large file(up to 2 GB) can easily be modified.
note that the file-mapping facilities of the underlying operating system are used to maximize performance.





]]>
I/O View ByteBuffer - Primitive Typeshttp://www.tkk7.com/yanchengxiaotubao/articles/390925.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Wed, 07 Nov 2012 02:46:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390925.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390925.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390925.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390925.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390925.html闃呰鍏ㄦ枃

]]>
New I/Ohttp://www.tkk7.com/yanchengxiaotubao/articles/390874.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Tue, 06 Nov 2012 06:42:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390874.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390874.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390874.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390874.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390874.htmlChannel & ByteBuffer
 channal & buffer - ByteBuffer
package think.in.java.io;
/**
 * That are closer to the operating system’s way of performing I/O: channels and buffers.
 * The only kind of buffer that communicates directly with a channel is a ByteBuffer—that is, 
 * a buffer that holds raw bytes.
 * 
 * It’s fairly low-level, precisely because this makes a more efficient mapping with most operating systems.
 * 
 * Here’s a simple example that exercises all three types of stream 
 * to produce channels that are writeable, read/writeable, and readable:
 * 
@author WPeng
 *
 * 
@since 2012-11-6
 
*/
public class GetChannel {

    
private static final int BSIZE = 1024;

    
public static void main(String[] args) throws Exception {
        
// Write a file
        FileChannel fc = new FileOutputStream("data.txt").getChannel();
        fc.write(ByteBuffer.wrap(
"Some text".getBytes()));
        fc.close();
        
        
// Add to the end of the file
        fc = new RandomAccessFile("data.txt""rw").getChannel();
        fc.position(fc.size());  
//move to the end
        fc.write(ByteBuffer.wrap("Some more".getBytes()));
        fc.close();
        
        
// Read the file
        fc = new FileInputStream("data.txt").getChannel();
        ByteBuffer buff 
= ByteBuffer.allocate(BSIZE);
        
/**
            Reads a sequence of bytes from this channel into the given buffer.
            Bytes are read starting at this channel's current file position,
            and then the file position is updated with the number of bytes actually read.

            -1 if the channel has reached end-of-stream
        
*/
        fc.read(buff);
        buff.flip();
        
while(buff.hasRemaining()){
            System.out.print((
char)buff.get());
        }
    }

}
transferTo & transferFrom
package think.in.java.io;
/**
 * Using transferTo() between channels 
 * {Args: TransferTo.java TransferTo.txt}
 * 
@author WPeng
 *
 * 
@since 2012-11-6
 
*/
public class TransferTo {
    
public static void main(String[] args) throws IOException{
        
if(args.length != 2){
            System.out.println(
"arguments: sourcefile destfile");
            System.exit(
1);
        }
        FileChannel in 
= new FileInputStream(args[0]).getChannel(),
                    out 
= new FileOutputStream(args[1]).getChannel();
        in.transferTo(
0, in.size(), out);
        
// or:
        
// out.transferFrom(in, 0, in.size());
    }
}











]]>
mvn + spring [HelloWorld] http://www.tkk7.com/yanchengxiaotubao/articles/390836.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Mon, 05 Nov 2012 12:22:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390836.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390836.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390836.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390836.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390836.htmlThis quick guide example will use Maven to generate a simple Java project structure, and demonstrates how to retrieve Spring bean and print a “hello world” string.
Technologies used in this article :
  1. Spring 2.5.6
  2. Maven 2.2.1
  3. Eclipse 3.6
  4. JDK 1.6.0.13

Generate project structure with maven

In command prompt, issue following Maven command :
mvn archetype:create -DgroupId=wei.peng.app -DartifactId=docgenDemo

 

 

Maven will generated all the Java’s standard folders structure for you (beside resources folder, you need to create it manually)

Covert to Eclipse project
Type “mvn eclipse:eclipse” to convert the newly generated Maven style project to Eclipse’s style project.
mvn eclipse:eclipse
Later, import the converted project into Eclipse IDE.
Create a resources folder
Create a resources “/src/main/resources” folder, the Spring’s bean xml configuration file will put here later. Maven will treat all files under this “resources” folder as resources files, and copy it to output classes automatically.

Add spring dependency
Add Spring dependency in Maven’s pom.xml file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
<modelVersion>4.0.0</modelVersion>

    
<groupId>wei.peng.app</groupId>
    
<artifactId>docgen-demo</artifactId>
    
<version>1.0-SNAPSHOT</version>
    
<packaging>jar</packaging>

    
<name>docgen-demo</name>
    
<url>http://maven.apache.org</url>

    
<properties>
        
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
</properties>

    
<dependencies>
        
<dependency>
            
<groupId>junit</groupId>
            
<artifactId>junit</artifactId>
            
<version>3.8.1</version>
            
<scope>test</scope>
        
</dependency>
        
<!-- Spring framework -->
        
<dependency>
            
<groupId>org.springframework</groupId>
            
<artifactId>spring</artifactId>
            
<version>2.5.6</version>
        
</dependency>
    
</dependencies>
</project>
Issue “mvn eclipse:eclipse” again, Maven will download the Spring dependency libraries automatically and put it into your Maven’s local repository. At the same time, Maven will add the downloaded libraries into Eclipse “.classpath” for dependency purpose.

Spring Bean
Create a normal Java class (HelloWorld.java) at “src/main/java/com/mkyong/common/HelloWorld.java”. Spring’s bean is just a normal Java class, and declare in Spring bean configuration file later.
package wei.peng.app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
@author Administrator
 * Hello world! (Maven + Spring)
 
*/
public class HelloWorld {

    
private String name;
    
    
public void setName(String name) {
        
this.name = name;
    }

    
public void printHello() {
        System.out.println(
"Hello " + name);
    }
    
    
public static void main(String[] args){
        ApplicationContext context 
= new ClassPathXmlApplicationContext("docgen-transformations-beans.xml");
        HelloWorld obj 
= (HelloWorld) context.getBean("helloBean");
        obj.printHello();
    }
}

Spring bean configuration file
Create a xml file (Spring-Module.xml) at “src/main/resources/docgen-transformations-beans.xml“. This is the Spring’s bean configuration files, which declared all the available Spring beans.
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>
 
    
<bean id="helloBean" class="wei.peng.app.HelloWorld">
        
<property name="name" value="Wpeng" />
    
</bean>
 
</beans>

Run it

 



]]>
InputStream & OutputStreamhttp://www.tkk7.com/yanchengxiaotubao/articles/390779.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Mon, 05 Nov 2012 02:09:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390779.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390779.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390779.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390779.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390779.htmlpackage think.in.java.io;import java.io.IOException;import java.io.StringReader;/**&nbs...  闃呰鍏ㄦ枃

]]>
JDK 璁捐妯″紡 Patternhttp://www.tkk7.com/yanchengxiaotubao/articles/390771.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Mon, 05 Nov 2012 01:26:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390771.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390771.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390771.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390771.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390771.html1. 絳栫暐妯″紡  - strategy pattern
/**
    Strategy Pattern 浣跨敤鍙傛暟args[]錛坮egular expression 姝e垯琛ㄨ揪寮忥級鍐沖畾鎵瑕佽鍙栫殑鏂囦歡綾誨瀷銆?nbsp;鑰屼笉鏄湪code涓洿鎺ard code銆?br />*/
public class DirList3 {
    
public static void main(final String[] args) {
        File path 
= new File("D:\\Users\\wpeng\\workspace\\Hello\\src\\think\\in\\java\\io");
        String[] list;
        
if(args.length == 0){
            list 
= path.list();
        }
else{
            list 
= path.list(new FilenameFilter() {
                
private Pattern pattern = Pattern.compile(args[0]);
                @Override
                
public boolean accept(File dir, String name) {
                    
return pattern.matcher(name).matches();
                }
            });
        }
        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
        
for(String dirItem : list){
            System.out.println(dirItem);
        }
    }
}

2. 瑁呴グ鑰呮ā寮?- Decorator Pattern
/**
 * 瑁呴グ鑰呮ā寮?nbsp;decorator pattern
 * FileInputStearm -> FileRader -> BufferReader
 * 
@author WPeng
 * 
@since 2012-11-5
 
*/
public class BufferedInputFile {
    
// Throw exceptions to console
    public static String read(String filename) throws IOException{
        
// Reading input by lines
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String s;
        StringBuilder sb 
= new StringBuilder();
        
while((s = in.readLine()) != null){
            sb.append(s 
+ "\n");
        }
        in.close();
        
return sb.toString();
    }
    
public static void main(String[] args) throws IOException {
        System.out.print(read(
"BufferedInputFile.java"));

    // FileInputStream -> BufferedInputStream -> DataInputStream
    DataInputStream in = 
            
new DataInputStream(
                    
new BufferedInputStream(
                            
new FileInputStream("FormattedMemoryInput.java")));
        
while(true){
            System.out.print((
char)in.readByte());
        }
    }
}
3. 妯℃澘妯″紡 Template pattern
public class MappedIO {
    
private static int numOfInts = 4000000;
    
private static int numOfUbuffInts = 200000;
    
    
private abstract static class Tester{
        
private String name;
        
public Tester(String name){
            
this.name = name;
        }
        
        
public void runTest(){
            System.out.print(name 
+ "");
            
try {
                
long start = System.nanoTime();
                test();
                
double duration = System.nanoTime() - start;
                System.out.format(
"%.2f\n", duration/1.0e9);
            } 
catch (IOException e) {
                
throw new RuntimeException(e);
            }
        }
        
        
public abstract void test() throws IOException;
    }
    
    
private static Tester[] tests = {
        
new Tester("Stream Write") {
            @Override
            
public void test() throws IOException {
                DataOutputStream dos 
= new DataOutputStream(
                        
new BufferedOutputStream(
                                
new FileOutputStream(new File("temp.tmp"))));
                
for(int i=0; i<numOfInts; i++){
                    dos.writeInt(i);
                }
                dos.close();
            }
        },
        
new Tester("Mapped Write") {
            @Override
            
public void test() throws IOException {
                FileChannel fc 
= new RandomAccessFile("temp.tmp""rw").getChannel();
                IntBuffer ib 
= fc.map(
                        FileChannel.MapMode.READ_WRITE, 
0, fc.size()).asIntBuffer();
                
for(int i=0; i<numOfInts; i++){
                    ib.put(i);
                }
                fc.close();
            }
        }

    };

    
public static void main(String[] args) {
        
for(Tester test : tests){
            test.runTest();
        }
    }
}













]]>
Directory untilitieshttp://www.tkk7.com/yanchengxiaotubao/articles/390678.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Fri, 02 Nov 2012 06:19:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390678.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390678.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390678.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390678.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390678.htmlIt is useful to have a tool that will produce the set of files for you.
package think.in.java.io;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Produce a seqence of File objects that match a regular expression in either a local directory,
 * or by walking a directory tree.
 * 
@author WPeng
 *
 * 
@since 2012-11-2
 
*/
public final class Directory {
    
    
public static File[] local(File dir, final String regex){
        
/** Returns an array of abstract pathnames denoting the files and directories in the directory 
         * denoted by this abstract pathname that satisfy the specified filter. 
*/
        
return dir.listFiles(new FilenameFilter() {
            
private Pattern pattern = Pattern.compile(regex);
            @Override
            
public boolean accept(File dir, String name) {
                
return pattern.matcher(new File(name).getName()).matches();
            }
        });
    }
    
    
public static File[] local(String path, final String regex){
        
//Overloaded
        return local(new File(path), regex);
    }
    
    
// A two-tuple for returning a pair of objects
    public static class TreeInfo implements Iterable<File>{
        
public List<File> files = new ArrayList<File>();
        
public List<File> dirs = new ArrayList<File>();
        
// The default iterable element is the file list
        public Iterator<File> iterator(){
            
return files.iterator();
        }
        
void addAll(TreeInfo other){
            files.addAll(other.files);
            dirs.addAll(other.dirs);
        }
        
public String toString(){
            
return "dirs: " + PPrint.pformat(dirs) + "\n\nfiles: " + PPrint.pformat(files);
        }
    }
    
    
public static TreeInfo walk(String start, String regex){
        
// Begin recurion
        return recurseDirs(new File(start), regex);
    }
    
public static TreeInfo walk(File start, String regex){
        
// Overload
        return recurseDirs(start, regex);
    }
    
public static TreeInfo walk(File start){
        
// Everything
        return recurseDirs(start, ".*");
    }
    
public static TreeInfo walk(String start){
        
// Overload
        return recurseDirs(new File(start), ".*");
    }
    
static TreeInfo recurseDirs(File startDir, String regex){
        TreeInfo result 
= new TreeInfo();
        
for(File item : startDir.listFiles()){
            
if (item.isDirectory()) {
                result.dirs.add(item);
                result.addAll(recurseDirs(item, regex));
            }
else// Regular file
                if (item.getName().matches(regex)) {
                    result.files.add(item);
                }
            }
        }
        
return result;
    }
    
    
// Simple validation test
    public static void main(String[] args){
        
if(args.length == 0){
            System.out.println(walk(
"D:\\Users\\wpeng\\workspace\\Hello\\src\\think\\in\\java\\io"));
        }
else{
            
for(String arg : args){
                System.out.println(arg);
            }
        }
    }
    
    
}

package think.in.java.io;

import java.util.Arrays;
import java.util.Collection;

/**
 * Pretty-printer for collections
 * 
@author WPeng
 *
 * 
@since 2012-11-2
 
*/

public class PPrint {
    
public static String pformat(Collection<?> c) {
        
if(c.size() == 0){
            
return "[]";
        }
        StringBuilder result 
= new StringBuilder("[");
        
for(Object elem : c){
            
if(c.size() != 1){
                result.append(
"\n ");
            }
            result.append(elem);
        }
        
if(c.size() != 1){
            result.append(
"\n");
        }
        result.append(
"]");
        
return result.toString();
    }
    
    
public static void pprint(Collection<?> c){
        System.out.println(pformat(c));
    }
    
    
public static void pprint(Object[] c){
        System.out.println(pformat(Arrays.asList(c)));
    }
}

/**
dirs: [
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\1
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\2
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\3
]

files: [
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\1.txt
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\2.txt
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\3.txt
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\Directory.java
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList.java
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList2.java
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList3.java
 D:\Users\wpeng\workspace\Hello\src\think\in\java\io\PPrint.java
]
*/



]]>
File & FilenameFilterhttp://www.tkk7.com/yanchengxiaotubao/articles/390659.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Fri, 02 Nov 2012 01:57:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390659.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390659.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390659.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390659.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390659.htmlpackage think.in.java.io;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

/** 
 * Display a directory listing using regular expressions.
 * {Args: "D.*\.java"}
 * 
@author WPeng
 *
 * 
@since 2012-11-2
 
*/

public class DirList {
    
public static void main(String[] args) {
        File path 
= new File("D:\\Users\\wpeng\\workspace\\Hello\\src\\think\\in\\java\\io");
        String[] list;
        
if(args.length == 0){
            list 
= path.list();
        }
else{
            list 
= path.list(new DirFilter(args[0]));
        }
        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
        
for(String dirItem : list){
            System.out.println(dirItem);
        }
    }
}
 
class DirFilter implements FilenameFilter{
     
private Pattern pattern;
     
public DirFilter(String regex){
         pattern 
= Pattern.compile(regex);
     }
    @Override
    
public boolean accept(File dir, String name) {
        
return pattern.matcher(name).matches();
    }
     
 }

package think.in.java.io;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * Uses anonymous inner classes.
 * {Args: "D.*\.java"}
 * 
@author WPeng
 *
 * 
@since 2012-11-2
 
*/
public class DirList2 {
    
public static FilenameFilter filter(final String regex){
        
// Creation of anonymous inner class
        return new FilenameFilter() {
            
private Pattern pattern = Pattern.compile(regex);
            @Override
            
public boolean accept(File dir, String name) {
                
return pattern.matcher(name).matches();
            }
        }; 
// End of anonymous inner class
    }

    
public static void main(String[] args) {
        File path 
= new File("D:\\Users\\wpeng\\workspace\\Hello\\src\\think\\in\\java\\io");
        String[] list;
        
if(args.length == 0){
            list 
= path.list();
        }
else{
            list 
= path.list(filter(args[0]));
        }
        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
        
for(String dirItem : list){
            System.out.println(dirItem);
        }
    }

}

package think.in.java.io;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * Building the anonymous inner class "in-place".
 * {Args: "D.*\.java"}
 * 
@author WPeng
 *
 * 
@since 2012-11-2
 
*/

public class DirList3 {

    
public static void main(final String[] args) {
        File path 
= new File("D:\\Users\\wpeng\\workspace\\Hello\\src\\think\\in\\java\\io");
        String[] list;
        
if(args.length == 0){
            list 
= path.list();
        }
else{
            list 
= path.list(new FilenameFilter() {
                
private Pattern pattern = Pattern.compile(args[0]);
                @Override
                
public boolean accept(File dir, String name) {
                    
return pattern.matcher(name).matches();
                }
            });
        }
        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
        
for(String dirItem : list){
            System.out.println(dirItem);
        }
    }

}

/** Output:
 * DirList.java
 * DirList2.java
 * DirList3.java
 
*/



]]>
BigInteger & BigDicemalhttp://www.tkk7.com/yanchengxiaotubao/articles/390654.html鐩愬煄灝忓湡鍖?/dc:creator>鐩愬煄灝忓湡鍖?/author>Fri, 02 Nov 2012 01:14:00 GMThttp://www.tkk7.com/yanchengxiaotubao/articles/390654.htmlhttp://www.tkk7.com/yanchengxiaotubao/comments/390654.htmlhttp://www.tkk7.com/yanchengxiaotubao/articles/390654.html#Feedback0http://www.tkk7.com/yanchengxiaotubao/comments/commentRss/390654.htmlhttp://www.tkk7.com/yanchengxiaotubao/services/trackbacks/390654.htmlimport java.math.BigInteger;

public class HelloWorld {

    
public static void main(String[] args) {
        BigInteger bigI1 
= BigInteger.ONE;
        BigInteger bigI2 
= BigInteger.ONE;
        
        
// The operator + is undefined for the argument type(s) java.math.BigInteger
        
// BigInteger bigI3 = bigI1 + bigI2;
        BigInteger bigI4 = bigI1.add(bigI2);

        System.out.println(bigI1 
+ " " + bigI2 + " " + bigI4);
    }

}

// Expect Result: 1 1 2

]]>
主站蜘蛛池模板: 免费大片在线观看网站| 一个人免费观看视频在线中文| 亚洲国产一级在线观看 | 亚洲精品第一国产综合亚AV| 99久9在线|免费| 日韩电影免费观看| 性无码免费一区二区三区在线| 日韩精品在线免费观看| 久9热免费精品视频在线观看| 四虎影视成人永久免费观看视频 | MM1313亚洲国产精品| 国产精品亚洲色婷婷99久久精品| 亚洲aⅴ无码专区在线观看春色| 国产精品亚洲一区二区三区在线观看 | 日本亚洲高清乱码中文在线观看| 校园亚洲春色另类小说合集| 污污污视频在线免费观看| 一级做受视频免费是看美女 | 国产精品亚洲专区在线观看| 亚洲熟妇少妇任你躁在线观看| 亚洲国产无线乱码在线观看| 亚洲AV无码一区二区乱子仑| 边摸边脱吃奶边高潮视频免费| av网站免费线看| 日本不卡免费新一区二区三区| 午夜免费1000部| 国产精品极品美女免费观看| 亚洲国产精品人人做人人爽| 亚洲av无码专区在线播放| 亚洲精品视频在线观看免费| 在线A亚洲老鸭窝天堂| 久久精品国产亚洲夜色AV网站| 亚洲美免无码中文字幕在线| 亚洲国产熟亚洲女视频| 男男gay做爽爽免费视频| 中文字幕无码免费久久9一区9| 国产亚洲人成在线影院| 中文在线日本免费永久18近| ww4545四虎永久免费地址| 日韩免费电影在线观看| 亚洲无人区午夜福利码高清完整版|