Results 1 to 10 of 10
  1. #1
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default how we can read inside all of files in a directory

    Hi

    I need to read inside several txt file inside a directory, I don't know how
    please help me if you know,

    sincerely yours
    Cowboy
    this is my code : but it give ma an error

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class DataReader {
    	public static String reproRec;
    	public static void main(String[] args){
    
    		Scanner stdIn = new Scanner(System.in);
    		Scanner fileIn;
    		String record;
    		PrintWriter fileOut;
    
    
    		File outPut = new File( "C:\\Documents and Settings\\Tiff\\workspace\\DataLouder\\out.txt" );
    
    		try{
    			fileOut = new PrintWriter(new FileWriter(outPut, true));
    
    			try{
    				File dir = new File("C:\\Documents and Settings\\Tiff\\Desktop\\AgSource" );
    				String[] files = dir.list();
    				System.out.println("files in this directory are:");
    
    				for (String fileName :files)
    				{
    					System.out.println(fileName);
    				}
    
    
    				/** for (int i=0; i<files.length; i++) {
    				        // Get filename of file or directory
    				        String filename = files[i];
    				 }*/
    
    
    				for (String file :files){
    
    					fileIn = new Scanner(new FileReader(file));
    					while (fileIn.hasNext()){
    						record = fileIn.nextLine();
    						int segment = Integer.parseInt(record.substring(136,137));
    
    						switch (segment)
    						{
    						case(0):
    							reproRec=
    								record.substring(0,20)+ ", " ;
    						break;
    
    						case(1):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " ;
    						break;
    						case(2):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " ;
    						break;
    						case(3):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " ;
    						break;
    						case(4):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " ;
    						break;
    						case(5):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " ;
    						break;
    						case(6):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " ;
    						break;
    						case(7):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " ;
    						break;
    						case(8):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " ;
    						break;
    						case(9):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " ;
    						break;
    						case(10):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " ;
    						break;
    						case(11):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " ;
    						break;
    						case(12):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " ;
    						break;
    						case(13):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " +
    								record.substring(498, 528)+ ", " ;
    
    						break;
    						case(14):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " +
    								record.substring(498, 528)+ ", " +
    								record.substring(528, 558)+ ", " ;
    
    						break;
    						case(15):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " +
    								record.substring(498, 528)+ ", " +
    								record.substring(528, 558)+ ", " +
    								record.substring(558, 588)+ ", " ;
    
    						break;
    						case(16):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " +
    								record.substring(498, 528)+ ", " +
    								record.substring(528, 558)+ ", " +
    								record.substring(558, 588)+ ", " +
    								record.substring(588, 618)+ ", " ;
    
    						break;
    						case(17):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " +
    								record.substring(498, 528)+ ", " +
    								record.substring(528, 558)+ ", " +
    								record.substring(558, 588)+ ", " +
    								record.substring(588, 618)+ ", " +
    								record.substring(618, 648)+ ", " ;
    
    						break;
    						case(18):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " +
    								record.substring(498, 528)+ ", " +
    								record.substring(528, 558)+ ", " +
    								record.substring(558, 588)+ ", " +
    								record.substring(588, 618)+ ", " +
    								record.substring(618, 648)+ ", " +
    								record.substring(648, 678)+ ", " ;
    
    						break;
    						case(19):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " +
    								record.substring(498, 528)+ ", " +
    								record.substring(528, 558)+ ", " +
    								record.substring(558, 588)+ ", " +
    								record.substring(588, 618)+ ", " +
    								record.substring(618, 648)+ ", " +
    								record.substring(648, 678)+ ", " +
    								record.substring(678, 708)+ ", " ;
    						break;
    
    						case(20):
    							reproRec=
    								record.substring(0,20)+ ", " + 
    								record.substring(138, 168)+ ", " +
    								record.substring(168, 198)+ ", " +
    								record.substring(198, 228)+ ", " +
    								record.substring(228, 258)+ ", " +
    								record.substring(258, 288)+ ", " +
    								record.substring(288, 318)+ ", " +
    								record.substring(318, 348)+ ", " +
    								record.substring(348, 378)+ ", " +
    								record.substring(378, 408)+ ", " +
    								record.substring(408, 438)+ ", " +
    								record.substring(438, 468)+ ", " +
    								record.substring(468, 498)+ ", " +
    								record.substring(498, 528)+ ", " +
    								record.substring(528, 558)+ ", " +
    								record.substring(558, 588)+ ", " +
    								record.substring(588, 618)+ ", " +
    								record.substring(618, 648)+ ", " +
    								record.substring(648, 678)+ ", " +
    								record.substring(678, 708)+ ", " +
    								record.substring(708, 738)+ ", " ; 
    						break;					
    
    						}// end switch
    
    
    						fileOut.println(reproRec);
    
    
    					}// end while loop
    					fileIn.close();
    
    				}// end for each loop
    
    			}
    			catch (FileNotFoundException e)// for fileIn
    			{
    				System.out.println("Error:" + e.getMessage());
    			} 
    
    
    
    			fileOut.close();
    		}
    		catch (IOException e) // for fileOut
    		{
    			System.out.println("Error:" + e.getMessage());
    		}
    
    	}// end main
    }// end class
    Last edited by Fubarable; 08-02-2010 at 10:46 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    it gives me an error
    Please copy and paste here the full text of the error message.

  3. #3
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please copy and paste here the full text of the error message.
    the error message is :

    files in this directory are:
    source (1).txt
    source (2).txt
    source (3).txt
    source (4).txt
    source.txt
    Error:C:\Documents and Settings\Tiff\Desktop\AgSourcesource (1).txt (The system cannot find the file specified)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Please copy and paste here the full text of the error message.
    Don't edit it, copy and paste it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You'll need to paste the complete error message and indicate which line is causing the error. Also your code is a bit redundant and can possibly be shortened 80% or more. Doing this would help you (and us) quite a bit by making it harder to hide bugs, and also by simplify your debugging a lot.

    Finally, I've added code tags to your code to help make it more readable in the forum. To learn how to do this yourself, please see the link in my signature. Luck!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance, it's possible that your whole huge switch block here:
    Java Code:
                      record = fileIn.nextLine();
                      int segment = Integer.parseInt(record.substring(136,137));
    
                      switch (segment)
                      {
                      case(0):
                         reproRec=
                            record.substring(0,20)+ ", " ;
                      break;
    
                      case(1):
                         reproRec=
                            record.substring(0,20)+ ", " + 
                            record.substring(138, 168)+ ", " ;
                      break;
                      case(2):
                         reproRec=
                            record.substring(0,20)+ ", " + 
                            record.substring(138, 168)+ ", " +
                            record.substring(168, 198)+ ", " ;
                      break;
                      case(3):
                         reproRec=
                            record.substring(0,20)+ ", " + 
                            record.substring(138, 168)+ ", " +
                            record.substring(168, 198)+ ", " +
                            record.substring(198, 228)+ ", " ;
                      break;
                      case(4):
    
                         // ..... etc .....  code deleted for brevity's sake
    
                      }// end switch

    can be replaced by this simplified code:

    Java Code:
    record = fileIn.nextLine();
    int segment = Integer.parseInt(record.substring(136,137));
    
    reproRec = record.substring(0, 20) + ", ";
    for (int index = 0; index < segment) {
       int start = 138 + 30 * index;  // you'd also use constants to get rid of these "magic" numbers
       int end = start + 30;
       reproRec += record.substring(start, end) + ", ";
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    Might I suggest use listFiles() rather than list()?
    That way you don't have to faff with String concatenation, and you'll know you have a File.

  8. #8
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    the whole output and error message that I recieve is here:

    files in this directory are:
    C:\Documents and Settings\Tiff\Desktop\AgSource\source (1).txt
    C:\Documents and Settings\Tiff\Desktop\AgSource\source (2).txt
    C:\Documents and Settings\Tiff\Desktop\AgSource\source (3).txt
    C:\Documents and Settings\Tiff\Desktop\AgSource\source (4).txt
    C:\Documents and Settings\Tiff\Desktop\AgSource\source.txt

    Error:C:\Documents and Settings\Tiff\Desktop\AgSourceC:\Documents and Settings\Tiff\Desktop\AgSource\source (1).txt (The filename, directory name, or volume label syntax is incorrect)

  9. #9
    cowboy2010 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    I think the problem is that Java want to read adress of each file seperated by double back slash like this : C:\\Documents\\Tiff.... but when we save the file adress in a File array and then we read from that we read as one back slash : C:\Documents and Settings\Tiff\Desktop so we recieve this erros, So I don't know what should i do ?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    Use listFiles()?

    There is absolutely no need for you to be doing all this concatenation...

Similar Threads

  1. Creating File inside a Directory
    By viswanadh7 in forum AWT / Swing
    Replies: 2
    Last Post: 06-27-2013, 07:18 PM
  2. Is there a way to read a file directory
    By willemjav in forum Java Applets
    Replies: 3
    Last Post: 09-05-2011, 01:01 AM
  3. how to read the ArrayList inside HashMap
    By koddy in forum New To Java
    Replies: 6
    Last Post: 07-15-2010, 01:41 PM
  4. I can't read a text file located inside my jar.
    By Serrano0811 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 05:03 PM
  5. Replies: 3
    Last Post: 02-10-2010, 07:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •