如何最好地使“可访问”成为可能文件 I/O 流构造函数(还有通道和 ByteBuffer)
我想知道如何最好地使主例程中定义的一组文件 I/O 流构造函数对子例程“可访问”或“可见”。
我发现我无法使用“public”,编译器会发出“非法表达式”错误消息。
当我将文件 I/O 流和通道构造函数放置在为 整个程序“包”。编译器发出错误,指出没有声明 FileNotFound 或 IOException 处理,因此我在主线例程中添加了以下内容:
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) ;
我解决了“无异常处理错误”,但现在我的问题是子例程 引用“构造的”文件对象本质上不能..我得到了一堆 “找不到符号”错误消息。同样,如果我将“public”放在文件 I/O 构造函数前面,我会收到“非法表达式”消息。
有什么办法可以解决这个问题吗???
为什么 java 编译器坚持让程序处理文件未找到错误,我无法理解。 我的意思是已经有 if file_object.exists()
方法...
如果文件不存在...操作系统会让您知道。任何应用程序(OOP 或其他)在涉及任何类型的 I/O 时所做的都是向底层操作系统发出请求。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望方法使用您作为局部变量的对象,您可以将其作为参数传递。这是我能想到的几乎所有语言的标准做法。但是,最好不要传递 FileInputStream,而是以驼峰命名法传递文件名
因为局部变量是作用域(即它所在的方法)的局部变量。您不能在其他方法中使用它。
由于公共字段不能在方法中定义,因此必须在方法外部定义它们,通常是在类的开头。
阅读一些教程 http://www.google.com/search?q=java+turorials 1100 万条结果,或工作示例 http://www.google.com/搜索?q=java+examples 25百万个结果
它确实通过抛出 FileNotFoundException 来让您知道。您期望它做什么?
操作系统可能会返回一个错误,您需要能够处理该错误。
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.
这是基本的 Java:您必须将引用传递给需要它们的方法,或者将它们创建为实例变量并实例化一个类。
我建议您阅读“入门”教程:http://download.oracle。 com/javase/tutorial/java/index.html
This is basic Java: you have to pass references the methods that need them, or create them as instance variables and instantiate a class.
I suggest you read the "Getting Started" tutorial: http://download.oracle.com/javase/tutorial/java/index.html