2014-01-16 111 views
0

我製作了一個程序,用於保存用戶的名稱,位置和薪水。
例:
名稱:USER1
現在的位置是:會員
小時/ WRK:20在不覆蓋前一個文件的情況下寫入文件類

然後,我把另一個問題:
名稱:user2的
位置:經理
小時/ WRK:25

但是當它顯示信息時

如果是這樣的話:
名稱:user2的
位置:經理
小時/ WRK:25

名稱:user2的 位置:經理 小時/ WRK:25

我總是最後一個用戶輸入:(幫我PLZ。對不起,我的英語來自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); 
} 

}


第二類


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

} 

}

+0

請使用正確的格式 –

回答

1

在您的writeFile方法中,您將以覆蓋整個現有文件的方式創建一個新的FileWriter。

相反,您需要在追加模式下創建它。

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

當您打開使用java.io.FileWriter一個文件,你可以通過第二個布爾參數,指示是否要追加(true)或覆蓋(false)現有數據的constructor

例子:

FileWriter r = new FileWriter("POSITION.TXT", true); 
相關問題