1
 import java.util.*;
 import java.io.*;
public class ProductInfoDwnld{
static String abc;
public static void addProductToCart(String a_CartId, String a_productId, String a_desc, String a_price){
    BufferedWriter bw = null;
    try{
        File yourFile = new File(a_CartId);
        // Check if yourFile exists
        if(!yourFile.exists())
            yourFile.createNewFile(); // Create a new yourFile if it does not exist
        else{
            try {
                FileWriter fileWrite = new FileWriter(yourFile,true);
                fileWrite.write(a_productId + " " + a_desc + " " + a_price); // write(String str);
                fileWrite.close();
            }catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        FileOutputStream oFile = new FileOutputStream(yourFile,false);
    }catch(Exception e){
        System.out.println("Error");
    }
}
public static void main(String[] args){
    String CartID = "001.txt";
    String productId="001", Desc = "Laptop", price="20000Rs";
    addProductToCart(CartID,productId,Desc,price);
}
}      

以下のコードを使用して、FileWriter クラスを使用してファイル 001.txt に書き込もうとしています。しかし、データはファイルに書き込まれていません。理由がわかりません。助けが必要

4

5 に答える 5

3

この行FileOutputStream oFile = new FileOutputStream(yourFile,false);はファイルを上書きしています。それを削除し、それは動作します

于 2013-01-18T16:58:31.630 に答える
2

コードを少し再フォーマットしました:

import java.util.*;
import java.io.*;
public class ProductInfoDwnld{
    static String abc;
    public static void addProductToCart(String a_CartId, String a_productId, String a_desc, String a_price){
        BufferedWriter bw = null;
        try{
            File yourFile = new File(a_CartId);
            // Check if yourFile exists
            if(!yourFile.exists()) {
                yourFile.createNewFile(); // Create a new yourFile if it does not exist
            }

                    // Note you had an else block here:
                    // If the file didn't exist you only would create it
                    //  and not write any data to it.
                    // If the file exists you'd write data to it.
                    // I removed the else block

            try {
                FileWriter fileWrite = new FileWriter(yourFile,true);
                fileWrite.write(a_productId + " " + a_desc + " " + a_price); // write(String str);
                fileWrite.flush();
                fileWrite.close();
            }catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }catch(Exception e){
            System.out.println("Error");
        }
    }
    public static void main(String[] args){
        String CartID = "001.txt";
        String productId="001", Desc = "Laptop", price="20000Rs";
        addProductToCart(CartID,productId,Desc,price);
    }
}
于 2013-01-18T16:53:38.450 に答える
1

追加してみてください:

fileWrite.flush();

FileWriter オブジェクトを閉じる前に。

于 2013-01-18T16:50:07.340 に答える
0

私は既存のファイルで一度その問題に直面しました。既存のファイルに対する書き込み権限があることを確認してください。また、ファイルの作成時に、ファイルのアクセス許可が正しく設定されていることを確認してください。

于 2013-01-18T16:51:56.583 に答える
0

else ループ内でファイルの書き込みが行われるのはなぜですか。ファイルが存在しない場合、最初の反復はファイルを作成して終了します。それは実際の書き込みを行いません。これを試して:

if(!yourFile.exists())
        yourFile.createNewFile(); // Create a new yourFile if it does not exist

    try {
        FileWriter fileWrite = new FileWriter(yourFile,true);
        fileWrite.write(a_productId + " " + a_desc + " " + a_price); // write(String str);
        fileWrite.close();
    }catch (IOException ioe) {
        ioe.printStackTrace();
    }
于 2013-01-18T17:03:17.400 に答える