Created
February 5, 2016 13:46
-
-
Save Viacheslav77/a46a087e39eb0cd24cda to your computer and use it in GitHub Desktop.
Написать класс, который позволит записывать текстовые данные в один файл из разных потоков.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package FileWriteTread; | |
| import java.io.FileNotFoundException; | |
| import java.io.IOException; | |
| import java.io.RandomAccessFile; | |
| import java.util.RandomAccess; | |
| public class FileWrite { | |
| byte [] buf; | |
| static int cursor; | |
| static int nuberStream; | |
| String pathFile; | |
| public FileWrite (byte [] buf, String pathFile) throws FileNotFoundException, IOException{ | |
| this.buf=buf; | |
| this.pathFile=pathFile; | |
| try (RandomAccessFile out = new RandomAccessFile(pathFile,"rw")) | |
| { | |
| out.seek (cursor); | |
| out.write(buf,0,buf.length); | |
| cursor+=buf.length; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package FileWriteTread; | |
| // Написать класс, который позволит записывать текстовые данные в один файл из разных потоков. | |
| public class MyClass { | |
| public static void main(String[]args){ | |
| String[] text = {"Hello", "World","!"}; | |
| String path = "d:\\1\\FileWriteThreat.txt"; | |
| for(String s: text){ | |
| threadOne to = new threadOne(s+" ",path); | |
| to.start(); | |
| synchronized (to){ | |
| try { | |
| to.wait(); | |
| } catch (InterruptedException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package FileWriteTread; | |
| import java.io.FileNotFoundException; | |
| import java.io.IOException; | |
| public class threadOne extends Thread { | |
| String word; | |
| static int numberThread; | |
| String pathFile; | |
| public threadOne (String word, String pathFile ){ | |
| this.word=word; | |
| this.pathFile = pathFile; | |
| } | |
| public void run (){ | |
| byte[] buf; | |
| buf = word.getBytes(); | |
| try { | |
| FileWrite fw = new FileWrite(buf, pathFile); | |
| } catch (FileNotFoundException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment