Advertisement
JKJK__

java ex3

Nov 18th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.95 KB | None | 0 0
  1. //------------------------------------Files---------------------------------------
  2.  
  3.  
  4.  
  5. import java.io.*;
  6.  
  7. /**
  8.  * The example shows the usage of the File class. The one parameter (file name
  9.  * or directory name) is needed
  10.  *
  11.  * @author Gall Anonim
  12.  * @version 1.2
  13.  */
  14. public class FileDemo {
  15.  
  16.     /**
  17.      * The main method of the application
  18.      *
  19.      * @param args the name of the file system element (file or directory)
  20.      */
  21.     public static void main(String args[]) {
  22.  
  23.         if (args.length != 1) {
  24.             System.err.println("Give a file name or directory name!");
  25.             return;
  26.         }
  27.         String name = args[0];
  28.         File file = new File(name);
  29.  
  30.         if (!file.exists()) {
  31.             System.err.println(name + " does't exist!");
  32.             return;
  33.         }
  34.  
  35.         if (file.isFile()) {
  36.             if (!file.canRead()) {
  37.                 System.err.println(name + " can't be read!");
  38.                 return;
  39.             } else {
  40.                 try (FileInputStream in = new FileInputStream(name)) {
  41.                     byte buffer[] = new byte[1024];
  42.                     while (true) {
  43.                         int byteCount = in.read(buffer);
  44.                         if (byteCount == -1)
  45.                             break;
  46.                         System.out.write(buffer, 0, byteCount);
  47.                     }
  48.                 } catch (IOException e) {
  49.                     System.err.println(e.getMessage());
  50.                     return;
  51.                 }
  52.             }
  53.         } else // directory
  54.         {
  55.             System.out.println(name + " includes: ");
  56.             // List of directory elements    
  57.  
  58.             for (String element: file.list()) {
  59.                 System.out.print(element);
  60.                 File dir = new File(name + "\\" + element);
  61.                 if (dir.isDirectory())
  62.                     System.out.print("\t is a direcory");
  63.                 System.out.println();
  64.             }
  65.         }
  66.     }
  67. }
  68.  
  69.  
  70.  
  71.  
  72. //------------------------------------Properties---------------------------------------
  73.  
  74.  
  75.  
  76.  
  77. import java.io.*;
  78. import java.util.*;
  79.  
  80. /**
  81.  * Application presents how to use application properties.
  82.  *
  83.  * @author Gall Anonim
  84.  * @version 1.0
  85.  */
  86. public class ApplicationProperties {
  87.  
  88.     /**
  89.      * Main method of the application
  90.      *
  91.      * @param args the command line arguments
  92.      */
  93.     public static void main(String[] args) {
  94.  
  95.         /* initialization */
  96.         Properties properties = new Properties();
  97.         properties.setProperty("login", "duke");
  98.         properties.setProperty("pass", "java");
  99.  
  100.         /* writing properties */
  101.         try (FileOutputStream out = new FileOutputStream(".properties")) {
  102.             properties.store(out, "--Konfiguracja--");
  103.         } catch (IOException e) {
  104.             System.err.println(e.getMessage());
  105.         }
  106.  
  107.         /* writing properties into xml file*/
  108.         try (FileOutputStream out = new FileOutputStream("app.xml")) {
  109.             properties.storeToXML(out, "--Konfiguracja--");
  110.         } catch (IOException e) {
  111.             System.err.println(e.getMessage());
  112.         }
  113.  
  114.         /* reading properties */
  115.         try (FileInputStream in = new FileInputStream(".properties")) {
  116.             properties.load(in);
  117.             System.out.println("\n.property");
  118.             System.out.println("login=" + properties.getProperty("login"));
  119.             System.out.println("pass=" + properties.getProperty("pass"));
  120.             System.out.println("no property=" + properties.getProperty("no property"));            
  121.             System.out.println("no property=" + properties.getProperty("no property","no value"));            
  122.         } catch (IOException e) {
  123.             System.err.println(e.getMessage());
  124.         }
  125.  
  126.         /* reading properties from xml file*/
  127.         try (FileInputStream in = new FileInputStream("app.xml")) {
  128.             properties.loadFromXML(in);
  129.             System.out.println("\n.app.xml");
  130.             System.out.println("login=" + properties.getProperty("login"));
  131.             System.out.println("pass=" + properties.getProperty("pass"));
  132.         } catch (IOException e) {
  133.             System.err.println(e.getMessage());
  134.         }
  135.  
  136.     }
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. import java.util.Enumeration;
  148.  
  149. /**
  150.  * Application presents values of the system properties.
  151.  *
  152.  * @author Gall Anonim
  153.  * @version 1.0
  154.  */
  155. public class SystemProperties {
  156.  
  157.     /**
  158.      * Main method of the application
  159.      *
  160.      * @param args the command line arguments
  161.      */
  162.     public static void main(String[] args) {
  163.         java.util.Properties systemProperties = System.getProperties();
  164.         Enumeration propertiesNames = systemProperties.propertyNames();
  165.         while (propertiesNames.hasMoreElements()) {
  166.             String propertyName = (String) propertiesNames.nextElement();
  167.             String propertyValue = (String) systemProperties.getProperty(propertyName);
  168.             System.out.println(propertyName + " = " + propertyValue);
  169.         }
  170.     }
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177. //------------------------------------RhymingWords---------------------------------------
  178.  
  179.  
  180.  
  181.  
  182. package pl.polsl.lab.streams;
  183.  
  184. import java.io.*;
  185. import java.util.*;
  186.  
  187. public class RhymingWords {
  188.  
  189.     public static void main(String[] args) {
  190.         RhymingWords rhymingWords = new RhymingWords();
  191.         rhymingWords.operate();
  192.     }
  193.  
  194.     public void operate() {
  195.         List<String> listOfWords = new ArrayList<>();
  196.         try (BufferedReader in = new BufferedReader(
  197.                 new InputStreamReader(
  198.                         new FileInputStream("words.txt")))) {
  199.             String tmpWord;
  200.             while ((tmpWord = in.readLine()) != null) {
  201.                 listOfWords.add(tmpWord);
  202.             }
  203.         } catch (IOException e) {
  204.             System.err.println(e.getMessage());
  205.         }
  206.  
  207.         reverseWords(listOfWords);
  208.         Collections.sort(listOfWords);
  209.         reverseWords(listOfWords);
  210.  
  211.         for (String word : listOfWords) {
  212.             System.out.println(word);
  213.         }
  214.     }
  215.  
  216.     private void reverseWords(List<String> list) {
  217.         StringBuffer stringBuffer;
  218.         for (int i = 0; i < list.size(); i++) {
  219.             String word = list.get(i);
  220.             stringBuffer = new StringBuffer(word);
  221.             list.set(i, stringBuffer.reverse().toString());
  222.         }
  223.  
  224.     }
  225. }
  226.  
  227.  
  228.  
  229.  
  230.  
  231. //------------------------------------TCP---------------------------------------
  232.  
  233.  
  234.  
  235.  
  236.  
  237. import java.net.*;
  238. import java.io.*;
  239. import java.util.logging.Level;
  240. import java.util.logging.Logger;
  241.  
  242. /**
  243.  * The main class of the server
  244.  *
  245.  * @author Gall Anonim
  246.  * @version 1.2
  247.  */
  248. public class TCPServer implements Closeable {
  249.  
  250.     /**
  251.      * port number
  252.      */
  253.     final private int PORT = 8888;
  254.  
  255.     /**
  256.      * field represents the socket waiting for client connections
  257.      */
  258.     private ServerSocket serverSocket;
  259.  
  260.     /**
  261.      * Creates the server socket
  262.      *
  263.      * @throws IOException when prot is already bind
  264.      */
  265.     TCPServer() throws IOException {
  266.         serverSocket = new ServerSocket(PORT);
  267.     }
  268.  
  269.     /**
  270.      * The main application method
  271.      *
  272.      * @params args all parametres are ignored
  273.      */
  274.     public static void main(String args[]) {
  275.  
  276.         try (TCPServer tcpServer = new TCPServer()) {
  277.             System.out.println("Server started");
  278.             while (true) {
  279.                 Socket socket = tcpServer.serverSocket.accept();
  280.                 try (SingleService singleService = new SingleService(socket)) {
  281.                     singleService.realize();
  282.                 } catch (IOException e) {
  283.                     System.err.println(e.getMessage());
  284.                 }
  285.             }
  286.         } catch (IOException e) {
  287.             System.err.println(e.getMessage());
  288.         }
  289.     }
  290.  
  291.     @Override
  292.     public void close() throws IOException {
  293.         if (serverSocket != null) {
  294.             serverSocket.close();
  295.         }
  296.     }
  297. }
  298.  
  299. /**
  300.  * The server class servicing a single connection
  301.  */
  302. class SingleService implements Closeable {
  303.  
  304.     /**
  305.      * socket representing connection to the client
  306.      */
  307.     private Socket socket;
  308.     /**
  309.      * buffered input character stream
  310.      */
  311.     private BufferedReader input;
  312.     /**
  313.      * Formatted output character stream
  314.      */
  315.     private PrintWriter output;
  316.  
  317.     /**
  318.      * The constructor of instance of the SingleService class. Use the socket as
  319.      * a parameter.
  320.      *
  321.      * @param socket socket representing connection to the client
  322.      */
  323.     public SingleService(Socket socket) throws IOException {
  324.         this.socket = socket;
  325.         output = new PrintWriter(
  326.                 new BufferedWriter(
  327.                         new OutputStreamWriter(
  328.                                 socket.getOutputStream())), true);
  329.         input = new BufferedReader(
  330.                 new InputStreamReader(
  331.                         socket.getInputStream()));
  332.     }
  333.  
  334.     /**
  335.      * Realizes the service
  336.      */
  337.     public void realize() {
  338.         try {
  339.             output.println("Welcome to Java Sever");
  340.  
  341.             while (true) {
  342.                 String str = input.readLine();
  343.                 output.println("Server answers: " + str);
  344.                 if (str.toUpperCase().equals("QUIT")) {
  345.                     break;
  346.                 }
  347.                 System.out.println("Client sent: " + str);
  348.             }
  349.             System.out.println("closing...");
  350.         } catch (IOException e) {
  351.             System.err.println(e.getMessage());
  352.         } finally {
  353.             try {
  354.                 socket.close();
  355.             } catch (IOException e) {
  356.                 System.err.println(e.getMessage());
  357.             }
  358.         }
  359.     }
  360.  
  361.     @Override
  362.     public void close() throws IOException {
  363.         if (socket != null) {
  364.             socket.close();
  365.         }
  366.     }
  367. }
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375. //------------------------------------UDP---------------------------------------
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382. import java.net.*;
  383. import java.io.*;
  384.  
  385. /**
  386.  * The main class of the UDP server
  387.  *
  388.  * @author Gall Anonim
  389.  * @version 1.2
  390.  */
  391. public class ChatterServer {
  392.  
  393.     /** accessing port */
  394.     static final int PORT = 1711;
  395.     /** buffer for input data */
  396.     private byte[] buf = new byte[1024];
  397.     /** data frame */
  398.     private DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
  399.     /** communication socket */
  400.     private DatagramSocket socket;
  401.  
  402.     /**
  403.      * The UDP server constructor
  404.      */
  405.     public ChatterServer() {
  406.         try {
  407.             socket = new DatagramSocket(PORT);
  408.             System.out.println("Server started");
  409.             while (true) {
  410.                 socket.receive(datagramPacket);
  411.                 String rcvd = new String(datagramPacket.getData(), 0, datagramPacket.getLength())
  412.                         + ", from address: " + datagramPacket.getAddress()
  413.                         + ", port: " + datagramPacket.getPort();
  414.                 System.out.println(rcvd);
  415.  
  416.                 String echoString = "Echoed: " + rcvd;
  417.  
  418.                 buf = echoString.getBytes();
  419.                 DatagramPacket echo = new DatagramPacket(buf, buf.length,
  420.                         datagramPacket.getAddress(), datagramPacket.getPort());
  421.                 socket.send(echo);
  422.             }
  423.         } catch (SocketException e) {
  424.             System.err.println("Connection is not available!");
  425.             System.exit(1);
  426.         } catch (IOException e) {
  427.             System.err.println("Error during connection!");
  428.             e.printStackTrace();
  429.         }
  430.     }
  431.  
  432.     /**
  433.      * The main application method
  434.      */
  435.     public static void main(String[] args) {
  436.         new ChatterServer();
  437.     }
  438. }
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450. import java.net.*;
  451. import java.io.*;
  452.  
  453. /**
  454.  * The main UDP client class
  455.  *
  456.  * @author Gall Anonim
  457.  * @version 1.2
  458.  */
  459. public class ChatterClient extends Thread {
  460.  
  461.     /** communication socket */
  462.     private DatagramSocket datagramSocket;
  463.     /** server port number  */
  464.     static final int PORT = 1711;
  465.     /** server address */
  466.     private InetAddress hostAddress;
  467.     /** buffer for input data */
  468.     private byte[] buf = new byte[1024];
  469.     /** data frame */
  470.     private DatagramPacket dp = new DatagramPacket(buf, buf.length);
  471.     /** a client id */
  472.     private int id;
  473.  
  474.     /**
  475.      * The constructor of the UDP client object
  476.      * @param id client identifier
  477.      */
  478.     public ChatterClient(int id) {
  479.         this.id = id;
  480.         try {
  481.             datagramSocket = new DatagramSocket();
  482.             hostAddress = InetAddress.getByName("localhost");
  483.         } catch (UnknownHostException e) {
  484.             System.err.println("Unknown server!");
  485.         } catch (SocketException e) {
  486.             System.err.println("Connection is not available!");
  487.         }
  488.         System.out.println("Client " + id + " started");
  489.     }
  490.  
  491.     /**
  492.      *
  493.      */
  494.     @Override
  495.     public void run() {
  496.         try {
  497.             // 25 data frames are sent to the UDP server
  498.             for (int i = 0; i < 25; i++) {
  499.                 String outMessage = "Client #" + id + ", message #" + i;
  500.                 buf = outMessage.getBytes();
  501.                 datagramSocket.send(new DatagramPacket(buf, buf.length, hostAddress, PORT));
  502.                 datagramSocket.receive(dp);
  503.  
  504.                 String rcvd = "Client #" + id + ", rcvd from "
  505.                         + dp.getAddress() + ", "
  506.                         + dp.getPort() + ": "
  507.                         + new String(dp.getData(), 0, dp.getLength());
  508.  
  509.                 System.out.println(rcvd);
  510.             }
  511.         } catch (IOException e) {
  512.             System.err.println("Error during communication!");
  513.         }
  514.     }
  515.  
  516.     /**
  517.      * The main application method
  518.      */
  519.     public static void main(String[] args) {
  520.         // creates 5 clients for a simulation
  521.         for (int i = 0; i < 5; i++) {
  522.             new ChatterClient(i).start();
  523.         }
  524.     }
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement