3

I have an assignment where I have created a program to sell and order electronic devices and update two text files whenever a new sale/order has been made.

I found a way to update the text file instead of overwriting it so any old orders/sales are not lost and the new ones are added to the end of the file, but my assignment requires me to have the text file in the following form:

SALES
{
    SALE
    {

       (Sale info here)

     }

    SALE
    {

       (Another sale info here)

     }

}

The SALES { } needs to appear once in the whole file, and I need to update the file with each SALE { }. Can I make it so that the writer writes only after SALES } (therefore in the 3rd line) and before } (so in the second to last line), even after restarting the application?

This is part of the code of my writer:

    File file1= null;
    BufferedWriter writer=null;

    try {
        file1=new File(path);
    }

    catch (NullPointerException e) {
        System.err.println ("Not Found.");
    }

    try {
        writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file1, true)));
    }

    catch (FileNotFoundException e) {
        System.err.println("Error opening file for writing.");
    }

    try 
    {
        writer.write("SALES " + "\n" + "{");

        //Writer writes sale info here

        writer.write("\n" + "}");

    }

    catch (IOException e) {
        System.err.println("Write error!");

    }

Basically as of now, it creates SALES{ } every time I run the program, which is something I don't want.

Another way I thought of doing this is basically start the file with the following:

SALES
{
}

and just overwrite the last line with every new order, and at the end of each execution I will add another } in the end which will close the upper SALES {. But I also do not know how to do that.

Sorry if this sounds very amateurish. Thank you for any answers beforehand.

Dimi
  • 31
  • 3
  • please share your code as well – Imran Ali May 20 '16 at 13:44
  • Added part of my writer's code. – Dimi May 20 '16 at 13:53
  • Normally people solve this problem by writing a new file and then renaming afterwards. Is this possible in your case? – Alexander May 20 '16 at 13:57
  • @Alexander how do you mean? The only way I've thought of doing this is having the writer write in a temporary file and then copying the whole file between line 2 and the last line of the main sales file. But I don't know how I would do that aswell. – Dimi May 20 '16 at 14:01
  • I am not sure but this can be done by following this, read the file and initialize an object or collection of objects, add new object(entries) to that collection. Write the collection to the file. – viveksinghggits May 20 '16 at 14:02
  • First create your sales info and then append "SALES {" at the begining and after counting your new String add " } " to the end of the String. – cihan adil seven May 20 '16 at 14:13
  • @cihanseven the way I understand it, would this not do the same to every single Sale? I need the SALES { } once in the whole file. – Dimi May 20 '16 at 14:18
  • @Dimi That's my point. First of all finish your sales info String and when you're done just wrap it with brackets. – cihan adil seven May 20 '16 at 14:21
  • Truly sorry if I'm getting your suggestion wrong - I have done what (I understood) you said, and it does wrap all the sales in Sales { }. However, once the application is executed again, the new sales until the application closes, are also added in a second Sales { }, with me ending up with something in the form of Sales { sales from 1st execution } Sales { sales from 2nd execution }. What I want is Sales { sales from 1st execution and sales from 2nd execution }. If that makes sense. – Dimi May 20 '16 at 14:25

2 Answers2

0

One way you can give a try is by checking whether "SALES {" string is present in your file. If present you may directly write sales info else write the entire file.

You can include following snippet in your code to scan the file line by line as follows:

Scanner scanner = new Scanner(file1);
while(scanner.hasNextLine()){
 if("SALES{".equals(scanner.nextLine().trim())){
    //Writer writes sale info here
    break;
  }else{
   writer.write("SALES " + "\n" + "{");
    //Writer writes sale info here
    writer.write("\n" + "}");
  }
 } 
Dhaval Simaria
  • 1,886
  • 3
  • 28
  • 36
0

First of all, use this as a line separator:

String lineSeparator = System.getProperty("line.separator");

Why? diferent systems use diferent ways to separate the lines ( \n < linux, \r\n < windows, \r < mac).

In your code you will change de +"\n"+ to + lineSeparator + .

The best way to write this is to use a collection (array) of Sale Objects and then you will interate through this collection, like:

for(Sale sale : sales){
    sale.getters // Infos
    //write  +\t+ (tab) and save infos
    }

and then finish with "+}+"

For me its better to always create a new file in this case.

  • I have done what you said, this is exactly what my code looks like in the part that I wrote "Writer writes sale info here" and I have enclosed it in SALES { and }. But every new execution of the program writes all the new sales in that execution in a new SALES { and }. I want the SALES { to be written once, in the begging of the file and the } at the end. If I understood you correctly. – Dimi May 20 '16 at 14:56
  • "For me its better to always create a new file in this case." Yes, you have to write a new file. Read the file and save the old sale objects on the array and insert the new one sale objects to the array and save it. – Diego Moraes May 20 '16 at 15:24