如何指定要写入的文件? [英] How to specify which file to write to?

查看:104
本文介绍了如何指定要写入的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a program that takes the users input of currentSales, and if it's less than 1000 it goes to lowPerformer or more than 1000 goes to highPerformer.txt file. I'm stuck at the if statement. I'm not sure how I would code the program to tell the data what file to save to. Any assistance is appreciated.

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


推荐答案

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

这篇关于如何指定要写入的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆