Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:19:19+00:00 2026-06-17T12:19:19+00:00

import java.util.*; import java.io.*; public class ProductInfoDwnld{ static String abc; public static void addProductToCart(String

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

Using the code below I am trying to write into the file 001.txt using FileWriter class. But the data is not being written into the file. I am not able to understand the reason. Need help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:19:20+00:00Added an answer on June 17, 2026 at 12:19 pm

    Reformatted your code a touch:

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class InversionCounter { public static void main(String[]
import java.util.Arrays; public class Test { public static void main(String... args) { String[] strings
import java.util.Scanner; public class Main extends Hashmap{ public static void main(String[] args) { Hashmap
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
import java.util.*; public class oddNumbers { public static void main(String[] args) { System.out.println(enter two
import java.util.Scanner; public class feetToMeters { public static void main (String [] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.