Solved

RandomAccessFile/GUI question?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error trying to install RTMT Win7 5 60
couple of eclipse 5 36
eclipse apache tomcat admin console 52 96
sql import cannot be resolved jsp 3 20
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

776 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