Thread.sleep()和FileWriter()

时间:2018-04-15 11:01:51

标签: java multithreading filewriter bufferedwriter

我想创建一个每5秒写一个文件的java程序。现在 这段代码的问题在于" Thread.sleep(5000)"," bw.write(s)"什么都写不出来" b.txt"文件,它仍然是空的。请有人告诉我发生了什么。

import java.lang.*;
import java.io.*;
public class Threading{
public static void main(String args[]){
//File f=new File("Rand2.java");
String s="hello";
BufferedWriter bw=null;
FileWriter fw=null;

fw=new FileWriter("b.txt");
bw=new BufferedWriter(fw);


while(true){

    try{
        Thread.sleep(5000);

    }catch(InterruptedException ie){}
    try{    
    bw.write(s);
    //bw.write((int)f.length());
    System.out.println("Hey! I just printed something.");
    }catch(IOException io){System.out.println("IOException");}

}
}

}

1 个答案:

答案 0 :(得分:1)

您需要在// add animation to remap (without affecting other elements) let animationLayer = CALayer() layer?.addSublayer(animationLayer). // (don't use optional for iOS) // set the layer origin to the center of the view (or any center point for the animation) animationLayer.bounds.origin .x = -self.bounds.size.width / 2.0 animationLayer.bounds.origin .y = -self.bounds.height / 2.0 // make image layer with circle around the zero point let imageLayer = CAShapeLayer() animationLayer.addSublayer(imageLayer) let shapeBounds = CGRect(x: -bounds.size.width/2.0, y:-bounds.size.height/2.0, width: bounds.size.width, height: bounds.size.height) imageLayer.path = CGPath(ellipseIn: shapeBounds, transform: nil) imageLayer.fillColor = fillColour // **** apply your animations to imageLayer here **** 之后致电flush()。它被称为write(),因为它将数据缓存在缓冲区中,除非你强制它进行刷新,否则它可能会认为它还没有时间实际写入任何东西。