aspose file tools*
The moose likes Beginning Java and the fly likes Creating Random Access File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating Random Access File" Watch "Creating Random Access File" New topic
Author

Creating Random Access File

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Hello, I need help to make a Random Access File.
I've went through the Java-Sun website and I know that have to write these two lines to create a RAF:

File file = new File("Name of File.dat");
RandomAccessFile raf = new RandomAccessFile(file, "rw");

But I'm not quite sure where to put them. I've tried putting outside a method and declaring them as "static". I've tried putting them in the constructor. And I even tried making a separate method just for it. But the complier keeps giving me the same error messages:

-cannot resolve symbol variable file
-cannot resolve symbol variable raf

why?


Olly
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

But I'm not quite sure where to put them. I've tried putting outside a method and declaring them as "static". I've tried putting them in the constructor. And I even tried making a separate method just for it. But the complier keeps giving me the same error messages:


Can you provide a more detailed version of your source, that replicates the problem?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

import java.io.*;
class PRACTEST_RAF
{
static PRACTEST_RAF exe = new PRACTEST_RAF();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int menuvar;// Used by switch statement

PRACTEST_RAF()
{
File file = new File("RandomAccessFileExample.out");
RandomAccessFile raf = new RandomAccessFile(file, "rw");

}

public static void menu()throws IOException
{
System.out.println("1:Add Record");
System.out.println("2:Read Record");
System.out.println("0:Exit");

exe.menuvar=Integer.parseInt(exe.br.readLine());
switch(menuvar)
{
case 1: exe.Add();
break;
case 2: exe.Read();
break;
case 0: System.exit(0);
break;
default:System.out.println("Error! Incorrect Command");
exe.menu();
break;
}
}

public void Add()throws IOException
{
//Getting the values
System.out.print("\nBook code? > ");
int Book_code=Integer.parseInt(br.readLine());
System.out.print("\nBook title? > ");
String Book_Title=br.readLine();
System.out.print("\nQty on hand? > ");
int Qty_on_Hand=Integer.parseInt(br.readLine());
System.out.print("\nCost Price? > ");
int Cost_Price=Integer.parseInt(br.readLine());
System.out.print("\nSelling Price?> ");
int Selling_Price=Integer.parseInt(br.readLine());

//writing the values to a file
raf.seek(exe.file.length());
raf.writeInt(Book_code);
raf.writeBytes(Book_Title);
raf.writeInt(Qty_on_Hand);
raf.writeInt(Cost_Price);
raf.writeInt(Selling_Price);
}

public void Read() {} //Ignore this method. It's not important

public static void main(String[]args)throws IOException
{
exe.menu();
}
}
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It is a matter of scope and visibility. The variables you declared in your constructor are not visible to other code, and they become eligible for garbage collection when the constructor exits. Move them to member variables to make them visible to other methods. Here's one way to change things:

Does that rearranging seem to make sense? Check your Java book or the Sun language tutorial for more on scope & visibility.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating Random Access File