다음은 잘 작동하는 것을 보여주는 예입니다.
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
File file = File.createTempFile("deleteme", ".txt");
file.deleteOnExit();
Thread main = Thread.currentThread();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
do {
Thread.sleep(200);
long length = file.length();
System.out.println("File " + file + " is " + length + " bytes long.");
} while(main.isAlive());
System.out.println("Finished");
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
}, "monitor");
t.start();
FileOutputStream fos = new FileOutputStream(file);
for(int i=0;i<2000;i++) {
fos.write("words words words words words words words words words words words\n".getBytes());
Thread.sleep(1);
}
fos.close();
}
}
인쇄 뭔가있다.
File /tmp/deleteme4214599935706768614.txt is 11880 bytes long.
File /tmp/deleteme4214599935706768614.txt is 23562 bytes long.
File /tmp/deleteme4214599935706768614.txt is 35376 bytes long.
File /tmp/deleteme4214599935706768614.txt is 47256 bytes long.
File /tmp/deleteme4214599935706768614.txt is 59136 bytes long.
File /tmp/deleteme4214599935706768614.txt is 70950 bytes long.
File /tmp/deleteme4214599935706768614.txt is 82830 bytes long.
File /tmp/deleteme4214599935706768614.txt is 94644 bytes long.
File /tmp/deleteme4214599935706768614.txt is 106524 bytes long.
File /tmp/deleteme4214599935706768614.txt is 118338 bytes long.
File /tmp/deleteme4214599935706768614.txt is 130218 bytes long.
File /tmp/deleteme4214599935706768614.txt is 132000 bytes long.
Finished
재현 가능한 완벽한 예를 게시하십시오. 도대체 왜 더블을 쓰고 있니? 파일에 절반의 바이트가 있다고 생각합니까? –
첫째로, 왜'long'을'double'으로 저장하고 있습니까? 둘째,이 코드는 길이가 인쇄되는 곳을 볼 수 없다는 점을 제외하고는 정확합니다. 문제가 있다고 생각됩니다. –
이렇게하는 방법입니다. 반복적으로 호출 할 때 이것이 작동하지 않는다면, 당신은 다른 뭔가 잘못하고있는 것입니다. –