Xavier
Xavier

Reputation: 1

Writing in a file class without overwriting the previous one

I made a program that will save the NAME,POSITION and SALARY of a user.
ex.:
name: user1
postion: member
hrs/wrk: 20

Then I put another one:
name: user2
position: manager
hrs/wrk: 25

but when it displays the informations

if goes like this:
name: user2
position: manager
hrs/wrk: 25

name: user2 position: manager hrs/wrk: 25

I always get the last user input :( help me plz.. sorry for my english im from PH.

FIRST CLASS


import java.io.*;

import java.util.*;


public class Xavier
{
    public static Scanner x = new Scanner(System.in);
    public static int pos[] = new int[20];
    public static int path,user;
    public static String out,data;
    public static double hrswrk[]= new double [20];
    public static double sal[] = new double [20];
    public static String name[]= new String [20];
    public static BufferedReader xx = new BufferedReader(new      InputStreamReader(System.in));
public static void main(String args[])
{
    System.out.println("\n"+user+"HOTKEY\t\t\tPATH");
    System.out.println("1.....\t\t\tSALARY\n2.....\t\t\tDATABASE");
    path = x.nextInt();
    if(path == 1)
    {
        Output();
    }
    else
    {
        XX.displayDATABASE();
    }

}
public static void Output()
{
    XX.writeFile();
    XX.readFile();
    try
    {
    XX.getPosition();
    }
    catch(IOException z)
    {
        System.out.print("ERROR");
    }


}
public static double getSalary()
{
    if(pos[user] == 1)
    {
        sal[user] = 50*hrswrk[user];
    }
    if(pos[user] == 2)
    {
        sal[user] = 40*hrswrk[user];
    }
    if(pos[user] == 3)
    {
        sal[user] = 60*hrswrk[user];
    }

    System.out.println("YOUR SALARY IS: "+sal[user]);
    System.out.println("__________________________");
    XX.writeInfo();


    return(1);
}

}


SECOND CLASS


import java.util.*;
import java.io.*;
public class XX extends Xavier
{
public static void main(String args[])
{


}

public static  String getPosition() throws IOException
{
       //System.out.print("________________________\nHOTKEY\t\t\tPOSITION\n_________________________\n1.....\t\t\tMANAGER\n2.....\t\t\tMEMBER\n3.....\t\t\tJANITOR");

    System.out.println("______________________");
    System.out.print("ENTER POSITION: ");
    pos[user] = x.nextInt();
    System.out.print("ENTER HOURS WORKED: ");
    hrswrk[user] = x.nextDouble();
    System.out.print("ENTER NAME: ");
    name[user] = xx.readLine();



    Xavier.getSalary();

    return("");

}
public static void readFile()
{
    try
    {
    FileReader v= new FileReader("POSITION.TXT");
    BufferedReader n= new BufferedReader(v);

    while((out = n.readLine())!= null)
    {
        System.out.println(out);
    }
    n.close();
    }
    catch(IOException z)
    {
        System.out.print("FILE NOT FOUND");
    }
}
public static void writeFile()
{
    try
    {

    FileWriter r = new FileWriter("POSITION.TXT");
    PrintWriter e = new PrintWriter(r);

    e.println("___________________");
    e.println("1....MANAGER = 50");

    e.println("2....MEMBER = 40");
    e.println("3....JANITOR = 60");
    e.println("____________________");

    e.close();

    }
    catch(IOException z)
    {
        System.out.print("ERROR!");
    }

}
public static void writeInfo()
{
    try
    {

    FileWriter r = new FileWriter("DATABASE.TXT");
    PrintWriter ss = new PrintWriter(r);
    ss.print("_____________________________\n");
    ss.print("NAME:"+name[user]);
    if(pos[user] == 1)
    {
    ss.print("\nPOSITION:MANAGER");
    }
        if(pos[user] == 2)
    {
    ss.print("\nPOSITION:MEMBER");
    }
        if(pos[user] == 3)
    {
    ss.print("\nPOSITION:JANITOR");
    }
    else
    {

    }
    ss.println("\nSALARY:"+sal[user]);
    ss.print("__________________________");
    ss.close();
    }

    catch(IOException z)
    {
        System.out.print("ERROR");
    }
    XX.Loop();



}
public static void displayDATABASE()
{
    try
    {
        FileReader v=new FileReader("DATABASE.TXT");
        BufferedReader sss= new BufferedReader(v);

        while((data = sss.readLine())!=null)
        {
            System.out.println("\n"+data);
        }
        sss.close();
    }
    catch(IOException z)
    {
        System.out.print("ERROR");
    }


}
public static void Loop()
{
    user++;
    System.out.println("\n"+user+"HOTKEY\t\t\tPATH");
    System.out.println("1.....\t\t\tSALARY\n2.....\t\t\tDATABASE\n3.....EXIT");
    path = x.nextInt();
    if(path == 1)
    {
        Output();
    }
    if(path == 2)
    {
        XX.displayDATABASE();
    }
    else
    {
        System.exit(0);
    }

}

}

Upvotes: 0

Views: 110

Answers (2)

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31279

When you open a file using java.io.FileWriter, you can pass a second boolean argument to the constructor that indicates whether you want to append (true) or overwrite (false) existing data.

Example:

FileWriter r = new FileWriter("POSITION.TXT", true);

Upvotes: 0

Tim B
Tim B

Reputation: 41188

In your writeFile method you create a new FileWriter in a way that will overwrite the entire existing file.

Instead you need to create it in append mode.

FileWriter r = new FileWriter("POSITION.TXT", true);

Upvotes: 1

Related Questions