Answer to Question #161501 in Java | JSP | JSF for Tiger

Question #161501

To do list 

This project will record an event which will be described by Title, Description, Date, Time. The events will be stored and retrieved from a text file called events.txt. The program should read a file at a start and should store and delete new and already present events from text file. This program should find events with respect to date as well as name. All search event description should be displayed. 


1
Expert's answer
2021-02-05T15:59:48-0500
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
public class TextDisplay
{   
    static void modifyFile(String filePath, String oldString, String newString)
    {
        File fileToBeModified = new File(filePath);
        
        String oldContent = "";
         
        BufferedReader reader = null;
        
        FileWriter writer = null;
        
      
         
        try
        {
            reader = new BufferedReader(new FileReader(fileToBeModified));
             
            //Reading files
             
            String line = reader.readLine();
            
            
            //display text data
            
            try {
                while ((line = reader.readLine()) != null){
                  System.out.println(line);
                }
            } catch (IOException e1) {
                
                e1.printStackTrace();
            }

             
            while (line != null)
            {
                oldContent = oldContent + line + System.lineSeparator();
                 
                line = reader.readLine();
            }
             
            //replacing old data
             
            String newContent = oldContent.replaceAll(oldString, newString);
             
            //rewriting
             
            writer = new FileWriter(fileToBeModified);
             
            writer.write(newContent);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                //Closing the resources
                 
                reader.close();
                 
                writer.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            
        }
        }
    
     
    public static void main(String[] args)
    {
        modifyFile("E:/Testing/Student.txt", "85", "95");
         
        System.out.println("done");
        
        
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog