如何将JSP中的字段与文本文件分开?
我有一个perl脚本,它在指定的文件夹中搜索最新修改的文件。搜索结束时,此脚本将文本文件中的数据放入结构中:如何将JSP中的字段与文本文件分开?,jsp,text,field,Jsp,Text,Field,我有一个perl脚本,它在指定的文件夹中搜索最新修改的文件。搜索结束时,此脚本将文本文件中的数据放入结构中: [COMP] = folder1 [DATE] = 2015.06.01 08:12 [COMP] = folder1 [DATE] = 2015.06.01 05:27 [COMP] = folder1 [DATE] = 2015.05.31 11:44:44 …等等 对于显示,我必须使用jsp,因为服务器运行Tomcat 这是我的index.js
[COMP] = folder1
[DATE] = 2015.06.01 08:12
[COMP] = folder1
[DATE] = 2015.06.01 05:27
[COMP] = folder1
[DATE] = 2015.05.31 11:44:44
…等等
对于显示,我必须使用jsp,因为服务器运行Tomcat
这是我的index.jsp:
<%@ page import = "java.io.*, java.util.*" %>
<%
ServletContext context = getServletContext();
String file = (context.getRealPath("/list.txt"));
String s, list = new String();
try {
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
while((s = in.readLine())!= null) {
list += s + "\n";
}
in.close();
}
catch(Exception e) {System.out.println(e);}
%>
<pre>
<%=list%>
</pre>
现在,list变量包含COMP和DATE字段数据
所以,我的问题是,如何将JSP中的COPM、DATE字段与文本文件分开
谢谢你的回答 文件中的数据存储为键和值对。 在JSP中需要做的是,您需要使用java.util.properties类作为属性文件来读取文件,而不是以steam的形式读取文件 下面的代码应该可以工作
<%@ page import = "java.io.*, java.util.*" %>
<%
ServletContext context = getServletContext();
String file = (context.getRealPath("/list.txt"));
String s = new String();
List<String> compList = new ArrayList<String>();
List<String> dateList = new ArrayList<String>();
int i = 0;
try {
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
while((s = in.readLine())!= null) {
if(i % 2 = 0) {
compList.add(s);
} else {
dateList.add(s);
}
i++;
}
in.close();
}
catch(Exception e) {System.out.println(e);}
%>
<pre>
<%=compList%>
<%=dateList%>
</pre>
试试看。使用索引跟踪行号。如果是偶数,则添加到薪酬列表,否则添加到日期列表
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
String comp, sdate;
while(True) {
comp = in.readLine(),
if (comp == null) { break;}
sdate= in.readLine(),
if (sdate== null) { break;}
// you have comp and sdate : use them and put result in list
}
in.close();
它们位于文本文件的不同行上。您只需分别阅读:
您可以使用java.util.regex包来解析每一行。但是,如果你真的想有一个漂亮的演示,你最好使用servlet来解析输入文本文件,将元素作为列表放入请求属性中,转发到jsp文件并使用jsp来格式化结果Java代码很难测试和调试,不要在其中加入智能。谢谢你的回答!这是我的解决方案,不是全部,wokring代码:
这行不通。您将只获得最后两行。试试看。
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
String comp, sdate;
while(True) {
comp = in.readLine(),
if (comp == null) { break;}
sdate= in.readLine(),
if (sdate== null) { break;}
// you have comp and sdate : use them and put result in list
}
in.close();
my %hash = ();
loop start // folder read etc
some if statement
if ($before_on_week le $d)
{
$row = $row . " <td>$d</td>\n";
}
else
{
$row = $row . " <td><b><font color=red>$d</font></b></td>\n";
}
$row = $row . " <td>/var/www/$path</td>\n";
$row = $row . "</tr>\n";
#print FILE $row;
$hash{"$d $path"} = $row;
loop end
foreach my $key (sort keys %hash) {
my $row = $hash{$key};
print FILE $row;
}