0

I try to write to a text file but I cannot view the file, and sometimes it does not even write.

The bit of code I am focusing on at the moment is:

String file_name = ".txt";
String title = ed1.getText().toString();
try {
      FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE);
      OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
      outputWriter.write("Job Title");
      outputWriter.write(ed1.getText().toString());
      outputWriter.close();
      Toast.makeText(getBaseContext(), "File saved successfully!",
      Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
      e.printStackTrace();
    }

Here are the errors coming out of the logcat:

07-25 12:22:39.775 4537-4537/? E/PhoneInterfaceManager: [PhoneIntfMgr] getIccId: ICC ID is null or empty.
07-25 12:22:40.628 1556-4236/? E/MotoSensors: Proximity covered 1
07-25 12:22:41.067 1556-4236/? E/MotoSensors: Proximity covered 2
07-25 12:22:41.563 1556-4275/? E/WifiScanningService: Got invalid work source request: WorkSource{} from ClientInfo[uid=10028]
07-25 12:22:41.585 1556-4275/? E/WifiScanningService: Got invalid work source request: WorkSource{} from ClientInfo[uid=10028]
07-25 12:22:41.790 639-639/? E/cnss-daemon: Stale or unreachable neighbors, ndm state: 16
07-25 12:22:42.774 1556-4467/? E/LocSvc_eng: E/Calling gnss_sv_status_cb
07-25 12:22:43.687 1556-4467/? E/LocSvc_eng: E/Calling gnss_sv_status_cb
07-25 12:22:43.775 1556-4467/? E/LocSvc_eng: E/Calling gnss_sv_status_cb
07-25 12:22:44.782 1556-4467/? E/LocSvc_eng: E/Calling gnss_sv_status_cb
07-25 12:22:44.802 1556-4467/? E/ActivityManager: Sending non-protected broadcast com.motorola.location.instrumentation from system 1556:system/1000 pkg android
                                                  java.lang.Throwable
                                                      at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18226)
                                                      at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:18826)
                                                      at android.app.ContextImpl.sendBroadcast(ContextImpl.java:926)
                                                      at com.android.server.location.GnssLocationProvider.reportStatus(GnssLocationProvider.java:1977)
07-25 12:22:58.150 639-639/? E/cnss-daemon: Stale or unreachable neighbors, ndm state: 4
07-25 12:22:58.937 639-639/? E/cnss-daemon: Stale or unreachable neighbors, ndm state: 4
07-25 12:23:00.610 1556-4236/? E/MotoSensors: Proximity uncovered
07-25 12:23:02.712 1556-5513/? E/LocSvc_libulp: E/int ulp_brain_transition_all_providers(), no QUIPC/GNSS transition logic run due to both engines are OFF 
07-25 12:23:03.126 1556-4236/? E/MotoSensors: Proximity covered 1
07-25 12:23:03.445 1556-4236/? E/MotoSensors: Proximity uncovered
07-25 12:23:04.093 639-639/? E/cnss-daemon: gateway mac address: 00:00:00:00:00:00
07-25 12:23:04.093 639-639/? E/cnss-daemon: Invalid mac address: 0xb0656e6cM

Please tell me what I am doing wrong! I tried to view the files on my computer from my phone as that is where the app is. But, no files are even saving to my phone's internal storage.

perror
  • 7,071
  • 16
  • 58
  • 85
  • Are you calling this code from a class that extends Activity or Application? – cjnash Jul 25 '17 at 17:47
  • Possible duplicate of [How To Read/Write String From A File In Android](https://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android) – tima Aug 19 '17 at 00:31

1 Answers1

0
 String file_name = ".txt";

 String title = ed1.getText().toString();
 try {
        FileOutputStream fos = openFileOutput(file_name, Context.MODE_PRIVATE);
        ObjectOutputStream of = new ObjectOutputStream(fos);
        of.writeObject(title);
        of.flush();
        of.close();
        fos.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

try this

Santhosh
  • 160
  • 7