I’m trying to create a JavaServer Page that reads in two text files (dev.txt and BLTversion.txt) and lets the user write to them to update their contents. Only problem is any time the user tries to update BLTversion.txt it deletes the files contents…So I assume there is something wrong with the writing part? I’m going to keep trying to get this but just thought I would post it in case anybody can see where I’m going wrong before I figure it out. Here is the code:
<%
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");
}
}
}
%>
Use
where append should be
trueto retain the existing file contents and append with the new content.Refer to: