Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with writing data into a file  RSS feed

 
vikram nalagampalli
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks in advance for the help,
I am retreving ProvId, Role, SSN, Lname,Fname from backend.
I need to write the above mentioned Strings in this order into an AscII file in one line since i will getting multiple Strings of each mentioned above.
prov_id��� char(12)
��� role_cd��� char(1)
��� SSN_NUM��� char(9)
��� last_nam��� char(30)
��� fst_nam��� char(20)
Currently since i have all the above mentioned strings in array list i am doign the following
ArrayList list = o.listSSN();
Iterator iter = list.iterator();
while (iter.hasNext())
{
String portalID = (String)iter.next();
String SSN = (String)iter.next();
String provID = (String)iter.next();
String role = (String)iter.next();

String lName = demo.getLastName(portalID);
String fName = demo.getFirstName(portalID);

buf = new StringBuffer();
buf.append(provID);
buf.append(role);
buf.append(SSN);
buf.append(lName);
buf.append(fName);
buf.append('\n');
String source = buf.toString();
After this i am trying to add the above String source to another array list ???
1. Is this correct?
}
2. i am wondering if there is a way to append the blank space. i.e for example if the lastname is only 10 charcter long i will be needing rest 20 charcters of blamk space.
3. what i am trying to do above after i get the string Souce is pass it as parameter to the following method
private void writeMyFile(String str){
OutputStream f0 = null;
try{
byte buf[] = new byte[str.length()];

f0 = new FileOutputStream("C:/out.txt");
f0.write(buf);
f0.close();
}catch (IOException e) {
System.out.println("ioexception" + e.getMessage());
e.printStackTrace();
}
I think i am complicating it too much. Can anyone please give me an easier way to do this and more efficient way to do this. I will really appreciate an idea about how to deal this problem. I will implement it. will if u have sample i will really be thankful.
Regards,
Vikram
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After this i am trying to add the above String source to another array list ???
I'm not sure why you would do that?
2. i am wondering if there is a way to append the blank space. i.e for example if the lastname is only 10 charcter long i will be needing rest 20 charcters of blamk space.

I'd suggest making a utility method that looks something like this:

Where stringToPad = your String; padChar is that character to pad with (e.g. ' '); totalLength is the length of the returned string (e.g. 30).

That code above will not actually write anything because your buffer (buf) is never filled with anything.
Here's a suggestion:

Hope that helps somewhat.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!