- public static void main(String[] args) {
- ThreadFile1 tf1 = new ThreadFile1("1231234342349283498123409182340981234981723098471203984710928340198374091837409182340981230498145234529348509238509283-5982-309485-2039485-209348509458348569845983495839458923592374895");
- ThreadFile1 tf2 = new ThreadFile1("asdkfakjsdhflkjhsdlfkjhasdlkjfhalsdkjfhalksjdfhlakjsdfhlakjsdflkajsdnflkjahsdflkjahsdkfjhasdk");
- tf2.start();
- tf1.start();
- }
- }
- //线程里面对abc.txt进行写操作
- class ThreadFile1 extends Thread {
-
- String str = null;
- public ThreadFile1(String str){
- this.str = str;
- }
- @Override
- public void run(){
- File file = new File("abc.txt");
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(file);
-
- fos.write(new String(str).getBytes());
- fos.flush();
- Thread.sleep(3000);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- try {
- fos.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
-
- }
复制代码
得到的结果是 在abc.txt里面两个字符串串在一起。 我的问题是为什么abc.txt里面是两个串在一起而不是只有一个str(先有一个线程创建abc.txt,然后被另外一个线程创建abc.txt给覆盖了.
|