写入assets文件夹中的文件时出现错误文件号异常

时间:2012-08-27 07:47:12

标签: java android eclipse assets ioexception

我是Android编程的新手。我的问题是将任何信息写入assets文件夹中的文件。但是,Eclipse抛出了一个我不理解的异常:错误的文件号异常..这是我的代码:

package com.example.marport;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.marport.R;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Plakasorgulama extends Activity {

    final static String NAMESPACE = "http://webservice.marport.dcat.com/";
    final static String METHOD_NAME = "truckLicenceDate";
    final static String SOAP_ACTION = "http://webservice.marport.dcat.com/truckLicenceDate";
    final static String URL = "http://192.168.1.164:8070/test/services/MarportWebServicePort";

    private EditText plaka;
    private Button sorgu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plakasorgulama);

        plaka = (EditText) findViewById(R.id.etPlaka);
        sorgu = (Button) findViewById(R.id.bPlakaSorgulama);
        final SoapObject Request=new SoapObject(NAMESPACE,METHOD_NAME);

        sorgu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String plakainfo = plaka.getText().toString();
                SoapObject request = new SoapObject("licence", plakainfo); 
                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                soapEnvelope.dotNet=false;
                soapEnvelope.setOutputSoapObject(Request);
                HttpTransportSE aht=new HttpTransportSE(URL);

                try {
                      AssetFileDescriptor descriptor = getAssets().openFd("htmlcode.txt");
                      BufferedWriter out = new BufferedWriter(new FileWriter(descriptor.getFileDescriptor()));
                      out.write("tryyyyyyyyyyyyyyyyyyyyyy");
                      out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                try {
                    aht.call(SOAP_ACTION, soapEnvelope);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  try {
                    SoapPrimitive result=(SoapPrimitive) soapEnvelope.getResponse();
                } catch (SoapFault e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }
        });               
    }
}

此外,我尝试给出资产文件夹的直接路径。当我这样做时,这次FileNotFound异常来了..这个问题让我生病了。

这些是日志:

08-27 07:38:36.749: W/System.err(740): java.io.IOException: Bad file number
08-27 07:38:36.770: W/System.err(740):  at org.apache.harmony.luni.platform.OSFileSystem.writeImpl(Native Method)
08-27 07:38:36.770: W/System.err(740):  at org.apache.harmony.luni.platform.OSFileSystem.write(OSFileSystem.java:129)
08-27 07:38:36.800: W/System.err(740):  at java.io.FileOutputStream.write(FileOutputStream.java:297)
08-27 07:38:36.800: W/System.err(740):  at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:175)
08-27 07:38:36.800: W/System.err(740):  at java.io.OutputStreamWriter.close(OutputStreamWriter.java:151)
08-27 07:38:36.800: W/System.err(740):  at java.io.BufferedWriter.close(BufferedWriter.java:130)
08-27 07:38:36.800: W/System.err(740):  at com.example.marport.Plakasorgulama$1.onClick(Plakasorgulama.java:61)
08-27 07:38:36.800: W/System.err(740):  at android.view.View.performClick(View.java:2408)
08-27 07:38:36.800: W/System.err(740):  at android.view.View$PerformClick.run(View.java:8816)
08-27 07:38:36.800: W/System.err(740):  at android.os.Handler.handleCallback(Handler.java:587)
08-27 07:38:36.800: W/System.err(740):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 07:38:36.810: W/System.err(740):  at android.os.Looper.loop(Looper.java:123)
08-27 07:38:36.820: W/System.err(740):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-27 07:38:36.820: W/System.err(740):  at java.lang.reflect.Method.invokeNative(Native Method)
08-27 07:38:36.820: W/System.err(740):  at java.lang.reflect.Method.invoke(Method.java:521)
08-27 07:38:36.820: W/System.err(740):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-27 07:38:36.820: W/System.err(740):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-27 07:38:36.820: W/System.err(740):  at dalvik.system.NativeStart.main(Native Method)

非常感谢。

1 个答案:

答案 0 :(得分:3)

Assets文件夹中的文件仅以READ ONLY格式提供,您无法在其上书写。