adding string to existing file

20 views
Skip to first unread message

pbellpi...@gmail.com

unread,
Oct 21, 2020, 1:22:03 PM10/21/20
to CodenameOne Discussions
Hi 
I like to add a string to an existing file in the DCIM  folder. The file with the first line is correctly made, but  the next lines not.

Here is the  code

native
mport java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import android.content.Context;


public class SaveposImpl {
    public void savestring(Integer w, String param) {
        
                  //String Filenm= "/storage/sdcard0/my_csv_file.csv";   
                String Filenm=   "/storage/emulated/0/DCIM/Track.csv";
                            
                     try{  
                            com.codename1.io.File  f=new com.codename1.io.File( Filenm );
                         if ( w==1) { 
                            if(f.exists()){
                              f.delete();
                              f.createNewFile();
                          } else {
                               f.createNewFile();

                            }
                        
                
        
                     boolean append = true;
          FileOutputStream fOut = new FileOutputStream(Filenm, append );
        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        osw.write(param);
        osw.flush();
        osw.close();  }  }
      catch (Exception e) {
       android.util.Log.d("failed to save file", e.toString());
      }
      
    }


import com.codename1.system.NativeInterface;

/**
 *
 * @author Administrator
 */

public interface Savepos extends NativeInterface {
    public void savestring(Integer w, String param);
}




in the application

   String param = w + ","+ Latp + ","+ Lonp + "," +  TOGl + "," + speed + "," + acc;
                          
                              Display.getInstance().invokeAndBlock(new Runnable() {
                                    public void run() {
                                     savepos1.savestring( w,param);
                                    }
                                });
                     

pbellpi...@gmail.com

unread,
Oct 21, 2020, 2:42:35 PM10/21/20
to CodenameOne Discussions
there  was a mistake in the code, but  still the string isn't appended. 

 public void savestring(Integer w, String param) {
        
                  //String Filenm= "/storage/sdcard0/my_csv_file.csv";   
                String Filenm=   "/storage/emulated/0/DCIM/Track.csv";
                            
                     try{  
                         
                  
                            com.codename1.io.File  f=new com.codename1.io.File( Filenm );
                         if ( w==1) { 
                            if(f.exists()){
                              f.delete();
                              f.createNewFile();
                          } else {
                               f.createNewFile();

                            } } 
                        
                 
        
                     boolean append = true;
          FileOutputStream fOut = new FileOutputStream(Filenm, append );
        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        osw.write(param);
        osw.flush();
        osw.close();    }
      catch (Exception e) {
       android.util.Log.d("failed to save file", e.toString());
      }
     
    }

Op woensdag 21 oktober 2020 om 19:22:03 UTC+2 schreef pbellpi...@gmail.com:

Peter Bell

unread,
Oct 21, 2020, 2:54:15 PM10/21/20
to codenameone...@googlegroups.com
oke i found the solution the     
 public void savestring(Integer w, String param) {
       
                  //String Filenm= "/storage/sdcard0/my_csv_file.csv";  
                String Filenm=   "/storage/emulated/0/DCIM/Track.csv";
                           
                     try{  
                         
                 
                            com.codename1.io.File  f=new com.codename1.io.File( Filenm );
                         if ( w==1) {
                            if(f.exists()){
                              f.delete();
                              f.createNewFile();
                          } else {
                               f.createNewFile();

                            } }
                       
         
       

    FileWriter fw = new FileWriter( Filenm,true); //the true will append the new data
    fw.write(param + "\n");//appends the string to the file
    fw.close();   }

      catch (Exception e) {
       android.util.Log.d("failed to save file", e.toString());
      }
     
    }

Op wo 21 okt. 2020 om 20:42 schreef pbellpi...@gmail.com <pbellpi...@gmail.com>:
--
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/KblxR5MC_vQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/c2f4140c-8dcd-4fc1-bfb5-00cbed7b01b6n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages