如何最好地使“可访问”成为可能文件 I/O 流构造函数(还有通道和 ByteBuffer)

发布于 2024-11-04 20:40:27 字数 1272 浏览 3 评论 0原文

我想知道如何最好地使主例程中定义的一组文件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2024-11-11 20:40:27

如果您希望方法使用您作为局部变量的对象,您可以将其作为参数传递。这是我能想到的几乎所有语言的标准做法。但是,最好不要传递 FileInputStream,而是以驼峰命名法传递文件名

// in main
process(requestFile, responseFile, logFile);

// later
public static void process(String requestFile, String responseFile, String logFile) throw IOException {
    //Connect to the Request Input File 
    FileInputStream requestIn = new FileInputStream(requestFile);  
    //Connect to the Response Output File 
    FileOutputStream responseOut = new FileOutputStream(responseFile);
    //Connect to the Request/Response Log File 
    FileOutputStream logOut = new FileOutputStream(logFile);

    requestIn.close();
    responseOut.close();
    logOut.close();
}

我收到一堆“未找到符号”错误消息。

因为局部变量是作用域(即它所在的方法)的局部变量。您不能在其他方法中使用它。

同样,如果我将“public”放在文件 I/O 构造函数前面,我会收到“非法表达式”消息。

由于公共字段不能在方法中定义,因此必须在方法外部定义它们,通常是在类的开头。

有什么办法可以解决这个问题吗???

阅读一些教程 http://www.google.com/search?q=java+turorials 1100 万条结果,或工作示例 http://www.google.com/搜索?q=java+examples 25百万个结果

为什么 java 编译器坚持让程序处理文件未找到错误,这超出了我的理解。我的意思是已经有 If file_object.exists() 方法...如果文件不存在...操作系统会让您知道。

它确实通过抛出 FileNotFoundException 来让您知道。您期望它做什么?

任何应用程序(OOP 或其他)在涉及任何类型的 I/O 时所做的都是向底层操作系统发出请求。

操作系统可能会返回一个错误,您需要能够处理该错误。

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

// in main
process(requestFile, responseFile, logFile);

// later
public static void process(String requestFile, String responseFile, String logFile) throw IOException {
    //Connect to the Request Input File 
    FileInputStream requestIn = new FileInputStream(requestFile);  
    //Connect to the Response Output File 
    FileOutputStream responseOut = new FileOutputStream(responseFile);
    //Connect to the Request/Response Log File 
    FileOutputStream logOut = new FileOutputStream(logFile);

    requestIn.close();
    responseOut.close();
    logOut.close();
}

I get a bunch of "symbol not found" error messages.

Because a local variable is local to the scope i.e. the method it is in. You can't use it in another method.

Again, if I put "public" in front of the file I/O constructors, I get the Illegal expression message.

Because public fields cannot be defined in a method, they have to be define outside a method, usually at the start of the class.

Is there any way out of this ???

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

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.

It does let you know by throwing a FileNotFoundException. What do you expect it to do?

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.

And the OS can return an error which you need to be able to handle.

彡翼 2024-11-11 20:40:27

这是基本的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文