Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------Files---------------------------------------
- import java.io.*;
- /**
- * The example shows the usage of the File class. The one parameter (file name
- * or directory name) is needed
- *
- * @author Gall Anonim
- * @version 1.2
- */
- public class FileDemo {
- /**
- * The main method of the application
- *
- * @param args the name of the file system element (file or directory)
- */
- public static void main(String args[]) {
- if (args.length != 1) {
- System.err.println("Give a file name or directory name!");
- return;
- }
- String name = args[0];
- File file = new File(name);
- if (!file.exists()) {
- System.err.println(name + " does't exist!");
- return;
- }
- if (file.isFile()) {
- if (!file.canRead()) {
- System.err.println(name + " can't be read!");
- return;
- } else {
- try (FileInputStream in = new FileInputStream(name)) {
- byte buffer[] = new byte[1024];
- while (true) {
- int byteCount = in.read(buffer);
- if (byteCount == -1)
- break;
- System.out.write(buffer, 0, byteCount);
- }
- } catch (IOException e) {
- System.err.println(e.getMessage());
- return;
- }
- }
- } else // directory
- {
- System.out.println(name + " includes: ");
- // List of directory elements
- for (String element: file.list()) {
- System.out.print(element);
- File dir = new File(name + "\\" + element);
- if (dir.isDirectory())
- System.out.print("\t is a direcory");
- System.out.println();
- }
- }
- }
- }
- //------------------------------------Properties---------------------------------------
- import java.io.*;
- import java.util.*;
- /**
- * Application presents how to use application properties.
- *
- * @author Gall Anonim
- * @version 1.0
- */
- public class ApplicationProperties {
- /**
- * Main method of the application
- *
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- /* initialization */
- Properties properties = new Properties();
- properties.setProperty("login", "duke");
- properties.setProperty("pass", "java");
- /* writing properties */
- try (FileOutputStream out = new FileOutputStream(".properties")) {
- properties.store(out, "--Konfiguracja--");
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- /* writing properties into xml file*/
- try (FileOutputStream out = new FileOutputStream("app.xml")) {
- properties.storeToXML(out, "--Konfiguracja--");
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- /* reading properties */
- try (FileInputStream in = new FileInputStream(".properties")) {
- properties.load(in);
- System.out.println("\n.property");
- System.out.println("login=" + properties.getProperty("login"));
- System.out.println("pass=" + properties.getProperty("pass"));
- System.out.println("no property=" + properties.getProperty("no property"));
- System.out.println("no property=" + properties.getProperty("no property","no value"));
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- /* reading properties from xml file*/
- try (FileInputStream in = new FileInputStream("app.xml")) {
- properties.loadFromXML(in);
- System.out.println("\n.app.xml");
- System.out.println("login=" + properties.getProperty("login"));
- System.out.println("pass=" + properties.getProperty("pass"));
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- }
- }
- import java.util.Enumeration;
- /**
- * Application presents values of the system properties.
- *
- * @author Gall Anonim
- * @version 1.0
- */
- public class SystemProperties {
- /**
- * Main method of the application
- *
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- java.util.Properties systemProperties = System.getProperties();
- Enumeration propertiesNames = systemProperties.propertyNames();
- while (propertiesNames.hasMoreElements()) {
- String propertyName = (String) propertiesNames.nextElement();
- String propertyValue = (String) systemProperties.getProperty(propertyName);
- System.out.println(propertyName + " = " + propertyValue);
- }
- }
- }
- //------------------------------------RhymingWords---------------------------------------
- package pl.polsl.lab.streams;
- import java.io.*;
- import java.util.*;
- public class RhymingWords {
- public static void main(String[] args) {
- RhymingWords rhymingWords = new RhymingWords();
- rhymingWords.operate();
- }
- public void operate() {
- List<String> listOfWords = new ArrayList<>();
- try (BufferedReader in = new BufferedReader(
- new InputStreamReader(
- new FileInputStream("words.txt")))) {
- String tmpWord;
- while ((tmpWord = in.readLine()) != null) {
- listOfWords.add(tmpWord);
- }
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- reverseWords(listOfWords);
- Collections.sort(listOfWords);
- reverseWords(listOfWords);
- for (String word : listOfWords) {
- System.out.println(word);
- }
- }
- private void reverseWords(List<String> list) {
- StringBuffer stringBuffer;
- for (int i = 0; i < list.size(); i++) {
- String word = list.get(i);
- stringBuffer = new StringBuffer(word);
- list.set(i, stringBuffer.reverse().toString());
- }
- }
- }
- //------------------------------------TCP---------------------------------------
- import java.net.*;
- import java.io.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- * The main class of the server
- *
- * @author Gall Anonim
- * @version 1.2
- */
- public class TCPServer implements Closeable {
- /**
- * port number
- */
- final private int PORT = 8888;
- /**
- * field represents the socket waiting for client connections
- */
- private ServerSocket serverSocket;
- /**
- * Creates the server socket
- *
- * @throws IOException when prot is already bind
- */
- TCPServer() throws IOException {
- serverSocket = new ServerSocket(PORT);
- }
- /**
- * The main application method
- *
- * @params args all parametres are ignored
- */
- public static void main(String args[]) {
- try (TCPServer tcpServer = new TCPServer()) {
- System.out.println("Server started");
- while (true) {
- Socket socket = tcpServer.serverSocket.accept();
- try (SingleService singleService = new SingleService(socket)) {
- singleService.realize();
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- }
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- }
- @Override
- public void close() throws IOException {
- if (serverSocket != null) {
- serverSocket.close();
- }
- }
- }
- /**
- * The server class servicing a single connection
- */
- class SingleService implements Closeable {
- /**
- * socket representing connection to the client
- */
- private Socket socket;
- /**
- * buffered input character stream
- */
- private BufferedReader input;
- /**
- * Formatted output character stream
- */
- private PrintWriter output;
- /**
- * The constructor of instance of the SingleService class. Use the socket as
- * a parameter.
- *
- * @param socket socket representing connection to the client
- */
- public SingleService(Socket socket) throws IOException {
- this.socket = socket;
- output = new PrintWriter(
- new BufferedWriter(
- new OutputStreamWriter(
- socket.getOutputStream())), true);
- input = new BufferedReader(
- new InputStreamReader(
- socket.getInputStream()));
- }
- /**
- * Realizes the service
- */
- public void realize() {
- try {
- output.println("Welcome to Java Sever");
- while (true) {
- String str = input.readLine();
- output.println("Server answers: " + str);
- if (str.toUpperCase().equals("QUIT")) {
- break;
- }
- System.out.println("Client sent: " + str);
- }
- System.out.println("closing...");
- } catch (IOException e) {
- System.err.println(e.getMessage());
- } finally {
- try {
- socket.close();
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- }
- }
- @Override
- public void close() throws IOException {
- if (socket != null) {
- socket.close();
- }
- }
- }
- //------------------------------------UDP---------------------------------------
- import java.net.*;
- import java.io.*;
- /**
- * The main class of the UDP server
- *
- * @author Gall Anonim
- * @version 1.2
- */
- public class ChatterServer {
- /** accessing port */
- static final int PORT = 1711;
- /** buffer for input data */
- private byte[] buf = new byte[1024];
- /** data frame */
- private DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
- /** communication socket */
- private DatagramSocket socket;
- /**
- * The UDP server constructor
- */
- public ChatterServer() {
- try {
- socket = new DatagramSocket(PORT);
- System.out.println("Server started");
- while (true) {
- socket.receive(datagramPacket);
- String rcvd = new String(datagramPacket.getData(), 0, datagramPacket.getLength())
- + ", from address: " + datagramPacket.getAddress()
- + ", port: " + datagramPacket.getPort();
- System.out.println(rcvd);
- String echoString = "Echoed: " + rcvd;
- buf = echoString.getBytes();
- DatagramPacket echo = new DatagramPacket(buf, buf.length,
- datagramPacket.getAddress(), datagramPacket.getPort());
- socket.send(echo);
- }
- } catch (SocketException e) {
- System.err.println("Connection is not available!");
- System.exit(1);
- } catch (IOException e) {
- System.err.println("Error during connection!");
- e.printStackTrace();
- }
- }
- /**
- * The main application method
- */
- public static void main(String[] args) {
- new ChatterServer();
- }
- }
- import java.net.*;
- import java.io.*;
- /**
- * The main UDP client class
- *
- * @author Gall Anonim
- * @version 1.2
- */
- public class ChatterClient extends Thread {
- /** communication socket */
- private DatagramSocket datagramSocket;
- /** server port number */
- static final int PORT = 1711;
- /** server address */
- private InetAddress hostAddress;
- /** buffer for input data */
- private byte[] buf = new byte[1024];
- /** data frame */
- private DatagramPacket dp = new DatagramPacket(buf, buf.length);
- /** a client id */
- private int id;
- /**
- * The constructor of the UDP client object
- * @param id client identifier
- */
- public ChatterClient(int id) {
- this.id = id;
- try {
- datagramSocket = new DatagramSocket();
- hostAddress = InetAddress.getByName("localhost");
- } catch (UnknownHostException e) {
- System.err.println("Unknown server!");
- } catch (SocketException e) {
- System.err.println("Connection is not available!");
- }
- System.out.println("Client " + id + " started");
- }
- /**
- *
- */
- @Override
- public void run() {
- try {
- // 25 data frames are sent to the UDP server
- for (int i = 0; i < 25; i++) {
- String outMessage = "Client #" + id + ", message #" + i;
- buf = outMessage.getBytes();
- datagramSocket.send(new DatagramPacket(buf, buf.length, hostAddress, PORT));
- datagramSocket.receive(dp);
- String rcvd = "Client #" + id + ", rcvd from "
- + dp.getAddress() + ", "
- + dp.getPort() + ": "
- + new String(dp.getData(), 0, dp.getLength());
- System.out.println(rcvd);
- }
- } catch (IOException e) {
- System.err.println("Error during communication!");
- }
- }
- /**
- * The main application method
- */
- public static void main(String[] args) {
- // creates 5 clients for a simulation
- for (int i = 0; i < 5; i++) {
- new ChatterClient(i).start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement