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.
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");
}
}
Comments
Leave a comment