I’d like to know how best to make “accessable” or “visible” a set of File I/O Stream constructors defined in my main routine, to sub-routines.
I found that I cannot use “public”, the compiler issues an “Illegal Expression” error msg.
When I place the file I/O stream and channel constructors in the public class defined for
the entire program “package”. The compiler issues an error stating there’s no FileNotFound or IOException handling declared, so I put on my mainline routine the following:
public static void main(String args[]) throws FileNotFoundException, IOException
{
// and if I then place the File I/O contructors after this:
//Connect to the LU62XC Message File
FileOutputStream MesgOut = new FileOutputStream(Mesg_File) ;
FileChannel MesgChnl = MesgOut.getChannel() ;
ByteBuffer Mesg_Bufr = ByteBuffer.allocate(128) ;
//Connect to the Request Input File
FileInputStream RqstInp = new FileInputStream(Rqst_File) ;
//Connect to the Response Output File
FileOutputStream RespOut = new FileOutputStream(Resp_File) ;
//Connect to the Request/Response Log File
FileOutputStream LogrOut = new FileOutputStream(Logr_File) ;
I resolve the “no exception handling error”, but now my problem is the sub-routines
that reference the “constructed” file objects essentianlly can’t .. I get a bunch of
“symbol not found” error messages. Again, if I put “public” in front of the file I/O constructors, I get the Illegal expression message.
Is there any way out of this ???
Why the java compiler insists on the program handling file-not-found errors is beyond me.
I mean there’s already the if file_object.exists() method…
IF the file’s NOT there.. the OS will let you know. All ANY application program(OOP or otherwise) does when it comes to I/O of any kind is to make a request to the underlying OS.
If you want a method to use an object you have as a local variable you can pass its as an argument. This is standard practice in just about every language I can think of. However instead of passing the FileInputStream it is better to pass the file name in camelCase
Because a local variable is local to the scope i.e. the method it is in. You can’t use it in another method.
Because public fields cannot be defined in a method, they have to be define outside a method, usually at the start of the class.
Reading a few tutorials http://www.google.com/search?q=java+turorials 11 million results, or working example http://www.google.com/search?q=java+examples 25 million results
It does let you know by throwing a FileNotFoundException. What do you expect it to do?
And the OS can return an error which you need to be able to handle.