Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified
Accepted Solution

create CSV file

Hello friends,

I want to generate .csv file. & write data in it.

so can any one suggest any idea for it ?

 

Thanks & Regards

           Milan

Trusted Contributor
Posts: 126
Registered: ‎10-15-2010
My Device: Torch 9850
My Carrier: Not specified

Re: create CSV file

Try this...

 

DataOutputStream os = null;
        FileConnection fconn = null;
        String FileName = "sample.csv";
        try {
            fconn = (FileConnection) Connector.open("file:///SDCard/BlackBerry/"+FileName, Connector.READ_WRITE);
            if (!fconn.exists())
                fconn.create();
            os = fconn.openDataOutputStream();
            String Contact_Title ="First Name , Last Name , Email1 , Email2 , Organization , Title , Tel Work \n";                              
            os.write(Contact_Title.getBytes());
        }catch (Exception e) {  
            System.out.println("Con :"+e.getMessage());
        }finally {
            try {
                if (null != os)
                    os.close();
                if (null != fconn)
                    fconn.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

Developer
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: create CSV file

Hi,
Thanks For reply.
It's really helpful to me.

Thanks & Regards
Developer
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: create CSV file

Hi,
I am now able to create .csv file.
First time I was successfully added records.
But on second time when I try to add new record it overlapped on old records.
I tried lot but I was not able to add new records in new line.
If any idea help me.

Thanks & Regards
]
Developer
Posts: 95
Registered: ‎10-19-2010
My Device: Blackberry 8110
My Carrier: Generic

Re: create CSV file

DataOutputStream os = null;
DataInputStream is = null;
FileConnection fconn = null;
StringBuffer raw =  null;
String FileName = "sample.csv";
try {
  fconn = (FileConnection) Connector.open("file:///SDCard/BlackBerry/"+FileName, Connector.READ_WRITE);
  if (!fconn.exists()) { //Create the file with a first line
    fconn.create();
    os = fconn.openDataOutputStream();
    String Contact_Title ="First Name , Last Name , Email1 , Email2 , Organization , Title , Tel Work \n";  
    os.write(Contact_Title.getBytes());
  }
  else { //We append a new line of information
    is = fconn.openDataInputStream();
    os = fconn.openDataOutputStream();
    raw = new StringBuffer();
    byte[] data = new byte[256];
    int len = 0;
    raw = new StringBuffer();
    while ( -1 != (len = is.read(data))) {
      raw.append(new String(data, 0, len));
    }
    String Contact_Title ="First Name , Last Name , Email1 , Email2 , Organization , Title , Tel Work \n";  
    raw.append(Contact_Title);
    os.write(raw.toString().getBytes());
  }
}
catch (Exception ignored) {}
finally {
try {
  if (null != os)
  os.close();
  if (null != fconn)
    fconn.close();
  } 
  catch (IOException e1) {
    e1.printStackTrace();
  }
}
}

 Is this what you want?

Developer
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: create CSV file

Hi,
Thanks for your reply.
I had done the same.
but actually what I want is that I do not want to write the same data again which is already in the file.

you can do the same like this also

InputStream in = fconn.openDataInputStream();

byte[] inputdata = IOUtilities.streamToBytes(in);
String result = new String(inputdata);

Thanks & Regards
Developer
Posts: 95
Registered: ‎10-19-2010
My Device: Blackberry 8110
My Carrier: Generic

Re: create CSV file

String FileName = "sample.csv";
String Contact_Title ="Text to append\n";  
try {
  fconn = (FileConnection) Connector.open("file:///SDCard/BlackBerry/"+FileName, Connector.READ_WRITE);
  if (!fconn.exists()) {
    fconn.create();
  }
       	   	
  OutputStream out =fconn.openOutputStream(fconn.fileSize());
  out.write(Contact_Title.getBytes());
  out.flush();
  out.close();
}
catch (Exception ignored) {}
finally {
     	   //write the information in the file
  try {
    if (null != os)
      os.close();
    if (null != fconn)
      fconn.close();
  } catch (IOException e1) {}
}

 This way you don't rewrite the information in the file, you just append the new text to the file.

Developer
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: create CSV file

Hi,Thanks for reply.

& also for help me to solve my issue.

 

Thanks & Regards

Trusted Contributor
Posts: 126
Registered: ‎10-15-2010
My Device: Torch 9850
My Carrier: Not specified

Re: create CSV file

Hai milanvishal

 

please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!

Developer
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: create CSV file

hi,
now is it fine ?

Thanks & Regards