Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package streams2;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class app {
- public static void main(String[] args) throws IOException {
- //macierz();
- //echo();
- bitFileCopy("t.png", "t2.png");
- }
- private static void echo() throws IOException
- {
- TextRew t = new TextRew();
- while(!t.stop())
- {
- t.readLine();
- t.printR();
- }
- }
- private static void macierz() throws IOException
- {
- Macierz m = new Macierz();
- m.printToFile();
- m.printAv();
- m.readFromFile();
- m.printAv();
- m.serializeToFile();
- m.deserializeFromFile();
- //m.bitFileCopy("CloneFile.txt");
- }
- public static int bitFileCopy(String fName, String toFile) throws FileNotFoundException, IOException
- {
- File from = new File(fName);
- File target = new File(toFile);
- FileInputStream in = new FileInputStream (from);
- FileOutputStream out = new FileOutputStream (target);
- int counter = 0;
- int triger = 0;
- byte [] b = new byte [1024];
- do
- {
- triger = in.read(b);
- if (triger > 0)
- {
- counter += triger;
- out.write(b);
- }
- }
- while(triger != -1);
- in.close();
- out.close();
- return counter;
- }
- }
- package streams2;
- import java.io.*;
- import java.util.Random;
- import java.util.Scanner;
- public class Macierz implements Serializable
- {
- protected double[][] mac;
- protected File sFile;
- public File bitFile;
- public Macierz()
- {
- sFile = new File("Macierz.txt");
- bitFile = new File("bitFile.txt");
- mac = new double[5][];
- for (int i = 0; i < 5; i++)
- {
- mac[i] = new double[5];
- }
- Random r = new Random();
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- mac[i][j] = r.nextDouble()* 100;
- }
- }
- }
- public void printToFile() throws IOException
- {
- BufferedWriter w = new BufferedWriter(new FileWriter(sFile));
- w.write("Macierz");
- w.newLine();
- w.write("" + mac.length);
- w.newLine();
- w.write("" + mac[0].length);
- w.newLine();
- for (int i = 0; i < mac.length; i++)
- {
- for (int j = 0; j < mac[i].length; j++)
- {
- w.write(String.format("%.2f\t", mac[i][j]));
- }
- w.newLine();
- }
- //System.out.println(sFile.getCanonicalPath());
- w.close();
- }
- public void readFromFile() throws IOException
- {
- FileReader r = new FileReader(sFile);
- Scanner s = new Scanner(sFile);
- s.nextLine();
- int w = s.nextInt(), k = s.nextInt();
- mac = new double [w][];
- for (int i = 0; i < w; i++)
- {
- mac[i] = new double[k];
- }
- for(int i = 0; i < w; i++)
- for(int j = 0; j < k; j++)
- {
- mac[i][j] = s.nextDouble();
- }
- }
- public void printAv()
- {
- PrintWriter w = new PrintWriter(System.out, true);
- double a = 0;
- for(int i = 0; i < 5; i++)
- {
- for(int j = 0; j < 5; j++)
- {
- a += mac[i][j];
- }
- }
- w.println("" + a/25);
- }
- public void serializeToFile() throws IOException
- {
- FileOutputStream w = new FileOutputStream(bitFile);
- DataOutputStream out = new DataOutputStream(w);
- out.writeInt(mac.length);
- out.writeInt(mac[0].length);
- for(int i = 0; i < mac.length; i++)
- for(int j = 0; j < mac[i].length; j++)
- out.writeDouble(mac[i][j]);
- out.close();
- w.close();
- }
- public void deserializeFromFile() throws IOException
- {
- FileInputStream wej = new FileInputStream(bitFile);
- DataInputStream in = new DataInputStream(wej);
- int w = in.readInt();
- int k = in.readInt();
- mac = new double[w][];
- for(int i = 0; i < w; i++)
- {
- mac[i] = new double[k];
- for(int j = 0; j < k; j++)
- mac[i][j] = in.readDouble();
- }
- in.close();
- wej.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement