Validating and reading a Word file in Android

前端 未结 6 1773
忘了有多久
忘了有多久 2020-12-05 16:19

I want to be able to access a word file in the sdcard of the user\'s phone, and the file will be chosen by the user. I want to be able to process the file, i.e. read its con

6条回答
  •  执念已碎
    2020-12-05 17:05

    Your question needs some more clarification

    Anyway this is what you should do to edit the file

    1. Read the file

      File sdcard = Environment.getExternalStorageDirectory();
      File file = new File(sdcard,"theFILE");
      StringBuilder content = new StringBuilder();
      try {
          BufferedReader br = new BufferedReader(new FileReader(file));
          String line;
          while ((line = br.readLine()) != null) {
              content.append(line);
              content.append('\n');
          }
      }
      catch (IOException e) {
      }
      
    2. Edit the content

      content.append("bla bla");
      content.append("kaza maza");
      
    3. Save the file

      FileOutputStream f = new FileOutputStream(file);
      f.write(content.toString().toCharArray());
      

    Remember to add to your manifest the permissions to write to the sdcard:

    
    

    Validation

    Unfortunately this is the hardest part. Apache POI is a choice; however it is a large library that does not guarantee the right behavior.

    The perfect solution is to check the MS-DOC format. It is a really big requirement. For such a limited application, I guess you won't find anything if you search the web. Therefore, you will have to implement a reader that abides by this format.

提交回复
热议问题