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 6191599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:47:39+00:00 2026-05-24T02:47:39+00:00

I am generating csv file and it force close, why? my code is package

  • 0

I am generating csv file and it force close, why?
my code is

   package contactlist.pkg;

       import java.io.FileWriter;
       import java.io.IOException;

     public class GenerateCsv
   {
 public static void main(String [] args)
 {
   generateCsvFile("C:\\test.csv"); 
 }

   private static void generateCsvFile(String sFileName)
   {
 try
  {
    FileWriter writer = new FileWriter(sFileName);

    writer.append("DisplayName");
    writer.append(',');
    writer.append("Age");
    writer.append('\n');

    writer.append("MKYONG");
    writer.append(',');
    writer.append("26");
        writer.append('\n');

    writer.append("YOUR NAME");
    writer.append(',');
    writer.append("29");
    writer.append('\n');

    //generate whatever data you want

    writer.flush();
    writer.close();
}
catch(IOException e)
{
     e.printStackTrace();
} 
   }
}

errors are

   07-27 13:12:30.508: ERROR/AndroidRuntime(939): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{contactlist.pkg/contactlist.pkg.GenerateCsv}: java.lang.ClassCastException: contactlist.pkg.GenerateCsv
   07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
    07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
      07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.os.Handler.dispatchMessage(Handler.java:99)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.os.Looper.loop(Looper.java:123)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.main(ActivityThread.java:3683)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at java.lang.reflect.Method.invokeNative(Native Method)
       07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at java.lang.reflect.Method.invoke(Method.java:507)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
      07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
         07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at dalvik.system.NativeStart.main(Native Method)
           07-27 13:12:30.508: ERROR/AndroidRuntime(939): Caused by: java.lang.ClassCastException: contactlist.pkg.GenerateCsv
         07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
       07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
  • 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-05-24T02:47:40+00:00Added an answer on May 24, 2026 at 2:47 am
    package contactlist.pkg;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class MainActivity extends Activity {
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            String dir = "/Android/data/contactlist.pkg/csv/";
            String fullDir = Environment.getExternalStorageDirectory().toString() + dir;
            generateCsvFile(fullDir, "data.csv");
        }
    
        private static void generateCsvFile(String dir, String fileName) {
            try {
    
                File theDir = new File(dir);
                theDir.mkdirs();
    
                FileWriter writer = new FileWriter(dir + fileName);
    
                writer.append("DisplayName");
                writer.append(',');
                writer.append("Age");
                writer.append('\n');
    
                writer.append("MKYONG");
                writer.append(',');
                writer.append("26");
                writer.append('\n');
    
                writer.append("YOUR NAME");
                writer.append(',');
                writer.append("29");
                writer.append('\n');
    
                //generate whatever data you want
    
                writer.flush();
                writer.close();
            } catch (IOException e) {
                Log.i("file", e.getMessage());
            }
        }
    }
    

    String dir = "/Android/data/contactlist.pkg/csv/"; is just and example of how you should structure your application folder in the external storage, it should be like this: /Android/data/<package_name>/files/, and you will need to add the permission on the AndroidManifest.xml, directly in manifest tag.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    And I humbly suggest you to read this Android Data Storage documentation 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am generating a csv (comma separated value) file with Java Server Pages (JSP).
I am generating a simple csv file using php. The file contains some user's
I am generating a csv file which has the link to specific files. It
I am generating a CSV file from Ruby. The problem is a column string
I am using C++ and I am generating a csv file to report some
I'm generating a csv file using php, now some columns contain a paragraph with
I'm generating the csv file with the following header commands: header(Content-type: text/csv; charset=utf-8; encoding=utf-8);
Have a program that's dynamically generating an Excel file and a csv. The excel
I am generating a CSV file but the people who are processing these file
I'm generating some xml files that needs to conform to an xsd file that

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.