Java 如何写入文本文件-HttpConnectionJ2ME

Java 如何写入文本文件-HttpConnectionJ2ME,java,java-me,midp,httpconnection,Java,Java Me,Midp,Httpconnection,我没有收到任何错误,但我的代码没有将任何新数据保存到文本文件中,以下是我的代码: public void saveToLeaderboard() throws IOException { String toSave = "Random info I want to save"; HttpConnection connection = null; connection = (HttpConnection) Connector.open("EXTER

我没有收到任何错误,但我的代码没有将任何新数据保存到文本文件中,以下是我的代码:

public void saveToLeaderboard() throws IOException {
        String toSave = "Random info I want to save";
        HttpConnection connection = null;

        connection = (HttpConnection) Connector.open("EXTERNAL URL", Connector.READ_WRITE, true);
        connection.setRequestMethod(HttpConnection.POST);

        DataOutputStream out = new DataOutputStream(connection.openDataOutputStream());
        out.writeUTF(toSave);        

        out.flush();
        connection.close();
    }

我做错了什么?

我在您的代码中看不到任何FileOutputStream。DataOutputStream应该如何写入文件?

要实际触发HTTP请求,您需要获取有关HTTP响应的任何信息,例如使用URLConnection.getInputStream或URLConnection.getResposeCode等的响应正文

有关更多详细信息,请参阅。

一天结束时我自己的解决方案:

在我的例子中,我创建了一个服务器端文件,它是一个.Net文件[.aspx],我设置了我的外部url,用url变量调用我的.Net页面,这些url变量保存了我所需的数据

在我的案例C中,使用服务器代码对txt文件进行了基本的保存

public void saveToLeaderboard() throws IOException {
        String toSave = "Data to save";
        InputStream inputStream = null;
        HttpConnection connection = (HttpConnection) Connector.open("External Url" + "?info=" + toSave, Connector.READ_WRITE, true);
        connection.setRequestMethod(HttpConnection.POST);
        inputStream = connection.openInputStream();
        inputStream.close();
        connection.close();
    }

最好尝试下面的代码片段

public void saveToLeaderboard() {

    String toSave = "Random info I want to save";
    HttpConnection connection = null;
    int response_code=HttpConnection.HTTP_OK-1;
    FileConnection fc=null;
    DataOutputStream dos=null;
    DataOutputStream out
    InputStream dis=null; 
    try
    {    
    connection = (HttpConnection) Connector.open("EXTERNAL URL");
    connection.setRequestMethod(HttpConnection.POST);

     out= connection.openDataOutputStream();
    out.writeUTF(toSave);        

    out.flush();
    response_code=connection.getResponseCode();

    if(response_code==HttpConnection.HTTP_OK)
    {
       dis=connection.openDataInputStream();
       int ch;

       fc=(FileConnection)Connector.open(path,Connector.READ_WRITE); //path is a path of the file to be saved 
       fos=fc.openDataOutputStream(); byte temp; while((ch=dis.read()) !=-1) 
       { 
           temp=(byte)ch; fos.write(temp); 
       } 
       fos.flush(); 
     } 
      else 
       { //Status code not ok }

    }
    catch(ConnectionNotFoundException cnex){//Connection not found} 
    catch(IOException cnex){//ioe exception} 
    catch(Exception cnex){//exception} 


    if(out!=null)
    {
       try{
           out.close();
       }
       catch(Exception cnex){//exception} 
    }  
    if(dis!=null)
    {
       try{
           dis.close();
       }
       catch(Exception cnex){//exception} 
    }  
    if(connection!=null)
    {
       try{
           connection.close();
       }
       catch(Exception cnex){//exception} 
    }  
   if(fos!=null)
    {
       try{
           fos.close();
       }
       catch(Exception cnex){//exception} 
    }  

很好,但那是我的建议。您需要从您的连接中获取输入流或响应代码。无论如何,谢谢,但您建议了我可以使用的流的方法或结构。一天结束时,我保留了相同的代码,但将url更改为指向服务器端C页面,而不是直接编辑文本文件。