Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

trying to read a list of files

Thread view: 
sazykin@gmail.com - 15 Aug 2006 13:50 GMT
I'm trying to read a list of files, have a loop through the loop but
onli one file is going through the InputStream - the first one the rest
just dont....

something is missing......

here is the code:

FileDialog fd = new FileDialog(parent, "Please choose a file:",
               FileDialog.LOAD);
            fd.show();
            String selectedDir = fd.getDirectory();
            if (selectedDir == null) {
                // no file selected
            } else {

                File dir  = new File( selectedDir);

                                        String[] filelist = dir.list();
                                        if (filelist == null) {
                                            // Either dir does not exist or is not a directory
                                        } else {
                                            for (int i=0; i<filelist.length; i++) {
                                                // Get filename of file or directory
                                                String filename = filelist[i];
                                            }
                                        }
                                        //filter for seg files.
                                        FilenameFilter filter = new FilenameFilter() {
                                            public boolean accept(File dir, String name) {
                                                return name.endsWith(".seg");
                                            }
                                        };
                                        filelist = dir.list(filter); //our file list of seg2
files is ready
                                    //for (int i=0; i<filelist.length; i++){
                                        //System.out.println(selectedDir+filelist[i]);}

                    for (int i=1; i<filelist.length; i++){
                    //start loop through the list of files

                    try {

                            File file = new File( selectedDir + filelist[i]);
                           System.out.println("!!!"+file);
                              InputStream is = new FileInputStream(file);
                              DataInputStream dis = new DataInputStream( is );
                              long length = file.length();

                              if (length > Integer.MAX_VALUE) {
                             throw new IOException("File is too large");
                              }
                                  else {
                            System.out.println("TEST"+file);

                             byte[] bytes = new byte[(int)length];
                             int offset = 0;
                             int numRead = 0;
                             while (offset < bytes.length &&
                               (numRead = is.read(bytes, offset, bytes.length-offset) )
>= 0) {
                               offset += numRead;

                                                     }
                                                     System.out.println("offset=="+offset);

                                 if (offset < bytes.length) {
                           throw new IOException("Could not completely read file
"+file.getName());
                                     }

                             dis.close();
                         is.close();

and so on...........

} catch (Exception e){e.printStackTrace();}
          }//close main for loop through the list of files

any help... please...
Andrew Thompson - 15 Aug 2006 14:27 GMT
> I'm trying to ..

..post to all known Java groups?
Please refrain from multi-posting.

Note that Google groups interface offers to
'Separate multiple groups with commas' which
is what I did - posting to both c.l.j.p and c.l.j.help.
I have set the follow ups to c.l.j.programmer only.

BTW - please remove 'tab' characters from code
before posting to Usenet newsgroups, as most news
clients display a *very* large space for tab chars.

Andrew T.
sazykin@gmail.com - 15 Aug 2006 15:35 GMT
sorry guys, it works fine I just screw up somewhere in the middle now
it fixed....
if anyone needs to read all files in a dir they can use it.
enjoy....


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.