Solved

RandomAccessFile/GUI question?

Posted on 2001-06-26
3
257 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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

728 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