Answer to Question #162502 in Java | JSP | JSF for Sawand junaid

Question #162502

Book Management System  

The project will manage record of books in stock in a file called books.txt. For each book in stock, Id, Name, Category, Author, Editor, Price, in stock and total sold are stored. These books can be updated and deleted with purchase and sell of stock. 

The software should be able to find a book with id, name and quantity as well. 



1
Expert's answer
2021-02-10T13:25:34-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/Book.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