473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Read a multiple file textfile with strings and doubles and input info to database

I have a textfile that I need to read each line separated by \n

Every 3 lines I need to reset the array

The textfile may contain 100-1000 lines and every 3rd line is a double while the rest are strings, then I need to take each cluster of 3 lines and input that into a database.

I have the database connection, I can read the entire file, I just can't figure out how to break the file into clusters of 3 lines

So something like
Array[0] = Cluster1 String1
Array[1] = Cluster1 String2
Array[2] = Cluster1 Double
Array[0] = Cluster2 String1
Array[1] = Cluster2 String2
Array[2] = Cluster2 Double

Here is what I have so far, which will output the whole file


Expand|Select|Wrap|Line Numbers
  1. public void test()
  2.     {try
  3.         {
  4.             // Open the file that is the first 
  5.             // command line parameter
  6.             FileInputStream fstream = new FileInputStream(file);
  7.             // Get the object of DataInputStream
  8.             BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  9.             String strLine;                        
  10.  
  11.             ArrayList<String> lineList = new ArrayList<String>();    // create an arraylist populated by lines in the textfile
  12.             String thisLine = br.readLine();
  13.  
  14.             while (thisLine != null) {
  15.                 lineList.add(thisLine);
  16.                 thisLine = br.readLine();
  17.             }
  18.  
  19.             // used to output the method results as a test
  20.             // comment this out before finalizing
  21.             int w = 0;
  22.             for (String testLine : lineList) {    // enhanced for loop
  23.                 System.out.println("Line " + w + ": " + testLine);
  24.                 w++;
  25.             }
  26.                 fstream.close();
  27.             }catch (Exception e){//Catch exception if any
  28.             System.err.println("Error: " + e);
  29.         }
  30.     }
Mar 26 '13 #1
1 1486
r035198x
13,262 8TB
Use a FileReader instead of FileInputStream for reading character files.

Declare a variable before the loop and increment it for every read. Reset it to zero every time it reaches 3.
Mar 26 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: 3A Web Hosting | last post by:
Hi Is it possible to perform multiple file uploads via a form? It's no problem uploading single files but I want to be able to highlight a group of files and upload them all in one go. My...
3
by: Konan | last post by:
Pardon the simple question, but I have just begun to learn PHP. So far so good - all the examples in my books actually work. One thing that none of them address is how to read a file of strings...
2
by: Raghu Gupta | last post by:
Hi, Actually i am facing some problem using quickbooks and presently i am not so expert in using it. I am using c# code to retrive the data from quickbooks. it doesn't matter which language we...
4
by: Rob Leyshon | last post by:
For whatever reason I am unable to use FileSystemObject e.g. using the code: Dim fso As New FileSystemObject Everytime my program hits this it gives the error: "Compile error: User-defined...
11
by: jean-jeanot | last post by:
I can access to a file with the command: file_obj = open ( " D:\My documents\Textfile.txt",'r') When I now try to read a file with the following command: file_obj = open ("D:\My...
10
by: Arquitecto | last post by:
Hi , I have a question about a file operation i want to do . I have a data file lets say X bytes . I want to read the file and delete a byte every 2nd byte . I am a little comfused ,my approach...
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
8
johngault
by: johngault | last post by:
I've been working with this PHP page for several days now and I'm stumped. The page is supposed to allow the user to upload up to six images for their profile. When the user adds an image it (the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.