簡體   English   中英

不了解FileWriter類的使用

[英]Not understanding the use of FileWriter class

 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。 但是沒有將數據寫入文件中。 我不明白原因。 需要幫忙

這行FileOutputStream oFile = new FileOutputStream(yourFile,false); 正在覆蓋您的文件。 刪除它會起作用

重新格式化您的代碼:

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);
    }
}

嘗試添加:

fileWrite.flush();

關閉FileWriter對象之前。

我曾經用現有文件遇到過這個問題。 檢查您是否對現有文件具有寫權限。 另外,在創建文件時,請檢查文件權限設置是否正確。

為什么在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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM