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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tearstar
    New Member
    • Mar 2013
    • 1

    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


    Code:
    public void test()
    	{try
    		{
    			// Open the file that is the first 
    			// command line parameter
    			FileInputStream fstream = new FileInputStream(file);
    			// Get the object of DataInputStream
    			BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    			String strLine;						
    
    			ArrayList<String> lineList = new ArrayList<String>();	// create an arraylist populated by lines in the textfile
    			String thisLine = br.readLine();
    
    			while (thisLine != null) {
    				lineList.add(thisLine);
    				thisLine = br.readLine();
    			}
    
    			// used to output the method results as a test
    			// comment this out before finalizing
    			int w = 0;
    			for (String testLine : lineList) {	// enhanced for loop
    				System.out.println("Line " + w + ": " + testLine);
    				w++;
    			}
    				fstream.close();
    			}catch (Exception e){//Catch exception if any
    			System.err.println("Error: " + e);
    		}
    	}
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    Working...