2015-02-10 87 views
0

我目前正在尝试将字符串“testing”添加到inventory.txt中,以查看我的文件是否正在成功追加。出于某种原因,它不会追加!它编译并运行成功,但“测试”从未出现在我的文件中。我在网上做了一堆谷歌搜索,我认为我做对了....有人可以尝试并帮我找到我的错误?谢谢!无法附加到Java文件?

/** 
Add in javadoc comments 
*/ 


//import statements 
import java.io.*; 
import java.util.*; 

public class Try { 
    public static void main(String[] args){ 
     //variables 
     Scanner kb = new Scanner(System.in); 
     boolean valid = false; 
     int mainSelect = 0; 
     int itemOption = 0; 
     boolean valid2 = false; 
     boolean returnToMain = false; 
     String theName = ""; 
     double thePrice = 0; 
     int theQuantity = 0; 
     double currBal; 
     String lampName; 
     double lampPrice; 
     int lampQuantity; 
     String chairName; 
     double chairPrice; 
     int chairQuantity; 
     String deskName; 
     double deskPrice; 
     int deskQuantity; 
     int sellAmnt; 
     boolean valid3 = false; 

     //create file 
     try{ 
      PrintWriter outputFile = new PrintWriter("inventory.txt"); 
      outputFile.println("3000.0"); 
      outputFile.println("Lamps 15.3 400"); 
      outputFile.println("Chairs 19.95 250"); 
      outputFile.println("Desks 95.0 300"); 
      outputFile.close(); 
     } 
     catch(IOException e){ 
      System.out.println("File cannot be created."); 
     } 

     //read data in from file 
     try{ 
      File file = new File("inventory.txt"); 
      Scanner inFile = new Scanner(file); 
      currBal = inFile.nextDouble(); 
      lampName = inFile.next(); 
      lampPrice = inFile.nextDouble(); 
      lampQuantity = inFile.nextInt(); 
      chairName = inFile.next(); 
      chairPrice = inFile.nextDouble(); 
      chairQuantity = inFile.nextInt(); 
      deskName = inFile.next(); 
      deskPrice = inFile.nextDouble(); 
      deskQuantity = inFile.nextInt(); 
      inFile.close(); 

      //present user with main menu 
      do{ 
      System.out.println("Current Balance: $" + currBal); 
      System.out.println("\t1. " + lampName + "\t\t(" + lampQuantity + " at $" + lampPrice + ")"); 
      System.out.println("\t2. " + chairName + "\t\t(" + chairQuantity + " at $" + chairPrice + ")"); 
      System.out.println("\t3. " + deskName + "\t\t(" + deskQuantity + " at $" + deskPrice + ")"); 
      System.out.println("\t0. Exit"); 

      while(valid == false){ 
       System.out.print("\nPlease enter choice: "); 
       try{ 
        mainSelect = kb.nextInt(); 
        if(0 <= mainSelect || mainSelect >= 3){ 
         valid = true; 
        } 
        else{ 
         System.out.println("That is not a valid selection. Try again."); 
        } 
       } 
       catch(InputMismatchException ime){ 
        System.out.println("That is not a valid selection. Try again."); 
        kb.next(); 
       } 
      } 
      //present user with second menu 
      switch(mainSelect){ 
       case 1: 
        theQuantity = lampQuantity; 
        thePrice = lampPrice; 
        theName = lampName; 
        break; 
       case 2: 
        theQuantity = chairQuantity; 
        thePrice = chairPrice; 
        theName = chairName; 
        break; 
       case 3: 
        theQuantity = deskQuantity; 
        thePrice = deskPrice; 
        theName = deskName; 
        break; 
       case 0: 
        System.exit(0); 
        break; 
      } 
      System.out.println("\nCurrent balance: $" + currBal); 
      System.out.println("Current Quantity: " + theQuantity); 
      System.out.println("Current price: $" + thePrice); 
      System.out.println("1. Sell " + theName); 
      System.out.println("2. Buy " + theName); 
      System.out.println("3. Change price"); 
      System.out.println("0. Return to main menu"); 

      while(valid2 == false){ 
       System.out.print("\nPlease enter choice: "); 
       try{ 
        itemOption = kb.nextInt(); 
        if(0 <= itemOption || itemOption >= 3){ 
         valid2 = true; 
        } 
        else{ 
         System.out.println("That is not a valid selection. Try again."); 
        } 
       } 
       catch(InputMismatchException ime){ 
        System.out.println("That is not a valid selection. Try again."); 
        kb.next(); 
       } 
      } 
      //Action: sell 
      if(itemOption == 1){ 
       do{ 
        System.out.print("Amount to sell (current quantity: " + theQuantity + "): "); 
        sellAmnt = kb.nextInt(); 
        returnToMain = true; 
        try{ 
         sellAmnt = kb.nextInt(); 
         if(0 <= sellAmnt || sellAmnt >= theQuantity){ 
          valid3 = true; 
          try{ 
           //append file 
           FileWriter fw = new FileWriter("inventory.txt", true); 
           PrintWriter pw = new PrintWriter(fw); 
           pw.write("testing"); //not working!!!!!!!!!! 
           pw.close(); 
          } 
          catch(IOException e){ 
           System.out.println("File not found."); 
          } 
         } 
         else{ 
          System.out.println("\nThat amount is not within quantity bounds. Please enter a number within bounds."); 
         } 
        } 
        catch(InputMismatchException ime){ 
         System.out.println("That is not a valid number. Try again."); 
         kb.next(); 
        } 
       }while(valid3 == false); 
      } 
      //Action: buy 
      if(itemOption == 2){ 
       returnToMain = true; 
      } 
      //Action: change price 
      if(itemOption == 3){ 
       returnToMain = true; 
      } 

      }while(returnToMain == true); 
     } 
     catch(FileNotFoundException e){ 
      System.out.println("Cannot find file."); 
     } 
    } 
} 
+1

代码太多,只是向我们展示与您的问题最相关的部分 – 2015-02-10 20:34:39

+0

您的代码是否正在执行追加?在这之前我可以看到有几个条件。 – 2015-02-10 20:38:23

+0

请按照其他评论者的建议,并a)提供一个最小的代码片段,将重现失败。很可能你会在这个过程中解决你的问题,并且b)验证你写入“测试”应该写入的块的条件。添加一些日志以检查您的代码是否已达到该目标,或者使用调试器来执行该操作。 – h7r 2015-02-10 21:19:19

回答

1

这里:

//create file 
try{ 
    PrintWriter outputFile = new PrintWriter("inventory.txt"); 
    outputFile.println("3000.0"); 
    outputFile.println("Lamps 15.3 400"); 
    outputFile.println("Chairs 19.95 250"); 
    outputFile.println("Desks 95.0 300"); 
    outputFile.close(); 
} 
catch(IOException e){ 
    System.out.println("File cannot be created."); 
} 

你总是重新创建该文件,并添加内容到它,然后保存它。无论你如何处理文件,每次执行应用程序时都会重写。

作为建议,只有在您的文件不存在的情况下才应该执行这段代码。

+0

这看起来并不准确,因为他正在用'append'参数设置为'true'创建'FileWriter'。 – h7r 2015-02-10 21:11:29

+0

@ h7r即使您在'PrintWriter'的构造函数中使用'boolean'参数来追加内容,我无法找到在基本文件不存在的情况下继续添加此信息的原因。为了将来使用这个文件,是的,它应该使用'new PrintWriter(“inventory.txt”,true);'打开。 – 2015-02-10 21:13:18

+0

可能是,但a)他的问题不是关于风格或良好的做法b)正如我所说,它是正在被打开的'FileWriter'。 'PrintWriter'没有'append'标志,但是'autoFlush'。 http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#PrintWriter%28java.io.Writer,%20boolean%29 – h7r 2015-02-10 21:17:01