package streams;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

 

public class ReadFile {

    public static void main(String[] args) {

        File f = new File("src/bytes.txt");

        FileInputStream fis = null;

        try {

            fis = new FileInputStream(f);

            int content;

            while ((content = fis.read()) != -1) {

                System.out.println(content);

            }

        } catch (IOException e) {

            e.printStackTrace(System.out);

        } finally {

            try {

                fis.close();

            } catch (IOException e) {

                e.printStackTrace(System.out);

            }

        }        

    }

}

 

 

 

 

package streams;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

 

public class ReadFile2 {

    public static void main(String[] args) {

        File f = new File("src/bytes.txt");

        try (FileInputStream fis = new FileInputStream(f)) {

            int content;

            while ((content = fis.read()) != -1) {

                System.out.println(content);

            }

        } catch (IOException e) {

            e.printStackTrace(System.out);

        }

    }

}

 

 

 

 

package streams;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

 

public class WriteFile {

    public static void main(String[] args) {

        File f = new File("src/bytes_output.txt");

        FileOutputStream fos = null;

        BufferedOutputStream bos = null;

        try {

            fos = new FileOutputStream(f);

            bos = new BufferedOutputStream(fos);

            for (int i = 0; i < 100; i++) {

                bos.write(("Hello world "+i+"\n").getBytes());

                if (i%10 == 0) {

                    bos.flush();

                }

            }                

        } catch (IOException e) {

            e.printStackTrace(System.out);

        } finally {

            try {

                bos.flush();

                fos.close();

            } catch (IOException e) {

                e.printStackTrace(System.out);

            }

        }        

    }

}

 

 

 

 

package streams;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

 

public class ReaderExample {

    public static void main(String[] args) {

        File srcDirectory = new File("src");

        System.out.println(srcDirectory.isDirectory());

        File[] files = srcDirectory.listFiles();

        for (int i = 0; i < files.length; i++) {

            File f = files[i];

                if (f.isFile()) {

                    System.out.printf("Processing file %s\n\n", f.getName());

                    BufferedReader br = null;

                    FileReader fr = null;

                    try {

                        fr = new FileReader(f);

                        br = new BufferedReader(fr);

                        String line;

                        while ((line = br.readLine()) != null) {

                            System.out.println(line);

                         }

                         System.out.println("\n");

                    } catch (IOException e) {

                        e.printStackTrace(System.out);

                    } finally {

                        try {

                            br.close();

                        } catch (IOException e) {

                            e.printStackTrace(System.out);

                        }

                    }              

               }

          }

    }

}

 

 

 

 

package streams;

import java.io.IOException;

import java.nio.file.FileVisitResult;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.SimpleFileVisitor;

import java.nio.file.attribute.BasicFileAttributes;

 

public class ReaderExample {

    public static void main(String[] args) throws IOException {

        Path path = Paths.get("src");

        MyFileVisitor fileVisitor = new MyFileVisitor();

        Files.walkFileTree(path, fileVisitor);

    }

 

    private static class MyFileVisitor extends SimpleFileVisitor<Path> {

        @Override

        public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) throws IOException {

            for (String line : Files.readAllLines(path)) {

                System.out.println(line);

            }

            System.out.println("\n");

            return FileVisitResult.CONTINUE;

        }

       

        @Override 

        public FileVisitResult preVisitDirectory(Path directory, BasicFileAttributes attributes) throws IOException {

            return FileVisitResult.CONTINUE;

        }

    }

}

 

 

 

 

 

public static void main(String[] args) {

    File dir = new File("src/newdir/childdir");

    dir.mkdirs();

    File output = new File(dir, "output.txt");

    try (BufferedWriter br =  new BufferedWriter(new FileWriter(output))) {

        for (int i = 0; i <= 100; i++) {

            br.append("Line number "+i+"\n");

        }

    } catch (IOException e) {

        e.printStackTrace(System.out);

    }

}

 

 

 

 

 

package server;

import java.io.Serializable;

import java.util.Date;

 

public class Address implements Serializable {

    private static final long serialVersionUID = -7256403555699920549L;

    private String street;

    private Integer streetNumber;

    private String city;

    private Integer postcode;

    private transient Date createDate;

     

    public Address() {

        createDate = new Date();

    }

 

    public Address(String street, Integer streetNumber, String city) {

        this();

        this.street = street;

        this.city = city;

        this.streetNumber = streetNumber;

    }

/* Getter and setter fields have been omitted for brevity, but are required by the program */

}

 

 

 

 

 

package server;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.ServerSocket;

import java.net.Socket;

 

public class SocketServer {

 

    public static void main(String[] args) {

        try {

            ServerSocket serverSocket = new ServerSocket(9090);

            System.out.println("The server is running");

            while (true) {

                Socket client = serverSocket.accept();

                System.out.println("Client accepted");

                ObjectInputStream ois = new

                  ObjectInputStream(client.getInputStream());

               Address address = (Address)ois.readObject();         

               ObjectOutputStream oos = new  

                   ObjectOutputStream(client.getOutputStream());

               address.setPostcode((int)System.currentTimeMillis() %100000);

                oos.writeObject(address);

                oos.flush();

            }

        } catch (IOException e) {

            e.printStackTrace(System.out);

        } catch (ClassNotFoundException e) {

            e.printStackTrace(System.out);

        }

    }

}

 

 

 

 

 

package server;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.Socket;

 

public class SocketClient {

 

    public static void main(String[] args) {

        try {

           Socket s = new Socket("localhost", 9090);

           ObjectOutputStream oos = new

             ObjectOutputStream(s.getOutputStream());

           Address a = new Address("Main Road", 99, "Montgomory");

           oos.writeObject(a);

           oos.flush();

            ObjectInputStream ois = new

                ObjectInputStream(s.getInputStream());

             a = (Address)ois.readObject();

             System.out.println("The postcode is "+a.getPostcode());

        } catch (IOException e) {

             e.printStackTrace(System.out);

        } catch (ClassNotFoundException e) {

            e.printStackTrace(System.out);

        }  

    }

}