Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //OS_HW01_2 161514 17/18
- import java.io.*;
- public class OS_HW01_2 {
- public static void main(String[] args) throws IOException {
- // File dir = new File(System.getProperty("user.dir"));
- String pathRead = "src/texts/source.txt";
- String pathWrite = "src/texts/destination.txt";
- // File source = new File(pathRead);
- // source.mkdirs(); //this makes a dir, not a txt file
- // File destination = new File(pathWrite);
- // destination.mkdirs(); //this makes a dir, not a txt file
- BufferedInputStream in = new BufferedInputStream(new FileInputStream(pathRead));
- PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(pathWrite)));
- copyStreamReversed(in, out);
- }
- public static void copyStreamReversed(InputStream in, OutputStream out)
- throws IOException {
- try {
- // int c;
- byte[] buffer = new byte[100];
- // while ((c = in.read()) != -1)
- // ;
- in.read(buffer);
- reverse(buffer);
- out.write(buffer); //ask why?
- } finally {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- }
- }
- public static void reverse(byte[] arr) {
- if (arr == null) {
- return;
- }
- int i = 0;
- int j = arr.length - 1;
- byte tmp;
- while (j > i) {
- tmp = arr[j];
- arr[j] = arr[i];
- arr[i] = tmp;
- j--;
- i++;
- }
- }
- }
Add Comment
Please, Sign In to add comment