如何指定要写入的文件?

编程入门 行业动态 更新时间:2024-10-24 10:25:25
本文介绍了如何指定要写入的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个程序,它接受用户输入的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); }

更多推荐

如何指定要写入的文件?

本文发布于:2023-11-05 09:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要写   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!