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