在文件类中写入而不覆盖前一个

时间:2014-01-16 13:03:23

标签: java file-io filereader filewriter

我制作了一个程序,可以保存用户的姓名,职位和薪水 例:
名称:user1
位置:成员
hrs / wrk:20

然后我再放一个:
名称:user2
职位:经理
hrs / wrk:25

但是当它显示信息时

如果是这样的话:
名称:user2
职位:经理
hrs / wrk:25

name:user2 职位:经理 hrs / wrk:25

我总是得到最后一个用户输入:(帮帮我PLZ ...对不起我的PH来自英语。

第一类


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);
}

}


第二课


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);
    }

}

}

2 个答案:

答案 0 :(得分:1)

在writeFile方法中,您将以覆盖整个现有文件的方式创建新的FileWriter。

相反,您需要以追加模式创建它。

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

答案 1 :(得分:0)

使用java.io.FileWriter打开文件时,可以将第二个布尔参数传递给constructor,指示是要追加(true)还是覆盖(false })现有数据。

示例:

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