This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Creating Random Access File  RSS feed

 
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
author
Sheriff
Posts: 23586
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!