質問

いきな打tail-f'Java.ようにしていを読み込むにはログファイルで書かないようにしてしまっているんですけどファイルを開くと、はにロックしているファイルに他のプロセスできない書き込みます。のお役に立てるようお願い申し上げます!

こちらのコードを使用してい現

public void read(){
    Scanner fp = null;
    try{
        fp = new Scanner(new FileReader(this.filename));
        fp.useDelimiter("\n");
    }catch(java.io.FileNotFoundException e){
        System.out.println("java.io.FileNotFoundException e");
    }
    while(true){
        if(fp.hasNext()){
            this.parse(fp.next());
        }           
    }       
}
役に立ちましたか?

解決

再生テールはトリッキーにより特別な場合のようにファイルを前方一致、中間)削除します。開を行なわずにファイルのロック用 StandardOpenOption.READ 新しいJavaファイルAPIはこのように:

try (InputStream is = Files.newInputStream(path, StandardOpenOption.READ)) {
    InputStreamReader reader = new InputStreamReader(is, fileEncoding);
    BufferedReader lineReader = new BufferedReader(reader);
    // Process all lines.
    String line;
    while ((line = lineReader.readLine()) != null) {
        // Line content content is in variable line.
    }
}

私の作成を試み、尾Java参照:

ご自由に発想からコード又は単にコピーしてパーツがあります。んだ問題がん意識している。

他のヒント

のFileChannel API を見ここを。ファイルをロックするために、あなたはここにを確認することができます

   java.ioはあなたの必須ファイルロックを与え、java.nioのはあなたに与えます   諮問ファイルロック

あなたはクラスの下に使用することができます任意のロックせずに任意のファイルを読みたい場合は、

import java.nio.channels.FileChannel;
import java.nio.file.Paths;

あなたは末尾に同じ

のためのコードの下のラインの使用によるファイルの行をしたい場合
public void tail(String logPath){
    String logStr = null;
    FileChannel fc = null;
    try {
        fc = FileChannel.open(Paths.get(logPath), StandardOpenOption.READ);
        fc.position(fc.size());
    } catch (FileNotFoundException e1) {
        System.out.println("FileNotFoundException occurred in Thread : " + Thread.currentThread().getName());
        return;
    } catch (IOException e) {
        System.out.println("IOException occurred while opening FileChannel in Thread : " + Thread.currentThread().getName());
    }
    while (true) {
        try {
            logStr = readLine(fc);
            if (logStr != null) {
                System.out.println(logStr);
            } else {
                Thread.sleep(1000);
            }
        } catch (IOException|InterruptedException e) {
            System.out.println("Exception occurred in Thread : " + Thread.currentThread().getName());
            try {
                fc.close();
            } catch (IOException e1) {
            }
            break;
        }
    }
}

private String readLine(FileChannel fc) throws IOException {
    ByteBuffer buffers = ByteBuffer.allocate(128);
    // Standard size of a line assumed to be 128 bytes
    long lastPos = fc.position();
    if (fc.read(buffers) > 0) {
        byte[] data = buffers.array();
        boolean foundTmpTerminator = false;
        boolean foundTerminator = false;
        long endPosition = 0;
        for (byte nextByte : data) {
            endPosition++;
            switch (nextByte) {
            case -1:
                foundTerminator = true;
                break;
            case (byte) '\r':
                foundTmpTerminator = true;
                break;
            case (byte) '\n':
                foundTmpTerminator = true;
                break;
            default:
                if (foundTmpTerminator) {
                    endPosition--;
                    foundTerminator = true;
                }
            }
            if (foundTerminator) {
                break;
            }
        }
        fc.position(lastPos + endPosition);
        if (foundTerminator) {
            return new String(data, 0, (int) endPosition);
        } else {
            return new String(data, 0, (int) endPosition) + readLine(fc);
        }
    }
    return null;
}
あなたが開いている間、あなたが右の共有フラグを指定しない限り、

はWindowsがファイルに対して強制ロックを使用しています。あなたがビジー状態のファイルを開きたい場合は、共有フラグCreateFileでのWin32-APIのFILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITEへのハンドルを必要とします。

これは、属性やものを読み取るため、開いているファイルへのいくつかの場所でJDKの内部で使用されるが、限り、私はそれがエクスポートされていない/ Javaのクラスライブラリレベルに利用できる見ることができるようにされます。それを行うためにネイティブライブラリを見つける必要があるでしょうあなたはとてもます。

あなたの周りの迅速な作業が、コマンドからprocess.getInputStream()を読むことができるように私は思う"cmd /D/C type file.lck"

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top