Solved

RandomAccessFile/GUI question?

Posted on 2001-06-26
3
253 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
3 Comments
 
LVL 3

Accepted Solution

by:
rjackman earned 50 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

790 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