Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Test {
- public class WritingClass {
- public DataOutputStream dos;
- public DataInputStream dis;
- public WritingClass(File output, File input) throws FileNotFoundException {
- dos = new DataOutputStream(new FileOutputStream(output));
- dis = new DataInputStream(new FileInputStream(output));
- }
- public String writeThis(String str) throws IOException {
- dos.writeUTF(str);
- String response = dis.readUTF();
- return response;
- }
- }
- public class ReadingClass {
- public DataOutputStream dos;
- public DataInputStream dis;
- public ReadingClass(File output, File input) throws FileNotFoundException {
- dos = new DataOutputStream(new FileOutputStream(output));
- dis = new DataInputStream(new FileInputStream(output));
- }
- public String readThat() throws IOException {
- String read = dis.readUTF();
- dos.writeUTF("nice string");
- return read;
- }
- }
- public Test() throws IOException {
- final File output = new File("out.txt");
- if(!output.exists())
- output.createNewFile();
- final File input = new File("in.txt");
- if(!input.exists())
- input.createNewFile();
- final WritingClass wc = new WritingClass(output, input);
- final ReadingClass rc = new ReadingClass(output, input);
- Thread writeThread = new Thread() {
- @Override
- public void run() {
- System.out.println("writeThread > Starting..");
- try {
- String testStr = "test";
- String reponse = wc.writeThis(testStr);
- System.out.printf("writeThread > wrote: %s, response%n", testStr, reponse);
- } catch(Exception ex) {
- System.out.println("writeThread > Exception");
- ex.printStackTrace();
- }
- System.out.println("writeThread > Stopping..");
- }
- };
- Thread readThread = new Thread() {
- @Override
- public void run() {
- System.out.println(" readThread > Starting writeThread..");
- try {
- System.out.printf(" readThread > read: %s%n", rc.readThat());
- } catch(Exception ex) {
- System.out.println(" readThread > Exception");
- ex.printStackTrace();
- }
- System.out.println(" readThread > Stopping..");
- }
- };
- writeThread.start();
- readThread.start();
- }
- public static void main(String[] args) throws Exception {
- Test t = new Test();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement