0

Hi i have below code for write file, that read a and b file and put data in ab.xml file

   String filePath1 = "/sdcard/Dictionarys/a.txt";
String filePath2 = "/sdcard/Dictionarys/b.txt";
String filePath3 = "/sdcard/Dictionarys/ab.xml";


        try {

        File file = new File(filePath3);
        file.createNewFile();

        BufferedReader br1 = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath1), UTF8), BUFFER_SIZE);

        BufferedReader br2 = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath2), UTF8), BUFFER_SIZE);
        FileOutputStream file3 = new FileOutputStream(filePath3);
        OutputStreamWriter out3 = new OutputStreamWriter(file3,UTF8);
        BufferedWriter br3 = new BufferedWriter(out3, BUFFER_SIZE);

        String sCurrentLine1, sCurrentLine2;

        while ((sCurrentLine1 = br1.readLine()) != null && ((sCurrentLine2 = br2.readLine())!=null)) {
            String s3 = sCurrentLine2.substring(sCurrentLine1.length());
            br3.write("<abcd abc=\""+sCurrentLine1+"\" def=\""+s3.trim()+"\"/> \n");
            br3.flush();
            i++;
        }
        br3.write("<data>\n");
        br3.flush();
        br3.close();
        out3.flush();
        out3.close();
        file3.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

but xml file is not created on SDCARD path..

Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
  • have you given permission to Write External Storage? It can be easy to overlook sometimes, or else try following this http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder – Ajay Feb 12 '13 at 10:41
  • Please try not to use hard coded sdcard path. Because in some devices the sdcard path can be /mnt/sdcard0/. – dumbfingers Feb 12 '13 at 10:44

2 Answers2

2

Try getting Path this way:

 Environment.getExternalStorageDirectory()

Sometimes string path doesn't work and make sure you have following permissions added in the manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • Note that the documentation for [`Environment.getExternalStorageDirectory()`](http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()) indicates you should not use this method directly, but use `Context.getExternalFilesDir()` or `getExternalStoragePublicDirectory(String)` instead – nicopico Feb 12 '13 at 10:50
1

First, you shouldn't hardcode path to the sdcard, as it can change from phone to phone. Use method like Context.getExternalFilesDir() or getExternalStoragePublicDirectory(String) instead.

Second, some filesystems have buffering. You should use the fsync method to make the actual write occurs. See Saving Data Safely for more information.

nicopico
  • 3,606
  • 1
  • 28
  • 30
  • 1
    I upvoted your question because I do not feel it deserved the downvote, but it is not really a good practice to ask for this... You should also accept Usama Sarwar answer if it helped you – nicopico Feb 12 '13 at 11:05