Created
February 1, 2016 23:34
-
-
Save Viacheslav77/532aa7bc90524460b82f to your computer and use it in GitHub Desktop.
1 Написать программу копирования файла блоками (1 поток на блок). 2. Написать программу копирования файла с выводом прогресса в % на консоль.
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 CopyFileThread; | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| public class CopyFileThread { | |
| public static void copyFile(String src, String dest) throws Exception { | |
| System.out.print("Копируем файл: " + src + "... в файл: " + dest + "."); | |
| byte[] buf; | |
| buf = new byte[3]; | |
| int size; | |
| int p; | |
| File f = new File (src); | |
| size = (int) f.getTotalSpace() /100000000 ; | |
| System.out.println("Total File" + src + " Bytes: " + size + " Потоков : " + size /buf.length); | |
| System.out.print("Прогресс : "); | |
| p = 100/(size /buf.length); | |
| for (int i = 0; i< size /buf.length; i++){ | |
| threadOne to = new threadOne (buf, src, dest); | |
| to.start (); | |
| System.out.print(p); | |
| p+=100/(size /buf.length); | |
| try { | |
| Thread.sleep(1000); | |
| } catch (InterruptedException e) { | |
| // TODO Auto-generated catch block | |
| //e.printStackTrace(); | |
| } | |
| } | |
| System.out.print("100 %.. "); | |
| } | |
| } |
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 CopyFileThread; | |
| /* | |
| 1 Написать программу копирования файла блоками (1 поток на блок). | |
| 2. Написать программу копирования файла с выводом прогресса в % на | |
| консоль. | |
| */ | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| import java.io.FilenameFilter; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| public class MyClass { | |
| public static void main(String[] args) throws Exception { | |
| CopyFileThread cf = new CopyFileThread(); | |
| cf.copyFile("d:\\1\\nn1.txt", "d:\\2\\nn1.txt"); | |
| } | |
| } |
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 CopyFileThread; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.RandomAccessFile; | |
| public class threadOne extends Thread { | |
| byte[] buf; | |
| static int counter; | |
| String src; | |
| String dest; | |
| public threadOne (byte[] buf, String src, String dest){ | |
| this.buf=buf ; | |
| this.src=src; | |
| this.dest = dest; | |
| } | |
| public void run () { | |
| try { | |
| FileInputStream in = new FileInputStream(src); | |
| try { | |
| RandomAccessFile out = new RandomAccessFile(dest, "rw"); | |
| try { | |
| int r; | |
| in.skip(counter); | |
| r = in.read(buf, 0, buf.length); | |
| if (r > 0) | |
| out.seek(counter); | |
| out.write(buf, 0 , buf.length); | |
| } finally { | |
| out.close(); | |
| } | |
| } finally { | |
| in.close(); | |
| } | |
| } catch (IOException e) { | |
| System.out.println("IOExeption"); | |
| } | |
| System.out.print( "% .."); | |
| counter +=buf.length; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment