我正在尝试创建一个读取两个文本文件(dev.txt 和 BLTversion.txt)并让用户写入它们以更新其内容的 JavaServer Page。唯一的问题是用户尝试更新 BLTversion.txt 时,它会删除文件内容......所以我认为写作部分有问题?我将继续尝试得到这个,但只是想我会发布它,以防在我弄清楚之前有人能看到我哪里出错了。这是代码:
<%
ArrayList list = new ArrayList();
ArrayList list2 = new ArrayList();
String line1 = "";
String line2 = "";
boolean error = false;
String errorString = "";
String original = request.getParameter("original");
String code = request.getParameter("code");
String prod = request.getParameter("prod");
String versions = request.getParameter("versions");
String engineer = request.getParameter("engineer");
String assignee = request.getParameter("assignee");
line1 = prod + "," + code + "," + engineer.toUpperCase() + "," + assignee.toUpperCase() + "," + versions;
line2 = code + " " + versions + ",";
if(code.matches("(?i),* ,*")){
errorString = errorString + "Your product code contains a space and is not valid <br />";
error = true;
}
if(versions.matches("(?i),* ,*")){
errorString = errorString + "Your versions contains a space and are not valid <br />";
error = true;
}
if(engineer.equals("")){
errorString = errorString + "Please enter an Engineer<br />";
error = true;
}
if(assignee.equals("")){
errorString = errorString + "Please enter an Assignee<br />";
error = true;
}
File f = new File(Properties + seperator + "dev.txt");
File f2 = new File(Properties + seperator + "BLTversion.txt");
if (error){
out.println(errorString);
}
else{
try{
FileInputStream fstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] langs = strLine.split(",");
if (!langs[1].equals(original)){
list.add(strLine);
}
}
br.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
if (error){
out.println(errorString);
}
else{
list.add(line1);
Collections.sort(list);
try{
Writer update = new BufferedWriter(new FileWriter(f));
for (int i = 0; i < list.size(); i++) {
if (i == (list.size() - 1)){
update.write((String) list.get(i));
}
else{
update.write((String) list.get(i) + newLineChar);
}
}
update.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
out.println("Your Product was not correctly processed, Please try again");
}
}
try{
FileInputStream fstream2 = new FileInputStream(f2);
DataInputStream in2 = new DataInputStream(fstream2);
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
String strLine2;
while ((strLine2 = br2.readLine()) != null) {
String[] langs = strLine2.split("\\s");
if (!langs[1].equals(original)){
list2.add(strLine2);
}
}
br2.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
if (error){
out.println(errorString);
}
else{
list2.add(line2);
Collections.sort(list2);
try{
Writer update2 = new BufferedWriter(new FileWriter(f2));
for (int i = 0; i < list.size(); i++) {
if (i == (list2.size() - 1)){
update2.write((String) list2.get(i));
}
else{
update2.write((String) list2.get(i) + newLineChar);
}
}
update2.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
out.println("Your Product was not correctly processed, Please try again");
}
}
}
%>