473,379 Members | 1,302 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,379 software developers and data experts.

Attempting to open several files from FileInputStream

10
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 FileOutputStream (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 FileOutputStream.

(Thanks)

The code is:

Expand|Select|Wrap|Line Numbers
  1. //Program Consumer Financial Software System (Capstone Project)
  2. //File:  CfssAcctSubPanel.java
  3.  
  4. import java.io.*;
  5. import java.awt.*;
  6. import java.lang.*;
  7. import java.util.*;
  8. import java.awt.event.*; // for WindowEvent, ItemEvent, ActionEvent;
  9. import java.awt.event.WindowAdapter;
  10. import javax.swing.*;  //for JFrame, JComboBox, JList, JLabel
  11. import javax.swing.event.*;
  12.  
  13. public class CfssAcctSubPanel extends JPanel
  14. {
  15.     //Create a Vector object, which is a dynamic array that hold objects.
  16.     private static Vector acctList = new Vector();
  17.     private JComboBox acctsCombo;
  18.     private JList casp1List, casp2List;
  19.     private JLabel caspAcctLbl = new JLabel("Account");
  20.     private JLabel caspTrnsLbl = new JLabel("Transaction");
  21.     private JLabel casp2BalLbl = new JLabel("Balance");
  22.     private JLabel casp2ValLbl = new JLabel("Value");
  23.  
  24.     public CfssAcctSubPanel()
  25.     {
  26.     String strAcct = "Accounts";
  27.     String strMasIndex = "MasterAccountsIndex";
  28.     //    int size5 = 180;
  29.  
  30.     try {
  31.         // Extracting username from logonExist file
  32.         FileInputStream username = new FileInputStream( "logonExist");
  33.         int size = username.available();
  34.         byte userArray[] = new byte[size];
  35.         username.read( userArray);
  36.         username.close();
  37.         String login = new String(userArray);
  38.  
  39.         // MasterAccountsIndex file directory
  40.         File userDir = new File(login + "/" + strAcct + "/" + strMasIndex);
  41.         System.out.println(userDir);
  42.  
  43.         // MasterAccountsIndex file listing array (indexArray)
  44.         FileInputStream indexFile = new FileInputStream(userDir);
  45.         int size2 = indexFile.available();
  46.         byte indexArray[] = new byte[size2];
  47.         indexFile.read( indexArray);
  48.         System.out.println(new String(indexArray));
  49.         indexFile.close();
  50.     }
  51.     catch(FileNotFoundException fnfe)
  52.         {
  53.         System.out.println("Can't find the file:" + strMasIndex);
  54.         }
  55.     catch(IOException ioe)
  56.         {
  57.         System.out.println("Problem reading the file!");
  58.         }
  59.     }
  60. }
  61.  
Oct 21 '07 #1
6 5143
JosAH
11,448 Expert 8TB
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 FileOutputStream (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
Oct 21 '07 #2
Tukeind
10
Jos, I apologize for not getting back to you sooner.

What I am trying to do is:

concatenate multiple InputStream (via SequenceInputStream)...

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:

Expand|Select|Wrap|Line Numbers
  1. //Panel to interact with user on Accounts tab
  2. //File:  CfssAcctSubPanel.java
  3.  
  4. import java.lang.String;
  5. import java.util.Vector;
  6. import java.awt.*;
  7. import java.io.*;
  8. import java.io.File.*;
  9. import java.nio.*;
  10. import java.nio.channels.*;
  11. import java.awt.*;
  12. import java.lang.*;
  13. import java.util.*;
  14. import javax.swing.*;
  15.  
  16. public class CfssAcctSubPanel extends JPanel
  17. {
  18.     // Vector objects
  19.     Vector acctList = new Vector();
  20.  
  21.     private JComboBox acctsCombo;
  22.     private JList casp1List, casp2List;
  23.     private JLabel caspAcctLbl = new JLabel("Account");
  24.     private JLabel caspTrnsLbl = new JLabel("Transaction");
  25.     private JLabel casp2BalLbl = new JLabel("Balance");
  26.     private JLabel casp2ValLbl = new JLabel("Value");
  27.  
  28.     String strMasIndex = "MasterAccountsIndex";
  29.     String strAcct = "Accounts";
  30.     String brLine;
  31.  
  32.     FileInputStream fIn;
  33.     FileChannel fChan;
  34.     long fSize;
  35.     MappedByteBuffer mBuf;
  36.  
  37.     public CfssAcctSubPanel()
  38.     {
  39.     //     acctMapChan amc = new acctMapChan();
  40.     //     amc.teling();
  41.  
  42.     try {
  43.  
  44.     // Extracting username from logonExist file
  45.     FileInputStream username = new FileInputStream( "logonExist");
  46.     int size = username.available();
  47.     byte userArray[] = new byte[size];
  48.     username.read( userArray);
  49.     username.close();
  50.     String login = new String(userArray);
  51.  
  52.     // login and userDir variable combined to render path to
  53.     // MasterAccountsIndex file
  54.     File userDir = new File(login + "/" + strAcct + "/" + strMasIndex);
  55.     System.out.println(userDir);
  56.  
  57.     // "dir" to change path to user Accounts directory
  58.     File dir = new File(login + "/" + strAcct + "/");
  59.     String[] children = dir.list();
  60.     if(children == null) {
  61.         // Either dir does not exist or is not a directory
  62.         } else {
  63.  
  64.         for(int i =0; i<children.length; i++) {
  65.             // Extract filenames from MasterAccountsIndex file 
  66.             FileInputStream fin = new FileInputStream(children[i]);
  67.             acctList.addElement(fin);
  68.             System.out.println(fin);
  69.         }
  70.         InputStream in = new SequenceInputStream(acctList.elements());
  71.         for(int i = in.read(); i != -1; i = in.read()) {
  72.             System.out.write(i);
  73.         }
  74.     }
  75.     }
  76.     catch(FileNotFoundException fnfe)
  77.     {
  78.         System.out.println("Can't find the file:" + strMasIndex);
  79.     }
  80.     catch(IOException ioe)
  81.     {
  82.         System.out.println("Problem reading the file!");
  83.     }    }
  84.  
  85.     public class acctMapChan
  86.     {
  87.     public acctMapChan()
  88.     {
  89.         System.out.println("hello from acctMapChan");
  90.     }
  91.     public void teling()
  92.     {
  93.         System.out.println("hello from teling");
  94.     }
  95.     }
  96. }
  97.  
Oct 25 '07 #3
Tukeind
10
Here is the output when I run the application:
java.io.FileInputStream@b09e89
java.io.FileInputStream@1787038
java.io.FileInputStream@fa9cf
java.io.FileInputStream@55571e
java.io.FileInputStream@ca8327
java.io.FileInputStream@16897b2
java.io.FileInputStream@1bf6770
java.io.FileInputStream@1201a25
java.io.FileInputStream@94948a
java.io.FileInputStream@a401c2
java.io.FileInputStream@16f8cd0
Oct 25 '07 #4
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. public InputStream openAll(List<File> files) { // List<String> is fine too
  2.  
  3.    InputStream is= null;
  4.  
  5.    if (files == null || files.length() == 0)
  6.       return is; // nothing to read
  7.  
  8.    is= new FileInputStream(files.get(0));
  9.    for (int i= 1; i < files.length(); i++)
  10.       is= new SequenceInputStream(is, new FileInputStream(files.get(i));
  11.    return is;
  12. }
  13.  
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.
Oct 25 '07 #5
Tukeind
10
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
Oct 25 '07 #6
Tukeind
10
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
Oct 25 '07 #7

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

Similar topics

3
by: Jacqueline Snook | last post by:
I am trying to reference a file on an apple mac from Java. In order to find what the URL is supposed to be, I opened it in the Safari web browser. The directory as returned was...
6
by: Patrick | last post by:
Hello all! I am porting an application from C++ to Java and have run into a problem using the DataInputStream reader object. The file I am trying to read in is anywhere from 20 to 60 MB and has a...
1
by: Abhijit Gadekar | last post by:
Hello, I am getting a problem with objectinputstream and fileinputstream. Whenever I try to close a ObjectInputStream object it gives an Exception. And if I try to delete the associated file on...
2
by: James Coe | last post by:
ARGH! Any ideas why this might be happening? The code I'm using comes straight from the example in the VB.NET Help System. All I did was tweak the name= stuff to match my application. Server...
1
by: chanmm | last post by:
I hit the problem in my WinXP can someone help me: The Web server reported the following error when attempting to create or open the Web project located at the following URL:...
8
by: J-P-W | last post by:
Hi, anyone got any thoughts on this problem? I have sales reps. that remotely send their data to an ftp server. The office downloads all files, the code creates an empty file, then downloads the...
1
by: sang | last post by:
Hello I want to open a MS Word file in given text area by using the JSP. I done it but i have a problem in the output <%@ page import = "java.io.*" %> <html> <body> text <textarea...
1
by: Satanduvel | last post by:
Hello i'm making an applet that has 2 JTables en when you select a file in a JTable you can upload the file. If i select 1 file i can upload it but when i'm selecting 2 or more i get the error...
13
by: milk242 | last post by:
I know I'm making a mistake, but I'm wondering if someone could tell me the type of mistake I'm making. I know I can write the whole array to a file, but I want to create separate binary files for...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.