簡體   English   中英

如何在Java中將數據讀/寫到外部文件(.txt)?

[英]How to Read/Write data to an external file (.txt) in Java?

我很沮喪 我可以得到提示,並且輸入的響應將被保存,但是直到退出程序為止。 我無法回憶起它們以后再編程,.txt文件為空。 我知道我必須四處走動,但是我無法弄清楚是什么地方,這使我發瘋。 我將非常感謝您的幫助。

import java.io.*;
import java.util.StringTokenizer;

public class CornerStoreDranks
{
 public static void main (String [] args)
 {
     Cooler coolerUno = new Cooler();
     int selection;
     char chyea;
     String drank, type, size, stock, doc, infoLine;
     String file = "Drank Management.txt";

    try
    {
        FileReader lincoln = new FileReader(file);
        BufferedReader bread = new BufferedReader(lincoln);
        FileWriter fWriter = new FileWriter(file);
        BufferedWriter muscle = new BufferedWriter(fWriter);
        PrintWriter printable = new PrintWriter(muscle);

        doc = bread.readLine();

        while(doc!=null)
        {
                StringTokenizer lineReader = new StringTokenizer(doc);

                drank = lineReader.nextToken();
                type = lineReader.nextToken();
                size = lineReader.nextToken();
                stock = lineReader.nextToken();
                infoLine = drank + type + size + stock;

                System.out.println(infoLine);

                doc = bread.readLine();
        }

         do
         {
            System.out.println ("Welcome to your cooler stock management application!\n\n");
            System.out.println ("Please select an option to manage your stock:\n\n");
            System.out.println ("1.  Add a DRAAAANK\n");
                System.out.println ("2.  Display a full list of your DRANKS\n");
            System.out.println ("3.  Update and Exit\n");

            selection = Keyboard.readInt();

            switch (selection)
            {
                case 1: System.out.println ("Enter your DRANK's name:\n");
                    drank = Keyboard.readString();
                    System.out.println ("Enter the type of DRAAAAANK (soda, juice, etc.):\n");
                    type = Keyboard.readString();
                    System.out.println ("Enter the size (in oz.) of your DRANK:\n");
                    size = Keyboard.readString();
                    System.out.println ("Enter the amount you have in stock of this DRANK!\n");
                    stock = Keyboard.readString();

                    coolerUno.stockDrank(drank, type, size, stock);
                    infoLine = drank + type + size + stock;

                    printable.print(infoLine);
                    printable.println();
                    break;

                case 2: System.out.println (coolerUno);
                    break;

                case 3: System.out.println ("Y'all set there? Y/N: \n");
                    chyea = Keyboard.readChar();
                    if (chyea == 'n'||chyea == 'N')
                        selection = 1;
                    else
                        selection = 3;

            }
        }
        while (selection != 3);
        muscle.flush();
        muscle.close();
    }
    catch(IOException exception)
    {
        System.out.println(exception.getMessage());
    }
 }
}

您的Filewriter不在附加模式下,這意味着他每次啟動程序時都會覆蓋File。 要啟用附加模式,請執行以下操作:

FileWriter fWriter = new FileWriter(file,true);

您的預期不對。 您具有選項“ 3. Update and Exit”,這意味着寫入所有更新並退出,並且您希望看到在每個步驟上寫的文件“ Y'all set there?Y / N:”。 這是一個矛盾。

您可以將輸出文件附加在“是的所有設置?是/否:”的每個“是”上,但是選項3僅是“退出”。

或者,您可以保留原樣,但不要期望文件在每個步驟中都會更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM