0
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ExplicitChannelRead {

    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        FileInputStream fIn = null;
        FileChannel fChan = null;
        ByteBuffer mBuf;
        int count;

        try{
            fIn = new FileInputStream("text.txt");

            fChan = fIn.getChannel();

            mBuf = ByteBuffer.allocate(128);

            do{
                count = fChan.read(mBuf);

                if(count!=-1){
                    mBuf.rewind();

                    for(int i =0; i<count; i++)
                        System.out.print((char) mBuf.get());

                }
            }while(count!=-1);

            System.out.println();
        }catch(IOException e){
            System.out.println("I/O Error : " + e);
        }finally{
            try{
                if(fChan!=null)
                    fChan.close();
            }catch(IOException e){
                System.out.println("Error closing Channel.");
            }

            try{
                if(fIn!= null)
                    fIn.close();
            }catch(IOException e){
                System.out.println("Error closing file.");
            }
        }
    }
}

当我在命令提示符下编译此代码时,出现错误

ExplictChannelRead.java:58:error:class, interface, or enum expected }

当我在我的 IDE 中编译它时,我收到以下错误

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

at nio_path.ExplicitChannelRead.main(ExplicitChannelRead.java:12)"

我从书中复制了整个代码。

4

4 回答 4

4

对我来说很好:)

引用文本后有一个悬挂 }...确保您有匹配的大括号...

于 2012-06-13T19:34:43.333 回答
2

您的源代码中有 11 个{字符和 12 个}字符。

找到丢失的{或删除}不需要的。

于 2012-06-13T19:35:10.403 回答
0

尝试在文件末尾添加一个 },您似乎缺少一个。

于 2012-06-13T19:33:28.933 回答
0

它使用 JDK 1.7.0_01 对我来说编译得很好。

于 2012-06-13T19:37:25.253 回答