无法创建文件

时间:2014-02-26 23:34:35

标签: java android

我通过Intent将一个值从android传递给java文件。价值越来越高了。当我写system.out.print时,该值显示在log cat中,但是当我尝试在文件中写入时,文件不会被创建。我没有得到任何错误,但没有显示任何内容。我试图改变文件的位置,仍然无法正常工作。我有权将txt文件保存在任何地方,虽然我可以检查并更改位置。

代码:

            Intent i= new Intent(context, JavaServiceClass.class);
            i.putExtra("stock_list", temparr);
            context.startService(i);

JavaServiceClass.java

 public void onStart(Intent intent, int startId) {

                ArrayList<String> stock_list = (ArrayList<String>) intent.getExtras().get("stock_list");            
            //  System.out.println(stock_list.get(1) );

                writeInFile(stock_list.get(1));
}

如果我执行system.out.println,则会在log cat中显示值

 public void writeInFile(String stock_list )
            {
                File file = new File("F:/abc.txt");

                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                try {

                    BufferedWriter bw=new BufferedWriter(new FileWriter(file,true));

                    //for (int i1=0;i1<stock_list.length();i1++) {
                        bw.append(stock_list.toString());
                        bw.write("\t");
                    //}             
                    bw.newLine();
                    bw.close();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

Log Cat警告:

java.io.IOException: open failed: ENOENT (No such file or directory)
 java.io.File.createNewFile(File.java:940)
com.example.wifisignalstrength.JavaServiceClass.writeInFile(JavaServiceClass.java:100)
com.example.wifisignalstrength.JavaServiceClass.onStart(JavaServiceClass.java:42)
android.app.Service.onStartCommand(Service.java:438)
android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2373)
android.app.ActivityThread.access$1900(ActivityThread.java:128)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4517)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
dalvik.system.NativeStart.main(Native Method)
 Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
 libcore.io.Posix.open(Native Method)
libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)

2 个答案:

答案 0 :(得分:0)

您正在F驱动器上创建一个带有Windows路径的文件:

File file = new File("F:/abc.txt");

此文件永远不会存在且无法创建,Android是Linux。查看Context类(由Activity,Service类扩展),用于在设备的内部/外部存储中创建文件。例如,要在缓存目录中创建文件:

File file = new File(getCacheDir(), "abc.txt");

此外,您需要在finally块中关闭编写器,并且您也应该正确处理异常。

答案 1 :(得分:0)

请尝试以下代码。

public void writeInFile(String stock_list ) throws Exception
            {
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "abc.txt");
    file.createNewFile();

    //write the data into file
    if(file.exists())
    {
         OutputStream fo = new FileOutputStream(file);              
         fo.write(stock_list);
         fo.close();
         System.out.println("file created: "+file);

    }               

   }

不要忘记在清单中添加以下行。

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