import java.io.*;
public class test
{
public static void main(String[] args)
{
try
{
File dir = new File("D:/教学/programming/c");
String[] fs = dir.list();
for (int i = 0; i < fs.length; i++)
{
if (fs[i].indexOf(".") != -1)
{
System.out.println(fs[i]);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
具体功能是统计出某个目录下的jsp文件名,画面名,画面ID,tag中的参数,以及tag所在的行数等信息.
代码如下:
用来存储一个文件信息的类:AimData.java
package io;
import java.util.List;
public class AimData {
private String htmlName = null;
private String htmlTitle = null;
private String fileName = null;
private List komokuList = null;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getHtmlName() {
return htmlName;
}
public void setHtmlName(String htmlName) {
this.htmlName = htmlName;
}
public String getHtmlTitle() {
return htmlTitle;
}
public void setHtmlTitle(String htmlTitle) {
this.htmlTitle = htmlTitle;
}
public List getKomokuList() {
return komokuList;
}
public void setKomokuList(List komokuList) {
this.komokuList = komokuList;
}
}
用来存储一个文件中一个tag的信息类:KomokuData.java
package io;
public class KomokuData {
private String komokuClass = null;
private int pos = 0;
public String getKomokuClass() {
return komokuClass;
}
public void setKomokuClass(String komokuClass) {
this.komokuClass = komokuClass;
}
public int getPos() {
return pos;
}
public void setPos(int pos) {
this.pos = pos;
}
}
具体实现代码:
package io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
public class ReadTextToCsv {
private List fileList = new ArrayList();
/**
* @param args
*/
public static void main(String[] args) {
ReadTextToCsv rd = new ReadTextToCsv();
File dir = new File("D:\\jsp\\");
// File dir = new File("D:\\aa\\");
try {
rd.readFile(dir);
rd.write();
} catch (Exception e) {
e.printStackTrace();
}
}
public void readFile(File subdir) throws IOException {
String[] fileNames = null;
String path = "";
if (subdir.isDirectory()) {
fileNames = subdir.list();
path = subdir.getPath() + "\\";
if (path.toUpperCase().indexOf("CVS") > 0) {
return;
}
// System.out.println(path);
for (int i = 0; i < fileNames.length; i++) {
this.readFile(new File(path + fileNames[i]));
}
} else {
// System.out.println(subdir.getName());
this.readContent(subdir.getPath());
return;
}
}
/**
*
* @param filename
* @throws IOException
*/
public void readContent(String filename) throws IOException {
String htmlName = null;
String htmlTitle = null;
AimData ad = new AimData();
KomokuData kd = new KomokuData();
List komokuList = new ArrayList();
int position = 0;
FileReader fr = new FileReader(filename);// Create
BufferedReader br = new BufferedReader(fr);// Create
String line = br.readLine();
while (line != null) {
position++;
// System.out.println(line + "<br>");
if (null == htmlName) {
htmlName = this.getHtmlName(line);
if (htmlName != null) {
// System.out.println("htmlName:" + htmlName);
ad.setHtmlName(htmlName);
}
}
if (null == htmlTitle) {
htmlTitle = this.getHtmlTitle(line);
if (htmlTitle != null) {
// System.out.println("htmlTitle:" + htmlTitle);
ad.setHtmlTitle(htmlTitle);
}
}
String komokuName = this.getKomokuName(line);
if (null != komokuName) {
kd = new KomokuData();
kd.setKomokuClass(komokuName);
kd.setPos(position);
komokuList.add(kd);
// System.out.println("komokuName:" + komokuName);
ad.setKomokuList(komokuList);
}
line = br.readLine();
}
if (komokuList.size() > 0) {
ad.setFileName(filename);
fileList.add(ad);
// System.out.println(filename);
}
br.close();// BufferedReader対象をクロスする
fr.close();// ファイルをクロスする
}
/**
*
* @param content
* @return
*/
public String getHtmlName(String content) {
String htmlName = "";
String tag = "title1";
String tag1 = ">";
String tag2 = "</td>";
int pos = 0;
int startpos = 0;
int endpos = 0;
pos = content.indexOf(tag);
if (pos < 0) {
return null;
} else {
startpos = content.indexOf(tag1, pos);
endpos = content.toLowerCase().indexOf(tag2, pos);
if (startpos < 0 || endpos < 0) {
return null;
}
}
htmlName = content.substring(startpos + tag1.length(), endpos);
return htmlName;
}
public String getHtmlTitle(String content) {
String htmlTitle = "";
String tag = "title_gamenid";
String tag1 = ">";
String tag2 = "</td>";
int pos = 0;
int startpos = 0;
int endpos = 0;
pos = content.indexOf(tag);
if (pos < 0) {
return null;
} else {
startpos = content.indexOf(tag1, pos);
endpos = content.toLowerCase().indexOf(tag2, pos);
if (startpos < 0 || endpos < 0) {
return null;
}
}
htmlTitle = content.substring(startpos + tag1.length(), endpos);
return htmlTitle;
}
/**
*
* @param content
* @return
*/
public String getKomokuName(String content) {
String komokuName = "";
String tag = "className=\"";
int pos = 0;
int endpos = 0;
pos = content.indexOf(tag);
if (pos < 0) {
return null;
}
endpos = content.indexOf("\"", pos + tag.length());
komokuName = content.substring(pos + tag.length(), endpos);
return komokuName;
}
/**
*
* @param content
* @return
*/
public boolean isAim(String content) {
boolean flag = false;
String tag = "<lapis:linkSearchTreeDialog";
int pos = 0;
pos = content.indexOf(tag);
if (pos > 0) {
flag = true;
}
return flag;
}
public void write() {
AimData ad = null;
KomokuData kd = null;
List komokuList = null;
int bango = 0;
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object
try {
// connected to "myfile.txt"
out = new FileOutputStream("myfile.csv");
// Connect print stream to the output stream
p = new PrintStream(out);
p.println("画面号");
for (int i = 0; i < this.fileList.size(); i++) {
ad = (AimData) fileList.get(i);
komokuList = (List) ad.getKomokuList();
for (int j = 0; j < komokuList.size(); j++) {
bango++;
kd = (KomokuData) komokuList.get(j);
p.println(bango + "," + ad.getHtmlName() + ","
+ ad.getHtmlTitle() + ","
+ this.getWebPath(ad.getFileName()) + ","
+ kd.getPos() + "," + kd.getKomokuClass());
}
}
p.println("总数:" + this.fileList.size());
p.close();
} catch (Exception e) {
System.err.println("Error writing to file");
}
}
public String getWebPath(String path) {
String webpath = "";
String basepath = "D:\\workspace\\";
int pos = 0;
pos = path.indexOf(basepath);
webpath = path.substring(pos + basepath.length());
webpath = webpath.replace('\\', '/');
return webpath;
}
}
}