Attempting to open several files from FileInputStream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tukeind
    New Member
    • Apr 2007
    • 10

    #1

    Attempting to open several files from FileInputStream

    Hello, I've opened a file (code below) containing file names onto a FileInputStream , and attempting to open each file and place its contents onto a FileOutputStrea m (or buffer) and need help getting it accomplished.

    I am developing the application for a Capstone project at school...where we are developing a:
    - Consumer Financial Software System (personal
    finance manager)

    I am using Java's j2sdk1.4.2_12 on Windows XP

    The embolden part of the code is where I like to begin a loop and iterate/cycle through the files on
    the FileInputStream (indexFile); having the loop open the file and place it onto a FileOutputStrea m.

    (Thanks)

    The code is:

    Code:
    //Program Consumer Financial Software System (Capstone Project)
    //File:  CfssAcctSubPanel.java
    
    import java.io.*;
    import java.awt.*;
    import java.lang.*;
    import java.util.*;
    import java.awt.event.*; // for WindowEvent, ItemEvent, ActionEvent;
    import java.awt.event.WindowAdapter;
    import javax.swing.*;  //for JFrame, JComboBox, JList, JLabel
    import javax.swing.event.*;
    
    public class CfssAcctSubPanel extends JPanel
    {
        //Create a Vector object, which is a dynamic array that hold objects.
        private static Vector acctList = new Vector();
        private JComboBox acctsCombo;
        private JList casp1List, casp2List;
        private JLabel caspAcctLbl = new JLabel("Account");
        private JLabel caspTrnsLbl = new JLabel("Transaction");
        private JLabel casp2BalLbl = new JLabel("Balance");
        private JLabel casp2ValLbl = new JLabel("Value");
    	
        public CfssAcctSubPanel()
        {
    	String strAcct = "Accounts";
    	String strMasIndex = "MasterAccountsIndex";
    	//	int size5 = 180;
    
    	try {
    	    // Extracting username from logonExist file
    	    FileInputStream username = new FileInputStream( "logonExist");
    	    int size = username.available();
    	    byte userArray[] = new byte[size];
    	    username.read( userArray);
    	    username.close();
    	    String login = new String(userArray);
    
    	    // MasterAccountsIndex file directory
    	    File userDir = new File(login + "/" + strAcct + "/" + strMasIndex);
    	    System.out.println(userDir);
    
    [B]	    // MasterAccountsIndex file listing array (indexArray)
    	    FileInputStream indexFile = new FileInputStream(userDir);
    	    int size2 = indexFile.available();
    	    byte indexArray[] = new byte[size2];
    	    indexFile.read( indexArray);
    	    System.out.println(new String(indexArray));
    	    indexFile.close();
    [/B]
    	}
    	catch(FileNotFoundException fnfe)
    	    {
    		System.out.println("Can't find the file:" + strMasIndex);
    	    }
    	catch(IOException ioe)
    	    {
    		System.out.println("Problem reading the file!");
    	    }
        }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Tukeind
    Hello, I've opened a file (code below) containing file names onto a FileInputStream , and attempting to open each file and place its contents onto a FileOutputStrea m (or buffer) and need help getting it accomplished.
    I'm afraid I don't understand your question; you have a single file that contains
    the names of a bunch of other files. You have one output file and then what?
    Read all those files and write their contents to that single output file?

    kind regards,

    Jos

    Comment

    • Tukeind
      New Member
      • Apr 2007
      • 10

      #3
      Jos, I apologize for not getting back to you sooner.

      What I am trying to do is:

      concatenate multiple InputStream (via SequenceInputSt ream)...

      Details: the program creates several directory once the user key-in their username.
      - in the Accounts directory of the user lies test data which I want to open as if I
      was at the command line in unix and issued a 'cat Exp_*'...this will display all
      the files that start with Exp_...I am trying to get the same affect onto a
      InputStream and then place that information into a JList (hopefully)...

      Do you understand me now?

      Thank you,
      Tukeind...

      The code I am currently working with is:

      Code:
      //Panel to interact with user on Accounts tab
      //File:  CfssAcctSubPanel.java
      
      import java.lang.String;
      import java.util.Vector;
      import java.awt.*;
      import java.io.*;
      import java.io.File.*;
      import java.nio.*;
      import java.nio.channels.*;
      import java.awt.*;
      import java.lang.*;
      import java.util.*;
      import javax.swing.*;
      
      public class CfssAcctSubPanel extends JPanel
      {
          // Vector objects
          Vector acctList = new Vector();
          
          private JComboBox acctsCombo;
          private JList casp1List, casp2List;
          private JLabel caspAcctLbl = new JLabel("Account");
          private JLabel caspTrnsLbl = new JLabel("Transaction");
          private JLabel casp2BalLbl = new JLabel("Balance");
          private JLabel casp2ValLbl = new JLabel("Value");
      	
          String strMasIndex = "MasterAccountsIndex";
          String strAcct = "Accounts";
          String brLine;
      
          FileInputStream fIn;
          FileChannel fChan;
          long fSize;
          MappedByteBuffer mBuf;
      
          public CfssAcctSubPanel()
          {
      	// 	acctMapChan amc = new acctMapChan();
      	// 	amc.teling();
      
      	try {
      
      	// Extracting username from logonExist file
      	FileInputStream username = new FileInputStream( "logonExist");
      	int size = username.available();
      	byte userArray[] = new byte[size];
      	username.read( userArray);
      	username.close();
      	String login = new String(userArray);
      
      	// login and userDir variable combined to render path to
      	// MasterAccountsIndex file
      	File userDir = new File(login + "/" + strAcct + "/" + strMasIndex);
      	System.out.println(userDir);
      
      	// "dir" to change path to user Accounts directory
      	File dir = new File(login + "/" + strAcct + "/");
      	String[] children = dir.list();
      	if(children == null) {
      	    // Either dir does not exist or is not a directory
      	    } else {
      
      		for(int i =0; i<children.length; i++) {
      		    // Extract filenames from MasterAccountsIndex file 
      		    FileInputStream fin = new FileInputStream(children[i]);
      		    acctList.addElement(fin);
      		    System.out.println(fin);
      		}
      		InputStream in = new SequenceInputStream(acctList.elements());
      		for(int i = in.read(); i != -1; i = in.read()) {
      		    System.out.write(i);
      		}
      	}
          }
          catch(FileNotFoundException fnfe)
      	{
      	    System.out.println("Can't find the file:" + strMasIndex);
      	}
          catch(IOException ioe)
      	{
      	    System.out.println("Problem reading the file!");
      	}    }
      
          public class acctMapChan
          {
      	public acctMapChan()
      	{
      	    System.out.println("hello from acctMapChan");
      	}
      	public void teling()
      	{
      	    System.out.println("hello from teling");
      	}
          }
      }

      Comment

      • Tukeind
        New Member
        • Apr 2007
        • 10

        #4
        Here is the output when I run the application:
        java.io.FileInp utStream@b09e89
        java.io.FileInp utStream@178703 8
        java.io.FileInp utStream@fa9cf
        java.io.FileInp utStream@55571e
        java.io.FileInp utStream@ca8327
        java.io.FileInp utStream@16897b 2
        java.io.FileInp utStream@1bf677 0
        java.io.FileInp utStream@1201a2 5
        java.io.FileInp utStream@94948a
        java.io.FileInp utStream@a401c2
        java.io.FileInp utStream@16f8cd 0
        Last edited by r035198x; Oct 25 '07, 07:37 PM. Reason: code tags bug had struck again

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          I don't know whether or not it answers your question because you're supplying
          us with so much information and most of it is a red herring, but suppose you have
          a List of Files or Strings (the path names of the file). The following little method
          returns an InputStream that can read them all:

          Code:
          public InputStream openAll(List<File> files) { // List<String> is fine too
          
             InputStream is= null;
          
             if (files == null || files.length() == 0)
                return is; // nothing to read
          
             is= new FileInputStream(files.get(0));
             for (int i= 1; i < files.length(); i++)
                is= new SequenceInputStream(is, new FileInputStream(files.get(i));
             return is;
          }
          Does this answer your question?

          kind regards,

          Jos

          ps. I left out the '=java' qualifier from the code tag because they've decided to
          display class names in light blue, for Pete's sake.

          Comment

          • Tukeind
            New Member
            • Apr 2007
            • 10

            #6
            Jos,

            I am attempting to see if it works now...I'll let you know...I am not trying to lead down the path of red herring trickery...its always challenging to start explaining where you are exactly stuck at in writing a piece of code.

            -Tukeind

            Comment

            • Tukeind
              New Member
              • Apr 2007
              • 10

              #7
              Hello Jos,

              I am trying to incorporate the code you gave me into what I already have...(forgive newbie)...could you give me a hint on how to incorporate it into the piece of code where i am doing a directory listing?

              Thanks,
              Tukeind

              Comment

              Working...