• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading Data from ASCII text file and inserting in database

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I am have an ASCII text file containing records.I am reading one record at a time using
BufferedReader.readLine();
Then i am reading each dataitem into a StringBuffer from the record as there is a delimiter present after each dataitem.
Now i am unable to typecast the dataitem with that of the field in the database?

here is my code:

import java.sql.Date;
import java.util.*;
import java.io.*;

public class asciitojava {
public static void main(String[] args) throws FileNotFoundException,IOException {

String filename="C:\\javaAbc\\abc.txt";
FileInputStream fis = new FileInputStream(filename);
BufferedReader in = new BufferedReader(new FileReader(filename));
String str;
while ((str = in.readLine()) != null)
{
StringBuffer st = new StringBuffer(str);
System.out.println("\nstr="+str);
StringBuffer word = new StringBuffer();
for(int j=0;j<=str.length();j++)
{


if(j<str.length())
if( (st.charAt(j)!='~'))
{
word.append(st.charAt(j));
}
else
{
System.out.println("word="+word);
word = new StringBuffer();
}
if(j==str.length())
System.out.println("word="+word);
}
}
in.close();

}
}


Please help..
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good start! You're getting lines and thinking about how to get fields out of lines. There are easier ways to parse a string tho.

If you're in JDK 5 look at Scanner. You can pull characters off a string (or any stream) and convert them to types like int and date.

If you're in an earlier JDK look at StringTokenizer. You can get delimited strings.

See if those tools help you get the individual parts out of your line. Try to gete to a point where you're holding a number of variables to represent the fields you have to insert. Be sure to test with missing words - they'd appear as two delimiters together. Come back with that and your datatype requirements for the database and we'll talk about the database insert step.
[ December 27, 2004: Message edited by: Stan James ]
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic