aspose file tools*
The moose likes Beginning Java and the fly likes java.io.IOException Too many open files error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

java.io.IOException Too many open files error

richardgonzales muller
Greenhorn

Joined: Sep 12, 2005
Posts: 4
Hella everybody,

I have written a program. The motive of this program is to find a pattern. my pattern is 'a' linked with both 'b' and 'c', the it should write to the output file. for example in my input file:

1 2
1 3
1 5
2 4
2 6.
3 5
MY output would be
1 2 3
1 2 5
1 3 5
2 4 6.

My program is working fine. But in my input file I have a sequence where there is a link from 5 to other numbers.
5 9
5 10
....
....
....
....
....
5 145.
So the number of links from 5 to others are around more than 135. Many other numbers like 7 and 9 and 11 also have more links(more than 150).When the program reads my input file it gives the error:

java.io.IOException: Too many open files.

But if I run the same program with someother smaller file where the links are lesser like the example which I have shown in the beginning, it works well and fine. My code is here.

import java.io.*;
import java.util.*;
import java.lang.String.*;
public class motif_example
{
public motif_example() throws Exception
{
PrintWriter p=new PrintWriter(new BufferedWriter(new FileWriter("E:/Final_files/analysis/output.txt")));
int ln=0;int ln1=0;int count=0;//int a,b,c=0;
//int A1,B1,C1=0;
String line,line1,A,B = " ";
String value[] =new String[2000];
String values[] =new String[2000];

for(int a=1; a<900; a++){
for (int b=1; b<1280; b++){
for (int c=b+1; c<1280; c++)
{
BufferedReader b1 = new BufferedReader(new FileReader("E:/Final_files/analysis/Transreg.txt"));
LineNumberReader l = new LineNumberReader(b1);
int A1=0;int B1=0;int C1=0;

while(true)
{
line = l.readLine();if (line==null) break;
StringTokenizer st = new StringTokenizer(line);
ln = l.getLineNumber();
if (ln>=1)
{
int i = 0;
while(st.hasMoreTokens())
{
value[i] = st.nextToken();//p.println(values[i]);
//A=value[0]; B=value[1];
if(i==1)
{

if((a==Integer.parseInt(value[0])&& b== Integer.parseInt(value[1])))
{A1=a; B1=b;/*p.println(A1+"\t"+B1+"\t");*/}
if((a==Integer.parseInt(value[0])&& c== Integer.parseInt(value[1])))
{A1=a; C1=c;}
}
i++;
}
}

}
if(B1!=0&&C1!=0)
{p.println(A1+"\t"+B1+"\t"+C1);}
}}}p.close();
}
public static void main(String s[]) throws Exception
{
new motif_example();
}
}



I also have another question. The computational time is high since it has to calculate all the combinations in the file.For its enough to achieve till 900 but the total number of other members in the file count to 1280. Hence I have took the upper limit of 1280 for 'b' and 'c'. CAn the program be changed somehow or is there any other better method to analyse such a pattern systems in a file.

Thanx in advance.
Best regards,
Richard
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

File handles are finite resources and you are opening lots of them:

Close b1 when you are done with it or move the creation out of the loops (depending on what you are using it for) and this problem should clear up.


[How To Ask Questions On JavaRanch]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

In your inner loop, you open the file Transreg.txt, but you never close it. All you need to do to fix the error would be to close that file at the end of the inner loop.

Now, as to making the program faster: without even looking at your algorithm, I can suggest that one way to vastly speed up the program would be to do all the file I/O, String tokenizing, and integer parsing once, before the main loop starts, and store the tokens in a 2-dimensional int array (lines X tokens). The main loop can then access this array. I guarantee the program will then be vastly faster.

After you've done that, you can look at the algorithm. Searching linearly through a list or array is slow compared to looking things up in a sorted list or a hash table, especially for long lists. You could probably speed things up another factor of 10-100 by sorting that 2-dimensional array properly, so that you can do away with the linear lookups. But that's a problem for another day.


[Jess in Action][AskingGoodQuestions]
richardgonzales muller
Greenhorn

Joined: Sep 12, 2005
Posts: 4
Hallo Ernst and baratender,

Thanks for ur reply. Now my problem is fixed up.Ernst one question for u. U said store it in two dimensional array. I thought about it, sounds interesting. but I am not sure about the approach. COuld u please help me out, if possible and provided if u have time. Can u please send the codes for storing and then comparing the array elements.

Thanks in advance
richardgonzales muller
Greenhorn

Joined: Sep 12, 2005
Posts: 4
i tried to store the values in a 2D array. But my codes is having some problems. Its gives me array out of bound Exception and also the lines where I am checking up the equality between my array elements and loop numbers, it raises error. please correct the code and tell me what goes wrong where.


import java.io.*;
import java.util.*;
import java.lang.String.*;
public class motif_analysis1
{
public motif_analysis1() throws Exception
{
{
int ln=0;int ln1=0;int count=0;//int a,b,c=0;
String line,line1,A,B = " ";
int [][]x = new int[2636][2];
String value[] =new String[200];
String values[] =new String[200];
BufferedReader b1 = new BufferedReader(new FileReader("E:/Final_files/motif_analysis/Transreg_Motif.txt"));
PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter("E:/Final_files/motif_analysis/motif1_output.txt")));
LineNumberReader l = new LineNumberReader(b1);
int A1=0;int B1=0;int C1=0;

while(true)
{
line = l.readLine();if (line==null) break;
StringTokenizer st = new StringTokenizer(line);
ln = l.getLineNumber();
if (ln>=1)
{
int i = 0;int j=1;
while(st.hasMoreTokens())
{
value[i] = st.nextToken();
x[ln][i+1] = Integer.parseInt(value[i]);p.print(x[ln][i+1]+"\t");
i++;
} j++;
}p.println();
}
}


for (int b=1; b<1280; b++){
for (int c=b+1; c<1280; c++){
for (int j=1; j<=2636;j++)
{
if((a==x[j][1])&& (b== x[j][2]))
{A1=a; B1=b;/*p.println(A1+"\t"+B1+"\t");}
if((a==x[j][1])&& (c== x[j][2]))
{A1=a; C1=c;}
if(B1!=0&&C1!=0)
}
{p.println(A1+"\t"+B1+"\t"+C1);}
}}}p.close();
}
public static void main(String s[]) throws Exception
{
new motif_analysis1();
}
}
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Question re-asked in new thread


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io.IOException Too many open files error