生成csv文件强制关闭android

时间:2011-07-27 07:46:50

标签: android

我正在生成csv文件并强制关闭,为什么? 我的代码是

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

错误

   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)

3 个答案:

答案 0 :(得分:3)

  

引起:java.lang.ClassCastException:contactlist.pkg.GenerateCsv

在android中,你没有main方法,而是实现一个活动,服务或接收器并覆盖它的一些方法(例如onCreate())。请参阅基本的android教程:Hello world

答案 1 :(得分:3)

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/";只是您应该如何在外部存储中构建应用程序文件夹的示例,它应该是这样的:/Android/data/<package_name>/files/,您需要在AndroidManifest.xml上添加权限,直接在manifest标签中。

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

我谦卑地建议你阅读这篇Android Data Storage文档:)

答案 2 :(得分:2)

您需要了解有关Android中文件结构的更多信息。没有C:驱动器只有你写的SD卡。使用getExternalStorageDirectory获取正确的文件路径,然后不要忘记添加WRITE_SDCARD权限。