2016-04-28 7 views
0

ユーザー入力によるテキストファイルの更新に問題があります。テキストファイルを更新するためのロジック問題 - Java

私は、製品情報を含むテキストファイル(ID;名;コスト;証券)を持っている:

001;Hand Soap;2.00;500 

製品を追加するためには、ユーザがいる場合、製品を更新する、いずれの順序で機能addProductを呼び出します製品名が既にファイルに存在するか、またはテキストファイルがまだ存在しない場合は、テキストファイルに追加されます。私は2つのことがわかりません:1回だけ追加する方法(それが読んでいるすべての行に追加されている瞬間)と空のテキストファイルを処理する方法。単純なテキストファイルには、この使用のために最良の選択ではないかもしれない、ハイレベルで

public void addProduct(Product product, int amountReceived) throws FileNotFoundException, IOException { 
    newProduct = product; 
    String productParams = newProduct.getProduct(); 
    String productID = newProduct.getProductID(); 
    int productStock = newProduct.getProductStock(); 
    String productName = newProduct.getProductName(); 
    String tempFileName = "tempFile.txt"; 

    System.out.println("Attempting to Add Product : " + newProduct.getProduct()); 

    BufferedReader br = null; 
    BufferedWriter bw = null; 

    try { 
     FileInputStream fstream = new FileInputStream(ProductMap.productFile); 
     br = new BufferedReader(new InputStreamReader(fstream)); 
     String line; 
     StringBuilder fileContent = new StringBuilder(); 

     while ((line = br.readLine()) != null) { 
      System.out.println("Line : " + line); 
      String [] productInfo = line.split(";"); 
      System.out.println("Added Product Info length : " + productInfo.length); 
      if (productInfo.length > 0) { 
       if (productInfo[1].equals(productName)) 
       { 
        System.out.println("Adding existing product"); 
        System.out.println("Product Info : " + String.valueOf(productInfo[3])); 
        //line = line.replace(String.valueOf(productInfo), String.valueOf(productStock - amountSold)); 
        productInfo[3] = String.valueOf(Integer.parseInt(productInfo[3]) + amountReceived); 
        String newLine = productInfo[0] + ";" + productInfo[1] + ";" + productInfo[2] + ";" + productInfo[3]; 
        fileContent.append(newLine); 
        fileContent.append("\n"); 

        System.out.println("Updated Product Info : " + String.valueOf(Integer.parseInt(productInfo[3]) + amountReceived)); 
        System.out.println("Line :" + newLine); 
       } else { 
        fileContent.append(line); 
        fileContent.append("\n"); 
        fileContent.append(productParams); 
        fileContent.append("\n"); 
        //fileContent.append(productParams + "\n"); 
        //System.out.println("Product Name : " + productInfo[1]); 
        //System.out.println("The full product info : " +productParams); 
       } 

      } 
      br.readLine(); 
     } 

     if (br.readLine() == null) { 
      fileContent.append(productParams); 
     } 


     System.out.println("Product Updated File Contents : " + fileContent); 
     FileWriter fstreamWrite = new FileWriter(ProductMap.productFile); 
     BufferedWriter out = new BufferedWriter(fstreamWrite); 
     System.out.println("File Content : " + fileContent); 
     out.write(fileContent.toString()); 
     out.close(); 

     in.close(); 
    } catch (Exception e) { 
     System.err.println("Error: " + e.getMessage()); 
    } 
} 

答えて

0

これはaddProductがどのように見えるかです。この実装では、ファイル全体を保持するのに十分なメモリが必要です。

追加だけがあって、ファイルに直接追加することができれば、もっと簡単になります。データベースが最善の選択であるように思えます。データベースとシンプルなテキストファイルの間のどこかに、RandomAccessFileが、各フィールドの標準長でデータを書き込むことができる場合に役立ちます。その後、ファイル全体を書き直すのではなく、特定の行を上書きすることができます。

現在の設定の制約から、ファイルが更新されるたびにすべてのデータを書き込む方法は考えられません。

空のファイルの問題を回避するには、現在のループのelse条件をスキップして、新しいデータがfileContent StringBufferに追加されないようにします。その後、データを書き戻すときに、ファイルから他の情報の前または後に新しいデータを書き込むことができます。

また、ループの最後にあるreadLineは必要ありません。ループの最下部で読み取られた行はスキップされ、ループの先頭で次の行が読み取られると実際には処理されません。