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 回答