如何指定要写入的文件?

时间:2014-12-01 05:42:24

标签: java if-statement java-io

我正在创建一个程序,它接受用户输入的currentSales,如果它小于1000,则转到lowPerformer或1000以上转到highPerformer.txt文件。我坚持使用if语句。我不确定如何编写程序来告诉数据要保存的文件。任何帮助表示赞赏。

public class HighandLowSales {
   public static void main(String[] args) {
      Scanner input1 = new Scanner(System.in);
      Path highPerformer =
              Paths.get("C:\\Users\\C\\Desktop\\IS103 "
                      + "Programming Logic\\Week7\\HighSales.txt");
      Path lowPerformer = 
              Paths.get("C:\\Users\\C\\Desktop\\IS103 Programming Logic\\"
                      + "Week7\\LowSales.txt");
      String delimiter = ",";
      String s;
      int id;
      String firstName;
      String lastName;

      double currentSales;
      final int QUIT = 999;
      try {
         Scanner input = new Scanner(System.in);
         OutputStream output = 
            new BufferedOutputStream(Files.newOutputStream(highPerformer, CREATE));
         OutputStream output1 =
            new BufferedOutputStream(Files.newOutputStream(lowPerformer, CREATE));

         BufferedWriter writer = 
            new BufferedWriter(new OutputStreamWriter(output));
         BufferedWriter writer1 = 
            new BufferedWriter(new OutputStreamWriter(output1));

         System.out.print("Enter employee ID number >> ");
         id = input.nextInt();
         while(id != QUIT) {
            System.out.print("Enter first name for employee #" + 
                             id + " >> ");
            input.nextLine();
            firstName = input.nextLine();
            System.out.print("Enter last name for employee # " +
                             id + " >> ");
            input.nextLine();
            lastName = input.nextLine();
            System.out.print("Enter current month sales in whole dollar " +
                             "for employee #" + id + " >> ");
            input.nextLine();
            currentSales = input.nextDouble();
            s = id + delimiter + firstName + delimiter + lastName + delimiter
                            + currentSales;

            if (currentSales>1000) 
               writer.write(s);


            writer.newLine();
            writer1.newLine();
            System.out.print("Enter next ID number or " + QUIT +
                             "to quit");
            id = input.nextInt();
         }
         writer.close();

      } catch(Exception e) {
         System.out.println("Message: " + e);
      }          
   }          
}

1 个答案:

答案 0 :(得分:0)

if(currentSales>1000){
    //write to high performer file
    writer.write(s);
} else {
    //write to low performer file
    writer1.write(s);
}