Android:无法在文本文件上写一个字符串

时间:2015-09-09 10:01:20

标签: java android file ftp filewriter

我写文件时遇到问题。我的应用程序只需从SD卡中获取一个文件,在其上写一个字符串并将此文件上传到服务器ftp。上传文件是完美的,但我不能在文件上写字符串,我已经尝试过很多方式编写字符串,但没有人工作。 这是我的代码,Main和FTPUpload

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FtpUpload upload = new FtpUpload(handler,this);
    upload.execute();

}

private final Handler handler = new Handler() {
    public void handleMessage(Message message) {
        ArrayList result = (ArrayList) message.getData().get("data");

        for(Object fileName : result) {             
            Toast.makeText(getApplicationContext(), fileName.toString(), 
                    Toast.LENGTH_LONG).show();

        }
    }
};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

FTPupload:

public class FtpUpload extends AsyncTask<Void, Void, ArrayList> {

private Handler handler;
private Context context;

public FtpUpload(Handler hand, Context context) {
    this.handler = hand;
    this.context = context;

}

@Override
protected ArrayList doInBackground(Void... params) {

    ArrayList files = new ArrayList();
    FTPClient mFTPClient = new FTPClient();

    boolean status = false;

    String filename = "prova.txt";
    String srcFilePath = "/mnt/extSdCard/Download/";
    try {
        mFTPClient.setConnectTimeout(10 * 1000);
        mFTPClient.connect(InetAddress.getByName("+++++++++++++"));
        status = mFTPClient.login("**********", "*********");
        mFTPClient.changeWorkingDirectory("/");
        mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
        mFTPClient.enterLocalPassiveMode();
        File file = new File(srcFilePath + filename);

        final String TESTSTRING = new String("AAAAAAAAAAAAAAA");


        try {
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
             baos.write(TESTSTRING.getBytes());
              final FileOutputStream fos = new FileOutputStream(file);
              fos.write(baos.toByteArray());
              fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedInputStream buffIn = null;
        buffIn = new BufferedInputStream(new FileInputStream(file), 8192);

        status = mFTPClient.storeFile(filename, buffIn);

    } catch (Exception e) {
    }

    return files;
}

@Override
protected void onPostExecute(ArrayList result) {
    Message message = handler.obtainMessage();
    Bundle bundle = new Bundle();
    bundle.putStringArrayList("data", result);
    message.setData(bundle);
    handler.sendMessage(message);
}

}

我尝试过不同的写作方式,例如:

 FileOutputStream dfdfd = new FileOutputStream(file);
 OutputStreamWriter osw = new OutputStreamWriter(dfdfd);
 osw.write(TESTSTRING)

或:

try {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(file.getName(), context.MODE_PRIVATE));
    outputStreamWriter.write(TESTSTRING);
    outputStreamWriter.close();
}
catch (IOException e) {
    Log.e("Exception", "File write failed: " + e.toString());
} 

我添加了两个权限:

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

注意:应用程序没有错误,服务器上的文件上传工作正常

2 个答案:

答案 0 :(得分:0)

您是否尝试使用BufferedWriter而不是OutputStreamWriter?这对我有用(虽然不是在Android上,但请试一试,看看它是否有效)。

FileOutputStream dfdfd = new FileOutputStream(file);
BufferedWriter bw = new BufferedWriter(dfdfd);
bw.write(TESTSTRING);

记得在事后关闭它

bw.close();

答案 1 :(得分:0)

我不认为在SD卡上保存文件是正确的方法。 也许你应该尝试context.getdirs()context.getfiledirs()或context.getcachedirs()。因为Android建议app应该在/ sdcard / Android / packagenames /中使用自己的dirs。