Java 输入的数据未写入文件

Java 输入的数据未写入文件,java,Java,我正在尝试创建并写入一个.txt文件,以便其他程序可以打开并读取它。问题是输入的数据没有写入创建的文件。它是一个空白的.txt文档 import java.util.Scanner; import java. io.*; //import class for file input. public class inventoryStock { public static void main(String args[]) throws Exception { //Declaratio

我正在尝试创建并写入一个.txt文件,以便其他程序可以打开并读取它。问题是输入的数据没有写入创建的文件。它是一个空白的.txt文档

import java.util.Scanner;
import java. io.*; //import class for file input.
public class inventoryStock
{
  public static void main(String args[]) throws Exception
  {
    //Declarations
    String[] itemName = new String [10];
    double[] itemCost = new double [10];
    double[] inStockNumber = new double [10];
    int counter = 0;
    //End declarations

    Scanner input = new Scanner (System.in);
     //Open output file.
    FileWriter fw = new FileWriter("updatedStock.txt");
    PrintWriter pw = new PrintWriter(fw);


      do
      {
        System.out.print("Enter item name");
        pw.println();
        itemName[counter] = input.next();
        System.out.print("Enter item cost");
        pw.println();
        itemCost[counter] = input.nextDouble();
        System.out.print("Enter Number in stock");
        pw.println();
        inStockNumber[counter] = input.nextDouble();

        counter += 1;
      }while(counter<10);
        pw.flush();

    pw.close();
    System.exit(0);
  } //End of main method
} //End of InventoryStock class.
import java.util.Scanner;
导入java。io.*//导入类以进行文件输入。
公共类库存
{
公共静态void main(字符串args[])引发异常
{
//声明
String[]itemName=新字符串[10];
double[]itemCost=新的double[10];
double[]inStockNumber=新的double[10];
int计数器=0;
//结束声明
扫描仪输入=新扫描仪(System.in);
//打开输出文件。
FileWriter fw=新的FileWriter(“updatedStock.txt”);
PrintWriter pw=新的PrintWriter(fw);
做
{
系统输出打印(“输入项目名称”);
println();
itemName[计数器]=输入。下一步();
系统输出打印(“输入项目成本”);
println();
itemCost[counter]=input.nextDouble();
系统输出打印(“输入库存编号”);
println();
inStockNumber[counter]=input.nextDouble();
计数器+=1;

}而(counter似乎您并没有真正编写您想要的文件。您可以尝试下面的代码

pw.println(itemName[counter] + ", " + itemCost[counter] + ", " + inStockNumber[counter]);
给你两个建议

  • 由于大小10在代码中随处可见。为了更好地维护,最好将其提取到单个变量中
  • 请遵循java的规则。对于您的情况,类名的第一个字母应该大写。请使用InventoryStock而不是InventoryStock
  • 整个代码如下所示,希望对您有所帮助。Thx

    import java.util.Scanner;
    import java.io.*; //import class for file input.
    
    public class InventoryStock {
        public static void main(String args[]) throws Exception {
    
            int size = 10;
            // Declarations
            String[] itemName = new String[size];
            double[] itemCost = new double[size];
            double[] inStockNumber = new double[size];
            int counter = 0;
            // End declarations
    
            Scanner input = new Scanner(System.in);
            // Open output file.
            FileWriter fw = new FileWriter("updatedStock.txt");
            PrintWriter pw = new PrintWriter(fw);
    
            {
                do {
                    System.out.print("Enter item name");
                    itemName[counter] = input.next();
                    System.out.print("Enter item cost");
                    itemCost[counter] = input.nextDouble();
                    System.out.print("Enter Number in stock");
                    inStockNumber[counter] = input.nextDouble();
    
                    pw.println(itemName[counter] + ", " + itemCost[counter] + ", " + inStockNumber[counter]);
    
                    counter += 1;
                } while (counter < size);
                fw.flush();
            }
            fw.close();
            System.exit(0);
        } // End of main method
    } // End of InventoryStock class.
    
    import java.util.Scanner;
    import java.io.*;//为文件输入导入类。
    公共类库存{
    公共静态void main(字符串args[])引发异常{
    int size=10;
    //声明
    String[]itemName=新字符串[大小];
    double[]itemCost=新的double[尺寸];
    double[]inStockNumber=新的double[尺寸];
    int计数器=0;
    //结束声明
    扫描仪输入=新扫描仪(System.in);
    //打开输出文件。
    FileWriter fw=新的FileWriter(“updatedStock.txt”);
    PrintWriter pw=新的PrintWriter(fw);
    {
    做{
    系统输出打印(“输入项目名称”);
    itemName[计数器]=输入。下一步();
    系统输出打印(“输入项目成本”);
    itemCost[counter]=input.nextDouble();
    系统输出打印(“输入库存编号”);
    inStockNumber[counter]=input.nextDouble();
    pw.println(项目名称[计数器]+”,“+项目成本[计数器]+”,“+库存编号[计数器]);
    计数器+=1;
    }而(计数器<大小);
    fw.flush();
    }
    fw.close();
    系统出口(0);
    }//主方法结束
    }//InventoryStock类结束。
    
    您必须实际告诉
    PrintWriter
    写入文件,否则即使您使用
    input.next()
    获取用户的输入,它也不会执行任何操作。请尝试以下操作:

    Scanner input = new Scanner (System.in);
    //Open output file.
    FileWriter fw = new FileWriter("updatedStock.txt");
    PrintWriter pw = new PrintWriter(fw, true);
    
    do
    {
        System.out.print("Enter item name");    
        itemName[counter] = input.next();
        pw.write(itemName[counter]);
        pw.println();
    
        System.out.print("Enter item cost");
        itemCost[counter] = input.nextDouble();
        pw.write(String.valueOf(itemCost[counter]));
        pw.println();
    
        System.out.print("Enter Number in stock");
        inStockNumber[counter] = input.nextDouble();
        pw.write(String.valueOf(inStockNumber[counter]));
        pw.println();
    
        counter += 1;
    }while(counter<10);
    
    pw.flush();
    pw.close();
    System.exit(0);
    
    扫描仪输入=新扫描仪(System.in);
    //打开输出文件。
    FileWriter fw=新的FileWriter(“updatedStock.txt”);
    PrintWriter pw=新的PrintWriter(fw,真);
    做
    {
    系统输出打印(“输入项目名称”);
    itemName[计数器]=输入。下一步();
    书写(项目名称[计数器]);
    println();
    系统输出打印(“输入项目成本”);
    itemCost[counter]=input.nextDouble();
    pw.write(String.valueOf(itemCost[counter]);
    println();
    系统输出打印(“输入库存编号”);
    inStockNumber[counter]=input.nextDouble();
    pw.write(String.valueOf(inStockNumber[计数器]);
    println();
    计数器+=1;
    
    }while(反欢迎使用堆栈溢出。如果需要帮助解决问题,您需要向我们显示实际代码。恕我直言,可能您没有将缓冲区刷新到磁盘。为什么在
    do
    之前有额外的“{”和
    pw.close()之前有额外的“}”;
    ?我是一个初学者,上一次因为没有花括号而被责骂。另外,我正在尝试在细节循环中获取输入。在添加扫描仪项目之前,我只得到了十条“输入项目名称”“输入项目成本”“输入库存数量”的条目您正在将输入存储到
    itemName
    itemCost
    inStockNumber
    中,但它从来不会用
    pw写入文件。写入
    ,所有
    pw
    所做的就是当前添加新行quidproquo,我该如何将其写入文件?非常感谢您的帮助。我仍在尝试学习所有这些技巧和获得我想要的效果所用的语言。我很感激。@quidproquotis也是获得我想要的东西的一个很好的方法。所以我错了,但它并不像我原来想的那么糟糕。你们非常感谢Gearon@Gearon的帮助