简体   繁体   中英

Writing to text file in Java

I have an app in need of (probably a glance from) a seasoned Java pro. The engine works perfectly, but I cannot save data to a text file; moreover, I have to tether my tablet, uninstall the old app, copy the apk, and reinstall every time I debug file operations because I cannot write to disk in the emulator.

I added:

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

to the manifest outside of the application tag as well as tried numerous write-to-file methods. The exception is either (depending on the current attempt) eacces or file does not exist. I have also Googled all day to find the solution. Does anybody have any idea?

// this is a canned method that should presumably work
 private void writeStringToTextFile(String s, String f){
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath());
dir.mkdirs();
File file = new File(dir, f);
try{
    FileOutputStream f1 = new FileOutputStream(file,false); //True = Append to file, false = Overwrite
    PrintStream p = new PrintStream(f1);
    p.print(s);
    p.close();
    f1.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}   }


// this is the save method
// it has been modified a little less than a million times today
// some lines may be unnecessary because I have tried several approaches
  public void saveSystem(View view){
          String s="";
          String FILENAME = "data/data/TopExpertAndroid/" + fileName;
          FileOutputStream f;
          File file = new File(FILENAME);
          if(!(file.exists())){
             try{
                 file.createNewFile();} 
             catch(Exception e){
                showSplash(e.getMessage() + e.getCause(), "OK");
                return;}}
          try{
              getFile();
              if(fileName.equals("")){}
              else{
                   f = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                   for(int temp=0; temp<50; temp++){
                       for(int loop=0; loop<50; loop++){
                           s += topExpert.nodeBank[temp][loop].activationResponse + "\n";
                           s += topExpert.nodeBank[temp][loop].display + "\n";
                           s += topExpert.nodeBank[temp][loop].falseLink + "\n";
                           s += topExpert.nodeBank[temp][loop].identifier + "\n";
                           if(topExpert.nodeBank[temp][loop].isTop){
                              s += "true";}
                           else{
                                s += "false";}
                           s += "\n" + topExpert.nodeBank[temp][loop].trueLink + "\n";}}
                           writeStringToTextFile(s,FILENAME);
              }}
          catch(Exception e){
                showSplash(e.getMessage() + e.getCause(), "OK");}}

The FILENAME variable probably is missing a / at the beginning:

String FILENAME = "/data/data/TopExpertAndroid/" + fileName;

And you should probably use getDataDirectory or getExternalStorageDirectory instead...

But what you say about not being able to write to the disk of the emulator seems pretty weird...?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM