2013-12-08 5 views
1

데이터 파일에 모든 직원, 개별 직원을 표시하고 직원을 삽입하며 직원을 삭제하는 섹션이 있어야하는 프로그램이 있습니다. 다음은 데이터 파일이나 텍스트 파일에서 행을 삭제하는 방법은 무엇입니까?

는 전체 코드입니다하지만 어떻게 당신은 "또는 널 (null)"을 원하는 라인을 대체하기 위해 아래의 코드를 사용할 수 있습니다

import java.io.*; 
import java.util.*; 
import java.util.Scanner; 
import javax.swing.*; 
import java.io.IOException; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 

public class Program6pt2 
{  

    static Scanner scan = new Scanner (System.in); 
    static String str, FName, LName, SS, SS1, Name; 
    static double pay, hours; 
    static double rate, gross, overtime, net, NO, fed, state, deduction; 
    static double union=7.85, hospital=25.65; 
    static char ch, ch1; 
    static int num, i; 
    //static Scanner scan= new Scanner(System.in); 
    public static void main(String[] args)throws FileNotFoundException, IOException 
    { 
     Scanner infile= new Scanner(new FileReader("G:\\Program5.txt")); 
     { 
     System.out.println("########################################################"); 
     System.out.println("#  The purpose of the following algorithms   #"); 
     System.out.println("#  are to find the area, surface area, volume,`  #"); 
     System.out.println("# and compound interest of the following programs #"); 
     System.out.println("########################################################"); 

     do { 

      System.out.println("######################################################"); 
      System.out.println("#             #"); 
      System.out.println("#  MENU          #"); 
      System.out.println("#  1. Individual Employees      #"); 
      System.out.println("#  2. All Employees        #"); 
      System.out.println("#  2. Insert          #"); 
      System.out.println("#  2. Quit          #"); 
      System.out.println("######################################################"); 
      System.out.println(); 
      System.out.println(" Please enter 1 or 2 to quit "); 
      num = scan.nextInt();  
      switch (num) 
      { 
       case 1:individual(SS, LName, FName, pay, hours); 
        break; 
       case 2: all(SS, LName, FName, pay, hours); 
        break; 
       case 3: insert(SS, LName, FName, pay, hours); 
        break; 
       case 4: delete(SS, LName, FName, pay, hours); 
        break; 
       default: System.out.println("Thank you so much for using this Program!"); 
      } 

     }while(num != 5);               

     } 
    } 


    public static void individual(String a, String b, String c, double d, double e)throws FileNotFoundException, IOException 
    { 
     Scanner infile= new Scanner(new FileReader("G:\\Program5.txt")); 
     int i=0; 
     System.out.println("---****************************************************************---"); 
     System.out.println("--- THIS MENU ALLOWS THE USER TO SELECT A SS NUMBER OR AN  ****---"); 
     System.out.println("--- INDIVIDUAL'S NAME AND DISPLAY ALL THEIR INFO AS WELL  ****---"); 
     System.out.println("--- DISPLAY ALL THE NAMES IN THE LIST AS WELL AS THEIR SS # IN ***----"); 
     System.out.println("--- A LIST              ****---"); 
     System.out.println("---****************************************************************---"); 
     do 
     { 
     System.out.println("################### #########################"); 
     System.out.println("#   MAIN MENU      #"); 
     System.out.println("#  1. Enter The Social Security Number #"); 
     System.out.println("#  2. Quit        #"); 
     System.out.println("#  3. Main Menu       #"); 
     System.out.println("#           #"); 
     System.out.println("#############################################"); 
     System.out.println(); 
     System.out.println(" Please enter 1,2,3 or 4 "); 
     num = scan.nextInt(); 
     System.out.println(); 


     switch (num) 
     { 
      case 1:SSnumber(a,b,c,d,e); 
       break; 
      case 3: System.out.println("Please enter a number"); 
       break;        
      default: System.out.println("Please select A, B, C, or D to Quit"); 
       break; 
     } 
     }while(num != 3); 
    } 

    public static void SSnumber(String A, String B, String C, double D, double E)throws FileNotFoundException, IOException 
    { 
     Scanner infile = new Scanner(new FileReader("G:\\Program5.txt")); 
     int i=0; 
     System.out.print("Enter the employee number (ex. 111-11-1111) : "); 
     SS1= scan.next(); 
     System.out.println(); 

     System.out.println("-----------------------------------------------------------------------------------------------------------"); 
     System.out.printf("%-4s%10s%13s%8s%10s%14s%12s%13s%10s%n", "NO.", "Last Name", "First Name", "Hours", "Payrate", "Overtime Pay", "Gross Pay", "Deductions", "Net Pay"); 
     System.out.println("-----------------------------------------------------------------------------------------------------------"); 

     while (infile.hasNext()) 
     { 
     A= infile.next(); 
     B= infile.next(); 
     C= infile.next(); 
     D= infile.nextDouble(); 
     E= infile.nextDouble(); 

     ++i; 
     if(A.compareTo(SS1) ==0) 
     { 
      if(E>40) 
      { 
       overtime= 1.5*D*(E-40); 
       gross= overtime+ (D*40); 
      } 
      else 
      { 
       overtime=0; 
       gross= E*D; 
      } 
      fed=gross*0.18; 
      state=gross*0.045; 
      deduction=fed+state+hospital+union; 
      net=gross-deduction; 
      //System.out.println(+i+ B+ C+ E+ D+ overtime+ gross+ deduction+ net); 

      System.out.printf("%-5d%-12s%-12s%6.1f%9.2f%10.2f%15.2f%13.2f%10.2f%n", i, B, C, E, D, overtime, gross, deduction, net); 
     } 
     } 
     infile.close(); 
     System.out.println(); 
     System.out.println(); 
    } 


    public static void all(String a, String b, String c, double d, double e)throws FileNotFoundException, IOException 
    { 

     Scanner infile = new Scanner(new FileReader("G:\\Program5.txt")); 
     int i=0; 


     System.out.println("-----------------------------------------------------------------------------------------------------------"); 
     System.out.printf("%-4s%10s%13s%8s%10s%14s%12s%13s%10s%n", "NO.", "Last Name", "First Name", "Hours", "Payrate", "Overtime Pay", "Gross Pay", "Deductions", "Net Pay"); 
     System.out.println("-----------------------------------------------------------------------------------------------------------"); 

     while (infile.hasNext()) 
     { 
     a= infile.next(); 
     b= infile.next(); 
     c= infile.next(); 
     d= infile.nextDouble(); 
     e= infile.nextDouble(); 

     ++i; 
     if(e>40) 
     { 
      overtime=1.5*d*(e-40); 
      gross= overtime+(d*40); 
     } 
     else 
     { 
      overtime=0; 
      gross=e*d; 
     } 
     fed=gross*0.18; 
     state=gross*0.045; 
     deduction=fed+state+union+hospital; 
     net=gross-deduction; 

     System.out.printf("%-5d%-12s%-12s%6.1f%9.2f%10.2f%15.2f%13.2f%10.2f%n", i, b, c, e, d, overtime, gross, deduction, net); 
     } 
     infile.close(); 
     System.out.println(); 
     System.out.println(); 
    } 



    public static void insert(String a, String b, String c, double d, double e)throws FileNotFoundException 
    { 
     Scanner infile = new Scanner(new FileReader("G:\\Program5.txt")); 
     try 
     { 

     while (infile.hasNext()) 
     { 
      a= infile.next(); 
      b= infile.next(); 
      c= infile.next(); 
      d= infile.nextDouble(); 
      e= infile.nextDouble(); 
     } 
     System.out.print("Enter the ID number (ex.111-22-3333) : "); 
     a=scan.next(); 
     System.out.print("Enter the Last Name (ex. Smith) : "); 
     b=scan.next(); 
     System.out.print("Enter the First Name (ex. John) : "); 
     c=scan.next(); 
     System.out.print("Enter the pay rate (ex 7.00) : "); 
     d=scan.nextDouble(); 
     System.out.print("Enter the hours worked (ex 57.0) : "); 
     e=scan.nextDouble(); 

     FileWriter insert1 = new FileWriter("G:\\Program5.txt", true); 

     insert1.append("\n"); 
     insert1.append(a + " "); 
     insert1.append(b + " "); 
     insert1.append(c + " "); 
     insert1.append(d + " "); 
     insert1.append(e + "\n"); 
     insert1.close(); 
     } 
     catch(Exception o) 
     { 
     System.out.println("Wrong"); 
     } 
     System.out.println(); 
    } 


    public static void delete(String a, String b, String c, double d, double e)throws FileNotFoundException 
    { 
     Scanner infile = new Scanner(new FileReader("G:\\Program5.txt")); 
     try 
     { 
     boolean append=false; 
     Scanner inFile = new Scanner(new FileReader("G:\\Program5.txt")); 
     FileWriter outfile= new FileWriter("G:\\Program5.txt", append); 
     BufferedWriter print = new BufferedWriter(outfile); 


     System.out.println("choose a employee to delete from the system."); 
     System.out.print("Enter the employee Social Security Number: "); 
     SS= scan.next(); 
     { 
      print.write("\n"); 
      print.write(a + " "); 

      print.write("\n"); 
     } 
     print.close(); 
     } 
     catch (IOException ioe) 
     { 
     ioe.printStackTrace(); 
     System.out.println("Wrong"); 
     } 
     System.out.println(); 
    } 
} 
+0

먼저 "삭제", "줄"및 "파일"을 정의해야합니다. (또한 프로그램의 영역, 표면적, 볼륨 및 복리를 정의하는 것을 보는 것이 흥미로울 것입니다.) –

+1

(일반적으로 표준 디스크 파일의 중간에서 한 줄을 삭제할 수는 없습니다. 끝에서 삭제할 파일을자를 수 있습니다. 또는 공백이나 일부 줄로 바꿀 수 있습니다. 또는 파일을 복사하고 새 복사본의 줄을 생략 할 수 있지만 거의 옵션입니다.) –

+0

@ Hotlicks! 하하! 항상 내 프로그램에 오래된 쉘을 사용합니다. 나는 결코 그들을 버리지 않는다! 나는 급료 지불 제도의 지역, sa, 양 및 & 복합 관심사를 찾아내는 것은 아주 재미있을 것이라는 점을 생각한다! 고맙습니다. – user3080143

답변

0

삭제 부분을 수행하는 단서가 없다;

public static boolean readReplace(String fileName, String oldPattern, 
     String replPattern, int lineNumber) { 
    String line; 
    StringBuffer sb = new StringBuffer(); 
    int nbLinesRead = 0; 
    try { 
     FileInputStream fis = new FileInputStream(fileName); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       fis)); 
     while ((line = reader.readLine()) != null) { 
      nbLinesRead++; 
      line = line.toLowerCase(); 

      if (nbLinesRead == lineNumber) { 
       line = line.replaceFirst(oldPattern.toLowerCase(), 
         replPattern); 
      } 
      sb.append(line + "\n"); 
     } 
     reader.close(); 
     BufferedWriter out = new BufferedWriter(new FileWriter(fileName)); 
     out.write(sb.toString()); 
     out.close(); 

    } catch (Exception e) { 
     return false; 
    } 
    return true; 
} 
+0

mohammed amine bourk! 한 가지 간단한 질문, '4'의 경우 스위치에 넣을 것이 무엇입니까? – user3080143

+0

사례 4 : readreplace(); – user3080143

+0

+1 투표로 감사드립니다. – Bourkadi

0

짧은 대답 : 할 수 없습니다.

긴 대답 : 새 파일을 만들고 그 파일에없는 파일을 제외한 모든 줄을 복사하십시오. 그런 다음 이전 파일의 이름을 바꾼 다음 새 파일의 이름을 바꾸고 이전 파일을 제거하십시오.

관련 문제