锁定文件并阅读

时间:2014-03-22 10:56:07

标签: java multithreading

这是一项学习任务。我想要多个线程来读取目录中的文件。如果一个线程找到一个未被占用的文件,它会读取该文件。否则线程将被终止。

我正在尝试锁定文件以供阅读,然后逐个符号地读取它。

但是在readFile metod中我得到了IOException。你能帮我理解如何解决这个问题吗?

这是我的代码:

private void burden() {

        File mainFolder = new File("C:\\FilesToRead");
        File[] files = mainFolder.listFiles();
        String freeFile;
        boolean jobExists = false;

        for (File file : files) {

            FileChannel channel;
            FileLock lock = null;
            try {
                channel = new RandomAccessFile(file, "rw").getChannel();;
                lock = channel.lock();
                // Ok. We get the lock
                String fl = file.getAbsolutePath();
                readFile(file);
                System.out.println(fl + " is captured.");
                System.out.println("Reading " + file);
            } catch (OverlappingFileLockException e) {
                continue; // File is open by someone else                 
            } catch (FileNotFoundException f) {

            } catch (IOException ex) {

            } catch (NonWritableChannelException n) {
                System.out.println("NonWritableChannelException");
            } finally {
                try {
                    lock.release();
                    System.out.println(file.getName() + " is released");
                } catch (IOException ex) {
                    System.out.println("IOException!");
                }
            }
        }
    } // burden();

    private void readFile(File file) {

        System.out.println("Reading " + file);

        FileReader inputStream = null;
        ArrayList<Integer> list = new ArrayList<>();
        try {
            int c;
            inputStream = new java.io.FileReader(file);

            while ((c = inputStream.read()) != -1) {
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
                list.add(c);
            }

        } catch (IOException e) {
            System.out.println("IOException!");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ex) {
                    System.out.println("IOException!");
                }
            }
        }

    }

这是图片(我将错误信息翻译成英文): enter image description here

稍后添加:这是我在调试器中得到的: at java.io.FileInputStream.readBytes(Native Method)

at java.io.FileInputStream.read(FileInputStream.java:272)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:126)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at parallelprogramming.Thrd.readFile(Thrd.java:82)
at parallelprogramming.Thrd.burden(Thrd.java:50)
at parallelprogramming.Thrd.run(Thrd.java:23)
at parallelprogramming.HotThrd.run(HotThrd.java:6)
at parallelprogramming.Thrd.<init>(Thrd.java:18)
at parallelprogramming.HotThrd.<init>(HotThrd.java:3)
at parallelprogramming.ThrdPool.addHotThrd(ThrdPool.java:40)
at parallelprogramming.ThrdPool.<init>(ThrdPool.java:29)
at parallelprogramming.ParallelProgramming.main(ParallelProgramming.java:18)

1 个答案:

答案 0 :(得分:1)

来自Javadoc

  

代表整个Java虚拟机保存文件锁。他们   是不适合控制多个线程对文件的访问   在同一个虚拟机中。

强调我的。