package jar.util;import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class fileUtil
{ public boolean RegEx(String regEx, String line)
{
Pattern pattern; Matcher matcher; pattern = Pattern.compile(regEx); matcher = pattern.matcher(line); return matcher.matches(); } public String getPackageContent(String regEx, String line)
{
Pattern pattern; Matcher matcher; pattern = Pattern.compile(regEx); matcher = pattern.matcher(line); matcher.matches(); return matcher.group(1); } public String getPkg(String fileName)
{
String pkg = null;
boolean ifIsInTheC = false;
try
{
FileReader in = new FileReader(fileName);
BufferedReader bf = new BufferedReader(in);
String line = null;
while ((line = bf.readLine()) != null)
{
if (line.equals(""))
{
continue;
}
if (RegEx("\\s*.*\\*+/", line))
{
ifIsInTheC = false;
continue;
}
if (ifIsInTheC)
{
continue;
}
if (RegEx("/\\**[^/]$", line))
{
ifIsInTheC = true;
continue;
}
if (RegEx("\\s*/\\**/", line))
{
continue;
} if (RegEx("\\s*package(.*)\\s*;\\s*", line))
{
pkg = getPackageContent("\\s*package(.*)\\s*;\\s*", line);
System.out.println("包结构是:" + pkg);
return pkg; } if (RegEx("\\s*class\\s+.*", line)
|| RegEx("\\s*.*\\s+class\\s+.*", line))
{
return pkg;
// System.out.println(line);
// do something
} // System.out.println(line);
} }
catch (Exception e)
{
// TODO: handle exception
}
return pkg;
} /**
*
*
*/
public static void main(String[] args)
{ fileUtil fff = new fileUtil(); String pkg = fff.getPkg("d:/MyPCTest.java"); System.out.println(pkg); }
}
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class fileUtil
{ public boolean RegEx(String regEx, String line)
{
Pattern pattern; Matcher matcher; pattern = Pattern.compile(regEx); matcher = pattern.matcher(line); return matcher.matches(); } public String getPackageContent(String regEx, String line)
{
Pattern pattern; Matcher matcher; pattern = Pattern.compile(regEx); matcher = pattern.matcher(line); matcher.matches(); return matcher.group(1); } public String getPkg(String fileName)
{
String pkg = null;
boolean ifIsInTheC = false;
try
{
FileReader in = new FileReader(fileName);
BufferedReader bf = new BufferedReader(in);
String line = null;
while ((line = bf.readLine()) != null)
{
if (line.equals(""))
{
continue;
}
if (RegEx("\\s*.*\\*+/", line))
{
ifIsInTheC = false;
continue;
}
if (ifIsInTheC)
{
continue;
}
if (RegEx("/\\**[^/]$", line))
{
ifIsInTheC = true;
continue;
}
if (RegEx("\\s*/\\**/", line))
{
continue;
} if (RegEx("\\s*package(.*)\\s*;\\s*", line))
{
pkg = getPackageContent("\\s*package(.*)\\s*;\\s*", line);
System.out.println("包结构是:" + pkg);
return pkg; } if (RegEx("\\s*class\\s+.*", line)
|| RegEx("\\s*.*\\s+class\\s+.*", line))
{
return pkg;
// System.out.println(line);
// do something
} // System.out.println(line);
} }
catch (Exception e)
{
// TODO: handle exception
}
return pkg;
} /**
*
*
*/
public static void main(String[] args)
{ fileUtil fff = new fileUtil(); String pkg = fff.getPkg("d:/MyPCTest.java"); System.out.println(pkg); }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货