Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:47:11+00:00 2026-06-07T05:47:11+00:00

I’m trying to create a JavaServer Page that reads in two text files (dev.txt

  • 0

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");
       }
   }
}    
  %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:47:12+00:00Added an answer on June 7, 2026 at 5:47 am

    Use

    new FileWriter( fileName, append );
    

    where append should be true to retain the existing file contents and append with the new content.

    Refer to:

    1. FileWriter(java.lang.String, boolean)
    2. FileWriter(java.io.File, boolean)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.