Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

RandomAccessFile/GUI question?

Posted on 2001-06-26
3
Medium Priority
?
262 Views
Last Modified: 2010-03-31
Hi

I have created a User interface that allows the user to select a file from a list. When the user selects a file they then click an open button which will then open the file, read and use the data in the file.

At the moment I just have a number in each of the files and am trying to open the file and print the number so that I know that the file is open and the data read. Here's the section of my code that reads the input file:

public void openFile(String filename) {
    System.out.println( filename );
    try {
        String file2 = path + "\\" + filename;
        standardTest = new RandomAccessFile(file2, "r");
    } catch (FileNotFoundException e) {
        System.out.println("Exception caught" + e);
    } catch (IOException e) {
        System.out.println("Exception caught" + e);
    }
       
    boolean EOF = false;
    while(!EOF) {
        try {
            int data = standardTest.readInt();
            System.out.println("Read value = " + data);
        } catch (EOFException e) {
            EOF = true;
        } catch (IOException e) {
         System.out.println("Exception caught" + e);
        }    
    }
           
}


When I run this, select a file and hit the open button it prints out the filename but does not print out the integer value in the file.

Can anyone help me with this?
Thanks
M
0
Comment
Question by:mmckeever
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
rjackman earned 150 total points
ID: 6227565
hi
try following instead of reading integer try reading the String
try
{
   //ur code for creatiing the file object
   String s1 = null;
   while((s1=standardTest.readLine())!=null)
  {
    Syatem.out.println("Contents Of File==>"+s1);
  }
}
catch(Exception rr)
{
  rr.printStackTrace();
}
0
 

Author Comment

by:mmckeever
ID: 6230407
Hi

I have used this and then parsed the string that is returned! Thanks
M
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6231327
hi there,

I hope this works


import java.io.*;

public class file1
{
     public static void main(String []args)
{
     file1 f1=new file1();
     f1.openFile("file1.java");    
}    
     public void openFile(String filename) {
       RandomAccessFile standardTest=null;
   System.out.println( filename );
String line=null;
try {
        standardTest = new RandomAccessFile(filename, "r");
        while ((line = standardTest.readLine()) != null) {
            System.out.println(line);
        }
        return;
    }
catch(Exception e1)
{
}
finally {
    }
           
}
}

bye
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question