//Displays directory listing using regular expressions, regular Expressions by DirFilter class.
//Implements the Interface FilenameFilter.
public interface FilenameFilter{
public boolean accept(File dir, String name);
}
list() return String[]
boolean b=Pattern.matches(regex, sequense chars) equals
{
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(sequense char);
boolean b=m.matchers();
}
Compression class | Function |
CheckedInputStream |
GetCheckSum() produces checksum for any InputStream
(not just decompression).
|
CheckedOutputStream |
GetCheckSum() produces checksum for any OutputStream
(not just decompression).
|
DeflaterOutputStream | Base class for compression classes. |
ZipOutputStream | A DeflaterOutputStream that compresses data into the ZIP file format. |
GZIPOutputStream | A 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.
|
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 < args.length; i++) {
System.out.println("Writing file " + args[i]);
BufferedReader in =
new BufferedReader(new FileReader(args[i]));
zos.putNextEntry(new ZipEntry(args[i]));
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());
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.out.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.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry)e.nextElement();
System.out.println( "File: " + ze2);
// ... and extract the data as before
}
c | Creates a new or empty archive. |
t | Lists the table of contents. |
x | Extracts all files. |
x file | Extracts the named file. |
f | Says: “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. |
m | Says that the first argument will be the name of the user-created manifest file. |
v | Generates verbose output describing what jar is doing. |
0 | Only store the files; doesn’t compress the files (use to create a JAR file that you can put in your classpath). |
M | Don’t automatically create a manifest file. |
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 ...
, Ralph the hamster[c12.Animal@1ddebc3], c12.House@14f8dab
, Fronk 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
, Fronk 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
, Fronk 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
]
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; }
}
[ 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 CADState.out
[class Circlecolor[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 Circlecolor[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 Circlecolor[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]
]
- 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.
Characters | |
B | The specific character B |
/xhh | Character with hex value 0xhh |
/uhhhh | The Unicode character with hex representation 0xhhhh |
/t | Tab |
/n | Newline |
/r | Carriage return |
/f | Form feed |
/e | Escape |
Character Classes | |
. | Represents any character |
[abc] | Any of the characters a, 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) |
/s | A whitespace character (space, tab, newline, formfeed, carriage return) |
/S | A non-whitespace character ([^/s]) |
/d | A numeric digit [0-9] |
/D | A non-digit [^0-9] |
/w | A word character [a-zA-Z_0-9] |
/W | A non-word character [^/w] |
Logical Operators | |
XY | X followed by Y |
X|Y | X or Y |
(X) | A capturing group. You can refer to the ith captured group later in the expression with /i |
Boundary Matchers | |
^ | Beginning of a line |
$ | End of a line |
/b | Word boundary |
/B | Non-word boundary |
/G | End of the previous match |
- 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 won't 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.
Greedy | Reluctant | Possessive | Matches |
X? | X?? | X?+ | X, one or none |
X* | X*? | X*+ | X, zero or more |
X+ | X+? | X++ | X, one or more |
X{n} | X{n}? | X{n}+ | X, exactly n times |
X{n,} | X{n,}? | X{n,}+ | X, at least n times |
X{n,m} | X{n,m}? | X{n,m}+ | X, at least n but not more than m times |
Compile Flag | Effect |
Pattern.CANON_EQ | Two 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 | 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 | 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 | In dotall mode, the expression ‘.’ matches any character, including a line terminator. By default, the ‘.’ expression does not match line terminators. |
Pattern.MULTILINE | 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 | 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 | In this mode, only the ‘/n’ line terminator is recognized in the behavior of ‘.’, ‘^’, and ‘$’. |
"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( " ")));
"[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.
"rug"
"bag"
"fix"
"rig"
"rag"
reset() without any arguments sets the Matcher to the beginning of the current sequence.