Java 第二个缓冲写入程序赢得';行不通
我的简单I/O清单程序有点问题。它是一个程序,将用户输入的输出写入一个文本文件,作为“库存”。我能做到。但是,当我尝试在同一文件夹中创建和附加不同的文本文件时,它不会将输入写入新的文本文件。我知道我真的缺乏经验来恰当地解释我的担忧。但我正在尽最大努力,所以基本上,我只想将作为“sales”输入的信息打印到同一路径文件夹的第二个text.file中。如果你检查并运行我的代码,我希望你能得到一些想法。代码正在工作,但正如我所说,唯一的问题是不同文本文件上的输入/输出。我非常感谢你的帮助。谢谢 卡尔 这是我的密码:Java 第二个缓冲写入程序赢得';行不通,java,path,text-files,bufferedwriter,Java,Path,Text Files,Bufferedwriter,我的简单I/O清单程序有点问题。它是一个程序,将用户输入的输出写入一个文本文件,作为“库存”。我能做到。但是,当我尝试在同一文件夹中创建和附加不同的文本文件时,它不会将输入写入新的文本文件。我知道我真的缺乏经验来恰当地解释我的担忧。但我正在尽最大努力,所以基本上,我只想将作为“sales”输入的信息打印到同一路径文件夹的第二个text.file中。如果你检查并运行我的代码,我希望你能得到一些想法。代码正在工作,但正如我所说,唯一的问题是不同文本文件上的输入/输出。我非常感谢你的帮助。谢谢 卡尔
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;
import static java.nio.file.StandardOpenOption.*;
import java.text.*;
import java.util.*;
public class Inventorypos {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String File ="D:\\Copro2\\Inventory.txt";
String File2 ="D:\\Copro2\\Sales.txt";
Path file = Paths.get("D:\\Copro2\\Inventory.txt");
Path salefile = Paths.get("D:\\Copro2\\Sales.txt");
String st = " ";
String sH = " ";
String sF = " ";
String sS = " ";
String delimeter = "................ ";
int itemnum;
int itemname;
String H="Hamburger";
String F="French Fries";
String S="Softdrinks";
int menu;
long menu2 = 0;
final long QUIT = 5;{
{
try{
Runtime rt=Runtime.getRuntime();
OutputStream invent = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
BufferedWriter writer = new
BufferedWriter(new OutputStreamWriter(invent));
//so here is my problem here, I couldn't make this work
OutputStream sale = new BufferedOutputStream(Files.newOutputStream(salefile, CREATE,StandardOpenOption.APPEND));
BufferedWriter writer2 = new
BufferedWriter(new OutputStreamWriter(sale));
String sa= "---- Sales ----";
writer2.write(sa);
writer2.newLine();
String in= "---- Inventory ----";
writer.write(in);
writer.newLine();
System.out.println("---- Page Menu ----");
System.out.println("1. Add Inventory \n2. Sell \n3. View Inventory \n4. View Sales \n5. Exit ");
System.out.print("Enter selection: ");
menu= input.nextInt();
while(menu2 != QUIT){
if(menu==1) {
System.out.println("Add Inventory: " );
System.out.print("1.) Hamburger \n2.)French Fries \n3.)Softdrinks\n");
System.out.println("Enter Item: " );
itemname= input.nextInt();
if (itemname == 1){
System.out.print("How many do you want to store? ");
itemnum = input.nextInt();
sH = H + delimeter + itemnum;
writer.write(sH, 0, sH.length());
writer.newLine();
}
else if (itemname == 2){
System.out.print("How many do you want to store? ");
itemnum = input.nextInt();
sF = F + delimeter + itemnum;
writer.write(sF, 0, sF.length());
writer.newLine();
}
else if (itemname == 3){
System.out.print("How many do you want to store? ");
itemnum = input.nextInt();
sS = S + delimeter + itemnum;
writer.write(sS, 0, sS.length());
writer.newLine();
}
else{
System.out.println("Invalid input!");
menu=1;
}
System.out.print("Enter 1 to store another item, 2 to view inventory, 3 sell,4 to quit: ");
menu2 = input.nextLong();
if (menu2==1){
menu=1;
}
else if (menu2==2){
menu=3;
}
else if (menu2==3){
menu=6;
}
else if (menu2==4){
menu2=QUIT;
}
else{
System.out.println("Invalid Input!");
}
}
if(menu==2){
System.out.print("---- Food Menu ----");
System.out.println("\n1.) Hamburger P25 \n2.) French Fries P25 \n3.) Softdrinks P25");
System.out.println("Enter your order: ");
int ord = input.nextInt();
if (ord==1){
System.out.println("How many?");
int hm = input.nextInt();
int totph = hm*25;
/*String intwo = "---- Updated inventory ----";
writer.write(intwo);
writer.newLine();
/*int left =
String nI = H+delimeter+*/
st = H+hm+ delimeter+ totph;
writer2.write(st,0,st.length());
writer2.newLine();
System.out.println("You purchased "+hm+" "+H+"s with the total price of "+totph+".");
}
else if (ord==2){
System.out.println("How many?");
int hm = input.nextInt();
int totph = hm*25;
/*String intwo = "---- Updated inventory ----";
writer.write(intwo);
writer.newLine();
/*int left =
String nI = H+delimeter+*/
st = F + hm + delimeter + totph;
writer2.write(st, 0, st.length());
writer2.newLine();
System.out.println("You purchased "+hm+" "+F+" with the total price of "+totph+".");
}
else if (ord==3){
System.out.println("How many?");
int hm = input.nextInt();
int totph = hm*25;
/*String intwo = "---- Updated inventory ----";
writer.write(intwo);
writer.newLine();
/*int left =
String nI = H+delimeter+*/
st = S+hm+ delimeter+ totph;
writer2.write(st,0,st.length());
writer2.newLine();
System.out.println("You purchased "+hm+" "+S+"s with the total price of "+totph+".");
}
else{
System.out.println("Invalid input!");
menu=2;
}
System.out.print("Enter 1 to go back to the food menu, 2 to view sales, 3 to go back to the main option or 4 to quit: ");
menu2 = input.nextInt();
if (menu2==1){
menu=2;
}
else if (menu2==2){
menu=4;
}else if (menu2==4){
menu2=QUIT;
}
else if (menu2==3){
menu=6;
}
else{
System.out.println("Invalid Input!");
}
}
if(menu==3){
{
menu2 = QUIT;
writer.close();
Process p=rt.exec("notepad "+File);
System.out.println("Do you want to continue? y for yes, n for no");
String con = input.next();
if (con.equalsIgnoreCase("y")){
main(null);
}
else if(con.equalsIgnoreCase("n")){
menu2=QUIT;
}
}
}
if (menu==4){
{
menu2 = QUIT;
writer.close();
Process p=rt.exec("notepad "+File2);
System.out.println("Do you want to continue? y for yes, n for no");
String con = input.next();
if (con.equalsIgnoreCase("y")){
main(null);
}
else if(con.equalsIgnoreCase("n")){
menu2=QUIT;
}
}
}
if (menu==5){
System.exit(0);
}
else if (menu ==6){
main(null);
}
else{
System.out.println("Invalid Input!");
}
}
}catch (Exception e){
System.out.println("Message: " +e);
}
}
}
}
}
这就是BufferedWriter对象的特殊之处。为了提高效率,它们将信息存储在“缓冲区”或临时信息堆中,然后当缓冲区超过一定大小时,它将“刷新”数据或将其写入文件。当调用flush()方法或close()方法时,将完成数据刷新。在您的代码中,我不相信您在编写器上调用了close(),不管类型如何,您实际上都是这样做的 您可以使用以下命令调用它:
writer.close();
或者,如果您只是想在不关闭写入程序的情况下保存数据,您可以调用:
writer.flush();
旁注:您真的应该将此代码拆分为几个单独的函数。这将使理解和讨论代码变得更加简单。旁注2:您不应该在最外层的作用域上声明所有这些变量。你的
st
,sH
等变量应该尽可能地声明在你使用它们的地方。对于292行代码,可能应该设计一个。而且你不应该习惯不必要地缩写变量名-代码中的字符不是稀缺资源,所有相关的编辑器/IDE都支持代码完成,这只会使代码更难阅读。谢谢大家的建议!我真的很感激