Hi all

Im experiencing problems whereby my codes can only read one file instead of all files in my directory. Does anyone know anything regarding this?

Below are my codes:

import java.io.*;
import java.util.*;

public class extractionn
{
    public static void main( String[] args )
    {

    	
     File dir = new File("D:\\project1\\MP Project\\Database\\Extracted\\binding protein\\1APB.pdb.txt"); 					//set file
     String path = dir.getAbsolutePath();				//get file path
     int lala = path.lastIndexOf("\\");					//get index of '\'
     String howPath = path.substring(0,lala+1);			//extract directory name
     File usePath = new File(howPath);					//assign directory for reading file
     String[] fileDir = usePath.list();					//store file in dit to array
     if(fileDir == null) 								//check if dir exists
     {
     	System.out.println("Directory Does Not Exist");
     }
      else
      {
     	for(int j=0; j<fileDir.length;j++)
     	
     	{
     		String fileName = fileDir[j];
          try
           {
           	
        	
       	    BufferedReader in = new BufferedReader(new FileReader(fileDir[j]));			//reading files in specified directory
		    //FileWriter outStream = new FileWriter ("D:\\project1\\MP Project\\Protein Database\\" +fileName +".txt"); //location of files created & naming of files
     	    String str;
		 
      		 while ((str = in.readLine()) != null)  									//file reading
      		 {
      		 	
      		 	
              if ((str.trim().startsWith("ATOM") && str.trim().endsWith("C"))) 		//extract lines that starts with ATOM AND end with anything but H
      			{
     String[] store = new String[10000];
     int z =0;	
      	 StringTokenizer s = new StringTokenizer(str," ");
		 int counter=0;
		 String line="";
	
		 while(s.hasMoreTokens())
				{
				 String ss = s.nextToken();
				 counter++;
				 
				 if (counter ==3 || counter==4) 
				  {
					line += ss;
					line +=" ";
				  }//end of if (counter ==3 || counter==4) 
				
      			}//end of while(s.hasMoreTokens())

						if(line.trim().startsWith("CA"))
						{				
			
						if(line.trim().endsWith("ALA"))
  				 		{
  				 			System.out.print("A");
  				 			store[z]= "A";
  				 			z++;					 	
  				 		}//end of if(line1.trim().startsWith("ALA"))

  				 		if(line.trim().endsWith("ARG"))
  				 		{
  				 			System.out.print("R");
  				 			store[z]= "R";
  				 			z++;		 	
  				 		}//end of if(line1.trim().startsWith("ARG"))

  				 		if(line.trim().endsWith("ASN"))
  				 		{
  				 			System.out.print("N");  				 	
  				 			store[z]= "N";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("ASN"))

  				 		if(line.trim().endsWith("ASP"))
  				 		{
  				 			System.out.print("D");  				 	
  				 			store[z]= "D";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("ASP"))
  				 
  				 		if(line.trim().endsWith("CYS"))
  				 		{
  				 			System.out.print("C");  				 	
  				 			store[z]= "C";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("CYS"))

  				 		if(line.trim().endsWith("GLN"))
  				 		{
  				 			System.out.print("Q");  				 	
  				 			store[z]= "Q";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("GLN"))

  				 		if(line.trim().endsWith("GLU"))
  				 		{
  				 			System.out.print("E");  				 	
  				 			store[z]= "E";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("GLU"))

  				 		if(line.trim().endsWith("GLY"))
  				 		{
  				 			System.out.print("G");  				 	
  				 			store[z]= "G";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("GLY"))

  				 		if(line.trim().endsWith("HIS"))
  				 		{
  				 			System.out.print("H");  				 	
  				 			store[z]= "H";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("HIS"))

  				 		if(line.trim().endsWith("ILE"))
  				 		{
  				 			System.out.print("I");  				 	
  				 			store[z]= "I";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("ILE"))

  				 		if(line.trim().endsWith("LEU"))
  				 		{
  				 			System.out.print("L");  				 	
  				 			store[z]= "L";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("LEU"))

  				 		if(line.trim().endsWith("LYS"))
  				 		{
  				 			System.out.print("K");  				 	
  				 			store[z]= "K";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("LYS"))

  				 		if(line.trim().endsWith("MET"))
  				 		{
  				 			System.out.print("M");  				 	
  				 			store[z]= "M";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("MET"))

  				 		if(line.trim().endsWith("PHE"))
  				 		{
  				 			System.out.print("F");  				 	
  				 			store[z]= "F";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("PHE"))

  				 		if(line.trim().endsWith("PRO"))
  				 		{
  				 			System.out.print("P");  				 	
  				 			store[z]= "P";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("PRO"))

  				 		if(line.trim().endsWith("SER"))
  				 		{
  				 			System.out.print("S");  				 	
  				 			store[z]= "S";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("SER"))

  				 		if(line.trim().endsWith("THR"))
  				 		{
  				 			System.out.print("T");  				 	
  				 			store[z]= "T";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("THR"))

  				 		if(line.trim().endsWith("TRP"))
  				 		{
  				 			System.out.print("W");  				 	
  				 			store[z]= "W";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("TRP"))

  				 		if(line.trim().endsWith("TYR"))
  				 		{
  				 			System.out.print("Y");  				 	
  				 			store[z]= "Y";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("TYR"))

				 		if(line.trim().endsWith("VAL"))
  				 		{	
  				 			System.out.print("V");
  				 			store[z]= "V";
  				 			z++;
  				 		}//end of if(line1.trim().startsWith("VAL"))

if(store[z] != null)
 System.out.println(store[z]+"\t");

			}//end of if(line.trim().startsWith("CA"))						
      			}
             }
		   //outStream.close();														//closing of writer
           in.close();																	//closing of reader
           } catch (IOException e) { }													//try & catch
     	   
     	}																				//end of for loop
      }																					//end of else
    }																					//end of method
}
																						//end of class

my codes can only read one file instead of all files in my directory.

Try debugging your code by printing out a list of all the files found in the directory:
System.out.println("All files=" + Arrays.toString(fileDir)); // show all files found

and then show each one as it is being processed:

System.out.println("next file=" + fileDir[j]); // show next file

Hi Norm, I've tried what you told me to.. it is able to print all the file names in that directory using the first line of codes that you suggested.The second line of codes showed was only the first file, my java file and its class file in that same directory. is there anything that I did wrongly?

Hard to tell what the problem is without seeing the code you are describing.
If you print out the full contents of the array with Arrays.toString() and then print out individual elements with the other print statement and the two outputs are different, then I must assume that you have changed the contents of the array between when you print it all out and when you print the individual elements.

bcuz when i tried printing all the files that were in that directory..the file names were able to be shown however when i tried printing the "next file" statement, it doesn show the remaining text files. what it does was only showing one text file and it went on straight to reading my java class file and stopped.

it doesn't show the remaining text files. what it does was only showing one text file and it went on straight to reading my java class file and stopped.

That sounds like your code execution is NOT looping. It only executes the loop once, shows one file, does something and then exits the loop.

Check the code to see how or why the loop is being exited before the next file is processed in the loop.

For example you used a very bad technique here to ignore errors:

    } catch (IOException e) { }     

add a e.printStackTrace() in the catch block to show any errors happening.

now im able to get all files to be shown..Im supposed to convert the 3 letter abbreviations to a one-letter abbreviation.. what im experiencing now is that the conversion of the first file is shown and outputted to all the other files in that directory. but all files are supposed to have different conversion sequences...it seems that something is wrong with my looping but i cannot find the error.

all files are supposed to have different conversion sequences

Sorry, I don't understand what the problem is. That sounds like the input data is wrong.

To debug your code, add some println()s to show how the variable's values are changing and how code execution is flowing.

hmmm..what i meant was that im converting the 3 letter abbreviations in a file into 1 letter abbreviation. but what im getting are all the 1 letter abbreviations of the first file and it prints the same output for the rest of the files..

Check your logic.
To debug your code, add some println()s to show how the variable's values are changing and how code execution is flowing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.