不了解FileWriter类的使用

时间:2013-01-18 16:48:30

标签: java

 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。但是数据没有写入文件。我无法理解原因。需要帮助

5 个答案:

答案 0 :(得分:3)

FileOutputStream oFile = new FileOutputStream(yourFile,false);行会覆盖您的文件。删除它,它将工作

答案 1 :(得分: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);
    }
}

答案 2 :(得分:1)

尝试添加:

fileWrite.flush();

关闭FileWriter对象之前。

答案 3 :(得分:0)

我在现有文件中遇到过这个问题。检查您是否对现有文件具有写入权限。同样在文件创建时检查文件权限是否设置正确。

答案 4 :(得分: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();
    }