• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading Data from ASCII text file and inserting in database

 
sai Narayan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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..
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!