Android java:如何编写字符串文本并将其保存到txt.file中

时间:2014-03-12 02:24:16

标签: android

好的,到目前为止我已经成功创建了该文件夹。在CreateFolderActivity.java中 以下是创建文件夹代码:

btn_cFolder.setOnClickListener(new OnClickListener() {       
  public void onClick(View v) 
            {
             String dateN = edit_date.getText().toString();

             new AlertDialog.Builder(DatePickerActivity.this, AlertDialog.THEME_HOLO_DARK)  

                .setTitle("Create Folder")
                .setMessage("Confirm to create " + dateN +" folder ?")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) { 

                         Environment.getExternalStorageDirectory();
                         String dateN = edit_date.getText().toString();
                         edit_date.setTypeface(edit_date.getTypeface(), Typeface.BOLD_ITALIC);
                            File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/CalendarNote/" + dateN);
                            boolean success = true;
                                if (!folder.exists()) {
                                    success = folder.mkdirs();
                                }
                                if (success) {
                                        Toast.makeText(getBaseContext(), "You have successfully created." , Toast.LENGTH_LONG ).show();
                                        Intent w = new Intent(DatePickerActivity.this, SelectTypeActivity.class);
                                        startActivity(w);


                                } else {
                                    Toast.makeText(getBaseContext(), "You have Failed to create." , Toast.LENGTH_LONG ).show();
                                }

                    }
                 })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { 
                        // do nothing
                    }
                 })
                .setIcon(R.drawable.ic_launcher)
                 .show();
            }
   });

我也成功创建了txt文件。 但现在唯一的问题是路径位置我尝试了很多方法来连接这两个 将文件保存到文件夹中。到目前为止,我希望txt文件像这样保存/CalendarNote/TheDateIPick/hello.txt。

现在我只能保存到CalendarNote所在的文件夹中。 以下代码是我修改但未成功的代码。请帮帮我。

public void SaveListener() {         
    imb_savefile = (ImageButton) findViewById(R.id.imb_savefile);

    imb_savefile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

             edit_date = (EditText) findViewById(R.id.edit_date);

            String t = edit_title.getText().toString();
             new AlertDialog.Builder(WriteNoteActivity.this, AlertDialog.THEME_HOLO_DARK)
            .setTitle("Save Note")
            .setMessage("Confirm to save " + t +"?")
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) { 
             String content = edit_content.getText().toString();
             String title = edit_title.getText().toString();
             String dateN = edit_date.getText().toString();
             boolean success = true;
                 try {
                    File sdCardDir = Environment.getExternalStorageDirectory();  
                    File targetFile;
                    targetFile = new File(sdCardDir.getCanonicalPath()  
                            + "/CalendarNote/"+ dateN);
                    File file=new File(targetFile + "/"+title+".txt");

                    if(!targetFile.exists()){
                        success = targetFile.mkdir();  
                    }
                    RandomAccessFile raf = new RandomAccessFile(file, "rw");  
                    raf.seek(file.length());  

                    raf.write(content.getBytes());  
                    raf.close();  
                } catch (IOException e) {
                    e.printStackTrace();
                } 
                 if (success) {
                    Toast.makeText(getBaseContext(), "You have successfully created." , Toast.LENGTH_LONG ).show();



            } else {
                Toast.makeText(getBaseContext(), "You have Failed to create." , Toast.LENGTH_LONG ).show();
            }
                    //Toast.makeText(getBaseContext(), "Note have successfully saved." , Toast.LENGTH_LONG ).show();
                }
             })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 

                    Toast.makeText(getBaseContext(), "Note Cancelled." , Toast.LENGTH_LONG ).show();
                }
             })
            .setIcon(R.drawable.ic_launcher)
             .show();
        }

    });

}

1 个答案:

答案 0 :(得分:0)

是的,有可能。有关详细信息,请参阅此处:http://developer.android.com/guide/topics/data/data-storage.html#filesInternal