Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8.  
  9.  
  10. public class Test {
  11.   public class WritingClass {
  12.     public DataOutputStream dos;
  13.     public DataInputStream dis;
  14.    
  15.     public WritingClass(File output, File input) throws FileNotFoundException {
  16.       dos = new DataOutputStream(new FileOutputStream(output));
  17.       dis = new DataInputStream(new FileInputStream(output));
  18.     }
  19.    
  20.     public String writeThis(String str) throws IOException {
  21.       dos.writeUTF(str);
  22.       String response = dis.readUTF();
  23.       return response;
  24.     }
  25.   }
  26.  
  27.   public class ReadingClass {
  28.     public DataOutputStream dos;
  29.     public DataInputStream dis;
  30.    
  31.     public ReadingClass(File output, File input) throws FileNotFoundException {
  32.       dos = new DataOutputStream(new FileOutputStream(output));
  33.       dis = new DataInputStream(new FileInputStream(output));
  34.     }
  35.    
  36.     public String readThat() throws IOException {
  37.       String read = dis.readUTF();
  38.       dos.writeUTF("nice string");
  39.       return read;
  40.     }
  41.   }
  42.  
  43.   public Test() throws IOException {
  44.     final File output = new File("out.txt");
  45.     if(!output.exists())
  46.       output.createNewFile();
  47.     final File input = new File("in.txt");
  48.     if(!input.exists())
  49.       input.createNewFile();
  50.  
  51.     final WritingClass wc = new WritingClass(output, input);
  52.     final ReadingClass rc = new ReadingClass(output, input);
  53.    
  54.     Thread writeThread = new Thread() {
  55.       @Override
  56.       public void run() {
  57.         System.out.println("writeThread > Starting..");
  58.         try {
  59.           String testStr = "test";
  60.           String reponse = wc.writeThis(testStr);
  61.           System.out.printf("writeThread > wrote: %s, response%n", testStr, reponse);
  62.         } catch(Exception ex) {
  63.           System.out.println("writeThread > Exception");
  64.           ex.printStackTrace();
  65.         }
  66.         System.out.println("writeThread > Stopping..");
  67.       }
  68.     };
  69.     Thread readThread = new Thread() {
  70.       @Override
  71.       public void run() {
  72.         System.out.println(" readThread > Starting writeThread..");
  73.         try {
  74.           System.out.printf(" readThread > read: %s%n", rc.readThat());
  75.         } catch(Exception ex) {
  76.           System.out.println(" readThread > Exception");
  77.           ex.printStackTrace();
  78.         }
  79.         System.out.println(" readThread > Stopping..");
  80.       }
  81.     };
  82.     writeThread.start();
  83.     readThread.start();
  84.   }
  85.  
  86.  
  87.   public static void main(String[] args) throws Exception {
  88.     Test t = new Test();
  89.   }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement