gpt4 book ai didi

java - 写入 Assets 文件夹中的文件时出现错误文件号异常

转载 作者:行者123 更新时间:2023-11-29 05:55:54 31 4
gpt4 key购买 nike

我是 Android 编程的新手。我的问题是将任何信息写入 Assets 文件夹中的文件。但是,Eclipse 抛出一个我不明白的异常:Bad file number exception..这是我的代码:

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

我还尝试提供 Assets 文件夹的直接路径。当我这样做时,这次出现了 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)

非常感谢。

最佳答案

Assets 文件夹中的文件只能以READ ONLY 格式使用,您不能在上面写入。

关于java - 写入 Assets 文件夹中的文件时出现错误文件号异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12138064/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com