• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Problem with RandomAccessFile

With the following code in the readfile() function, I can't read the RandomAccessFile it keeps going to the IOException. I'm just writing 20 names to a sequential file and putting four scores with each name. Then on the left side regular list order and on the right reverse list.  I was wondering if someone sees something I don't. Thanks.

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.util.*;

public class file_test
{
String names[] ={"Matt", "Mike", "Sam", "John", "Mark", "Jane", "Josh", "Hank", "Doug", "Nick", "Brad", "Eric", "Kenn", "Phil", "Adam", "Jack", "Nate", "Gary", "Bill", "Bob"};
FileOutputStream fileout;
DataOutputStream data;
String out;
public file_test()
{
DataOutputStream data=null;
for (int a=0;a<20;a++){
names[a]+="                    ";
String gg=names[a];
names[a]=gg.substring(0,19);
}
try
{
data = new DataOutputStream(new FileOutputStream("F://out.dat"));
for(int a=0;a<20;a++)
    {
            data.writeUTF(names[a]);
                               
                for(int z=0;z<4;z++)
               {
                   int test=(int)(Math.random()*101);
                   data.writeInt(test);
                   }
       }//data.close();
  }
catch (IOException E)
{
    JOptionPane.showMessageDialog (null,"File Not Found");
}
}
public void readfile()
{
try
{
RandomAccessFile raf= new RandomAccessFile("F://out.dat", "rw");
for(int a=1;a<=20;a++)
{
int p1=(a-1)*38;
raf.seek(p1);
String s1=raf.readUTF();
int s2=raf.readInt();
int s3=raf.readInt();
int s4=raf.readInt();
int s5=raf.readInt();
out=s1+s2+"  "+s3+"  "+s4+"  "+s5+"   ";
int p2=(20-a)*38;
raf.seek(p2);
String t1=raf.readUTF();
int t2=raf.readInt();
int t3=raf.readInt();
int t4=raf.readInt();
int t5=raf.readInt();
//out+=t1+t2+"  "+t3+"  "+t4+"  "+t5+"\n";
System.out.printf(out);
}
}
catch(IOException e)
{
System.out.println("You got a mistake");
}
}
public static void main (String[]args)
{
    file_test app = new file_test();
    app.readfile();
}
}
0
bigmdawg
Asked:
bigmdawg
1 Solution
 
Manikandan ThiagarajanSenior consultantCommented:
>>System.out.printf(out);

should be

System.out.print(out);

check your file is available or not
0
 
amol_chaudhariCommented:
hi bigmdawg,
                         first of all when u finish file_test function check whether the corrosponding file is created or not (check for file name F:\\out.dat). when u finish writing in the file close the handles (i.e. dataoutputstream) related to file.
                          in read file function u perform seeking by 38 bytes .. i think seek(19*(a-1)) is appropriate.......
hope this will help ................
0
 
bigmdawgAuthor Commented:
When i run the program the file is created. I made the adjustments and I'm still getting the IOException.
0
 
mrigankCommented:
Can you post the stack trace of the Exception that you are getting.
0
 
amol_chaudhariCommented:
hi
get the following code it is working

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.util.*;

public class file_test
{
String names[] ={"Matt", "Mike", "Sam", "John", "Mark", "Jane", "Josh", "Hank", "Doug", "Nick", "Brad", "Eric", "Kenn", "Phil", "Adam", "Jack", "Nate", "Gary", "Bill", "Bob"};
FileOutputStream fileout;
DataOutputStream data;
String out;
public file_test()
{
DataOutputStream data=null;
for (int a=0;a<20;a++){
names[a]+="                    ";
String gg=names[a];
names[a]=gg.substring(0,19);
}
try
{
data = new DataOutputStream(new FileOutputStream("d:\\out.dat"));
for(int a=0;a<20;a++)
    {
            data.writeUTF(names[a]);
                               
                for(int z=0;z<4;z++)
               {
                   int test=(int)(Math.random()*101);
                   data.writeInt(test);
                   }
       }//data.close();
  }
catch (IOException E)
{
    JOptionPane.showMessageDialog (null,"File Not Found");
}
}
public void readfile()
{
try
{
RandomAccessFile raf= new RandomAccessFile("d:\\out.dat", "rw");
for(int a=1;a<=20;a++)
{
long p1=(a-1)*37;
raf.seek(p1);
String s1=raf.readUTF();
int s2=raf.readInt();
int s3=raf.readInt();
int s4=raf.readInt();
int s5=raf.readInt();
out=s1+s2+"  "+s3+"  "+s4+"  "+s5+"   ";
int p2=(20-a)*37;
raf.seek(p2);
String t1=raf.readUTF();
int t2=raf.readInt();
int t3=raf.readInt();
int t4=raf.readInt();
int t5=raf.readInt();
//out+=t1+t2+"  "+t3+"  "+t4+"  "+t5+"\n";
System.out.printf(out);
}
}
catch(IOException e)
{
System.out.println("You got a mistake");
e.printStackTrace(System.out);
}
}
public static void main (String[]args)
{
    file_test app = new file_test();
    app.readfile();
}
}
bye
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now