1.文件类
*创建File类对象
File f;
f = new File ("a.java");
f = new File ("C:\nie\","a.java");
*File类提供了实现目录管理功能的方法
File path = new File("C:\nie\");
File f = new File(path,"a.java");
2.列出所有文件
import java.io.*;
class ListAllFiles
{
public static void main(String[] args){
ListFiles( new File( "c:\workspace"));
}
class ListAllFiles
{
public static void main(String[] args){
ListFiles( new File( "c:\workspace"));
}
public static void ListFiles( File dir ){
if( !dir.exists() || ! dir.isDirectory() ) return;
String [] files = dir.list();
for( int i=0; i<files.length; i++){
File file = new File( dir, files[i] );
if( file.isFile() ){
System.out.println(
dir + "\" + file.getName() + " " + file.length() );
}else{
System.out.println(
dir + "\" + file.getName() + " <dir>" );
ListFiles( file ); //对于子目录,进行递归调用
}
}
}
}
if( !dir.exists() || ! dir.isDirectory() ) return;
String [] files = dir.list();
for( int i=0; i<files.length; i++){
File file = new File( dir, files[i] );
if( file.isFile() ){
System.out.println(
dir + "\" + file.getName() + " " + file.length() );
}else{
System.out.println(
dir + "\" + file.getName() + " <dir>" );
ListFiles( file ); //对于子目录,进行递归调用
}
}
}
}
3.Pattern类
import java.util.regex.*;
public class RegexSplitter {
public static void main(String[] args) throws Exception {
Pattern p = Pattern.compile( "[, \s]+");
String[] result =
p.split( "one,two, three four , five ");
for (int i=0; i<result.length; i++)
System.out.println(result[i]);
}
}
public class RegexSplitter {
public static void main(String[] args) throws Exception {
Pattern p = Pattern.compile( "[, \s]+");
String[] result =
p.split( "one,two, three four , five ");
for (int i=0; i<result.length; i++)
System.out.println(result[i]);
}
}
4.匹配
import java.util.regex.*;
public class RegexEmailValidate {
public static void main(String[] args)
throws Exception {
String pattern = "^[^@]+@[\w]+(\.[\w]+)*$";
String email = "dstang2000@263.net";
boolean ok = Pattern.matches( pattern, email );
System.out.println( ok );
}
}
public class RegexEmailValidate {
public static void main(String[] args)
throws Exception {
String pattern = "^[^@]+@[\w]+(\.[\w]+)*$";
String email = "dstang2000@263.net";
boolean ok = Pattern.matches( pattern, email );
System.out.println( ok );
}
}
5.Matcher类
mport java.util.regex.*;
public class RegexReplacement {
public static void main(String[] args)
throws Exception {
Pattern pattern = Pattern.compile("cat");
Matcher matcher = pattern.matcher(
"one cat, two cats in the yard");
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
matcher.appendReplacement(sb, "big $0");
}
matcher.appendTail(sb);
System.out.println(sb.toString());
}
}
public class RegexReplacement {
public static void main(String[] args)
throws Exception {
Pattern pattern = Pattern.compile("cat");
Matcher matcher = pattern.matcher(
"one cat, two cats in the yard");
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
matcher.appendReplacement(sb, "big $0");
}
matcher.appendTail(sb);
System.out.println(sb.toString());
}
}
明天学习内容:
组件