Solved

RandomAccessFile/GUI question?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now