前天在公司写了个比较简单的页面打印文件信息的类,减少了JSP页面的复杂的。可惜不能带回家,今天回家就想做个更好的,可惜啊,花了太多时间去整了,最后的总结还是——这个类被使用的可能性还是不多的,而且还有点问题,传个JSP隐式对象out过去调用他的println不知道怎么第一行老是有大堆空格,目前还没有看那个编译之后的*_JSP.java类里面怎么打印的。暂时寄放在此。这个类还是可以使用的,只是暂时不知道应该提供哪些功能给它。其实还是学到不少新的知识。关于IO,jsp,集合,数组方面都有更多的了解。希望大家提出宝贵意见,要是您是初学者的话,有什么疑问我也很乐意解答和讨论。
更好的做法,以后可以使用tag标签调用就好了。
package sc.sphenic.jsp.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
public class FileJSPWriter {
private final int ARRAY_GRANT = 10;
// public static void
main(String args[]) {
// String fileName = "c:/text.txt";
// try {
// FileJSPWriter.writeln(new JSPWriter(100,
true), fileName);
// FileJSPWriter out = new FileJSPWriter(new
JSPWriter(100, true),
// fileName);
//
// while (out.writeLine() != null) {
// System.out.println();
// System.out.println(out.getCurRow());
// }
// String[] lines = out.getLines();
// for (int i = 0; i < lines.length; i++)
{
// System.out.println(lines[i]);
// }
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (PageIOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
private String[] lines = new String[ARRAY_GRANT];
private int maxRow;
private JspWriter out;
private BufferedReader in;
private String curLine;
private int curRow;
private int nxtRow;
public
FileJSPWriter(JspWriter out, File f) throws FileNotFoundException
{
setIn(f);
setOut(out);
// TODO Auto-generated
constructor stub
}
void setOut(JspWriter out)
{
this.out = out;
}
boolean absolute(int row)
throws PageIOException, PageFileException {
try {
String temp =
lines[row - 1];
if (temp !=
null) {
curLine
= temp;
curRow
= row + 1;
nxtRow
= curRow + 1;
return
true;
} else
{
throw
new ArrayIndexOutOfBoundsException();
}
} catch
(java.lang.ArrayIndexOutOfBoundsException e) {
// try to
find in file
while
(readLine()) {
if
(!EOF()) {
if
(row == maxRow) {// line found
return
true;
}
}
else {
throw
new PageFileException("Can not found the line in:"
+
row + "\n The max lines is :" + maxRow);
}
}
return
false;
}
}
public boolean EOF() {
return curLine == null;
}
public FileJSPWriter(JspWriter out, String
filePath)
throws
FileNotFoundException {
this(out, new
File(filePath));
}
public static void writeln(JspWriter out, File
f)
throws
FileNotFoundException, PageIOException {
FileJSPWriter fOut = new
FileJSPWriter(out, f);
while (fOut.writeLine() !=
null) {
// do
nothing
}
}
public static void writeln(JspWriter out, String
filePath)
throws
FileNotFoundException, PageIOException {
writeln(out, new
File(filePath));
}
public String writeLine() throws PageIOException
{
if (getLine()) {
try {
out.println(curLine);
} catch
(IOException e) {
throw
new PageIOException(e);
}
}
return curLine;
}
public boolean
writeLine(File f, boolean bReplace) throws PageIOException,
FileNotFoundException
{
checkReader(f, bReplace);
return writeLine() !=
null;
}
private void
checkReader(File f, boolean replace)
throws
FileNotFoundException {
if (replace) {
setIn(f);
}
if (in != null) {
}
}
void setIn(File f) throws FileNotFoundException
{
in = new BufferedReader(new
FileReader(f));
}
private boolean getLine() throws PageIOException
{
try {
return
absolute(curRow + 1);
} catch (PageFileException e)
{// EOF
return
false;
}
}
private boolean readLine()
throws PageIOException {
//
文件到达最后readLine将返回null
try {
if ((curLine
= in.readLine()) == null) {
return
false;
} else
{
updateRow();
return
true;
}
} catch (IOException e) {
try {
in.close();
} catch
(IOException e1) {
throw
new PageIOException("Close reader error");
}
throw new
PageIOException(e);
}
}
public boolean getLine(int
absoluteRow) throws PageIOException,
PageFileException
{
if
(absoluteRow < curRow) {
absolute(absoluteRow);
return
true;
} else {
return
readLine();
}
}
private void updateRow()
{
if (lines.length == maxRow) {//
out of index
//
extends the array
extendLines();
}
lines[maxRow] = curLine;
curRow++;
maxRow++;
}
private void extendLines()
{
String[] src = lines;
lines = new String[src.length +
ARRAY_GRANT];
System.arraycopy(src, 0, lines,
0, src.length);
}
void resetAllRow() {
maxRow = 0;
curRow = 0;
nxtRow = curRow + 1;
}
public int getCurRow()
{
return curRow;
}
public String[] getLines()
{
return lines;
}
}