Question about file

Creates a piped writer connected to the specified piped reader. Data characters written to this stream will then be available as input from snk.

http://download.oracle.com/javase/1.5.0/docs/api/java/io/PipedWriter.html
What is snk? When piped writer is used?

A channel for reading, writing, mapping, and manipulating a file.

What is mapping a file?May you explian about channel please?

- What is snk?
snk is short for sink. A sink is any consumer, eg: a PipedReader ‘consumes’ the character data placed on the pipe by the ‘source’: a PipedWriter.

- When is a piped writer used?
Pipes in Java IO provides the ability for two threads running in the same JVM to communicate.

- What is mapping a file? May you explain about channel please?
Read this

The pipe is used to transport data between two threads that are running.May someone give me a simple example code about that?

I want to practice pipe myself. I want to write a simple program that has two threads, These thread read from two files, One thread transfers data to another thread by pipe.

import java.io.*;
class SimpleThread extends Thread {
    public SimpleThread(String str) {
	super(str);
    }
    public void run() {
        
           FileInputStream in = null;
        InputStreamReader inchar=null;

        try {
             in = new FileInputStream("myfile1.txt");
             inchar = new InputStreamReader(in);
             System.out.println("Text from file ");

             int c;
             while ((c = inchar.read()) != -1) {
                System.out.print((char)c);
             }
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );



        } finally {
            try{
                inchar.close();
            }
            catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
         }
        }

}
}


class SimpleThread1 extends Thread {
    public SimpleThread1(String str) {
	super(str);
    }
    public void run() {

           FileInputStream in = null;
        InputStreamReader inchar=null;

        try {
             in = new FileInputStream("myfile2.txt");
             inchar = new InputStreamReader(in);
             System.out.println("Text from file ");

             int c;
             while ((c = inchar.read()) != -1) {
                System.out.print((char)c);
             }
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );



        } finally {
            try{
                inchar.close();
            }
            catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
         }
        }

}
}


public class Main {

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

        new SimpleThread("Jamaica").start();
         new SimpleThread1("Jamaica").start();



 

    }
}

I don’t know how to use pipe in this code, Please guide me

still waitng!

start with creating your piped writer and reader.
You could do this in your main method.
PipedReader snk = new PipedReader();
PipedWriter writer = new PipedWriter(snk);

then construct SimpleThread with the writer.
and then contruct SimpleThread1 with the reader.

good luck…

Jurn

Really? I’m still waiting for your check to arrive.

In the meantime, try typing “java pipe tutorial” into google and see what happens.

I check all the forms of constructors for thread class.I alao find this code:

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Supplements/Chapter09/pipeStreams.html

// Start the thread for the second level analysis. It
    // will take data from buf_in and write it into pipe_out.
    new Analyze1Thread  (pipe_out, buf_in).start ();

But the pipeout is not a string nor doesn’t relate to runnable interface.

import java.io.*;
import java.util.*;
class SimpleThread extends Thread {
    private Random rand=new Random();

    private PipedWriter out=new PipedWriter();
    public PipedWriter getPipedWriter(){ return out;}


    public SimpleThread(String str) {
	super(str);
    }
    public void run() {
        
           FileInputStream in = null;
        InputStreamReader inchar=null;

        try {
             in = new FileInputStream("myfile1.txt");
             inchar = new InputStreamReader(in);
             System.out.println("Text from file ");
             int c;
             while ((c = inchar.read()) != -1) {
                out.write(c);
                sleep(rand.nextInt(500));
             }
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
        } finally {
            try{
                inchar.close();
            }
            catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
         }
        }

}
}


class SimpleThread1 extends Thread {
    private PipedReader in;
    int i;

    public void Receiver(SimpleThread a) throws IOException{
        in=new PipedReader(a.getPipedWriter());
    }
    public SimpleThread1(String str) {
	super(str);
    }
    public void run() {
//for(int i=0;i<2000;i++);
          
        try {
             OutputStreamWriter out=null;
           out=new OutputStreamWriter(new FileOutputStream("myfile2.txt"),"UTF8");
           out.write((char)in.read());
           out.close();
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );

        } 

}
}


public class Main {

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

        new SimpleThread("Jamaica").start();
         new SimpleThread1("Jamaica").start();

    }
}

When I want to run it, These errors:

Runnable terminating with exceptionjava.io.IOException: Pipe not connected
Runnable terminating with exceptionjava.lang.NullPointerException

did you write this code yourself?
you need to call SimpleThread1#Receiver

import java.io.*;
import java.util.*;
class SimpleThread extends Thread {
    private Random rand=new Random();

    private PipedWriter out=new PipedWriter();
    public PipedWriter getPipedWriter(){ return out;}
  
    public void run() {
        
           FileInputStream in = null;
        InputStreamReader inchar=null;

        try {
             in = new FileInputStream("myfile1.txt");
             inchar = new InputStreamReader(in);
             System.out.println("Text from file ");
             int c;
             while ((c = inchar.read()) != -1) {
                out.write(c);
                sleep(rand.nextInt(500));
             }
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
        } finally {
            try{
                inchar.close();
            }
            catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
         }
        }

}
}



class SimpleThread1 extends Thread {
    private PipedReader in;
    int i;

    public SimpleThread1(SimpleThread a) throws IOException{
        in=new PipedReader(a.getPipedWriter());
    }
    
    public void run() {
//for(int i=0;i<2000;i++);
          
        try {
             OutputStreamWriter out=null;
           out=new OutputStreamWriter(new FileOutputStream("myfile2.txt"),"UTF8");
           out.write((char)in.read());
           out.flush();
           out.close();
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );

        } 

}
}



public class Main {

    public static void main(String[] args) throws IOException{
SimpleThread a=new SimpleThread();
        a.start();
         new SimpleThread1(a).start();


    }
}

But When I open myfile2.txt, I see nothing!

and what did you program say?
you should probably loop when reading from the pipe, as you did when reading from the file.

import java.io.*;
class SimpleThread1 extends Thread {
    private PipedReader in;
    int i;

    public SimpleThread1(SimpleThread a) throws IOException{
        in=new PipedReader(a.getPipedWriter());
    }
    
    public void run() {
//for(int i=0;i<2000;i++);
          
        try {
             OutputStreamWriter out=null;
           out=new OutputStreamWriter(new FileOutputStream("myfile2.txt"),"UTF8");
             
           
           while ((c = in.read()) != -1) {
           out.write((char)c);
           out.flush();
           out.close();
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );

        } 

}
}

I change the code.

Now the program does not stop

The following works as is, I’ll leave adding the file reading/writing as an exercise for you.


import java.io.*;

public class PipedExample {
  public static void main( String[] args ) {
    PipedWriterThread writer = new PipedWriterThread();
    BufferedPipeReaderThread reader = new BufferedPipeReaderThread();

    try {
      writer.connect( (PipedReader)reader );
    } catch ( IOException ioe ) {
      System.out.println( ioe );
    }
    new Thread( writer ).start();
    new Thread( reader ).start();
  }
}

class PipedWriterThread extends PipedWriter implements Runnable {
  String[] strings = { "testing - 1",
                       "testing - 2",
                       "testing - 3" };
  
  public void run(){
    try {
      for( int i = 0; i < strings.length; ++i ) {
        write( strings[i] + "\
" );
      }
      close();
    } catch ( IOException ioe ) {
      System.out.println( ioe );
    }
  }
}

class BufferedPipeReaderThread extends PipedReader implements Runnable {
  
  BufferedReader in;
  
  public BufferedPipeReaderThread() {
    in = new BufferedReader( this );
  }
  
  public void run() {
    String line = "";
    try {
      while( ( line = in.readLine() ) != null ) {
        System.out.println( line );
      }
    } catch ( IOException ioe ) {
      System.out.println( ioe );
    }
  }
}

public class SimpleThread extends PipedReader implements Runnable  {
    private Random rand=new Random();

    private PipedWriter out=new PipedWriter();
    public PipedWriter getPipedWriter(){ return out;}
  
    public void run() {
        
           FileInputStream in = null;
        InputStreamReader inchar=null;
        BufferedReader a;

        try {
             in = new FileInputStream("myfile1.txt");
             inchar = new InputStreamReader(in);
             a=new BufferedReader(inchar);
        
             int c;
             while ((c = a.read()) != -1) {
                out.write(c);
                out.flush();
                
             }
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
        } finally {
            try{
                inchar.close();
            }
            catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
         }
        }

}
}
class SimpleThread1 extends PipedWriter implements Runnable  {
    private PipedReader in;
    int i;

    public SimpleThread1(SimpleThread a) throws IOException{
        in=new PipedReader(a.getPipedWriter());
        this.connect( (PipedReader)a );
    }
    
    public void run() {
       
        try {
          


            BufferedWriter outc;
            OutputStreamWriter out=null;
           outc=new BufferedWriter
                   (new OutputStreamWriter(new FileOutputStream("myfile2.txt"),"UTF8"));
             
           int c;
           while ((c = in.read()) != -1) {
           out.write((char)c);
           out.flush();
           out.close();
            }
        }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );

        } 

}
}
public class Main {
    public static void main(String[] args) throws IOException{
        SimpleThread a=new SimpleThread();
        new Thread(a).start();
        
        SimpleThread1 b=new SimpleThread1(a);
        new Thread(b).start();

    }
}

Error:
Runnable terminating with exceptionjava.io.IOException: Pipe not connected

Well, that certainly makes my time spent writing an example for you worth it.

You seem to be unclear on the concept of extending a class.


class SimpleThread1 extends [B]PipedWriter[/B] implements Runnable  {
    private PipedReader in;
  ... code continues, as a Reader? You liar! You [i]just[/i] said it would be a writer!

A - Here’s a perfect example of why meaningful class, method and variable names are a really good idea. SimpleThread? Really? Just name it Fred next time and save us the heartache.

B - When we say that we’re extending a class, by using extends, our class now is the class we’re extending (note: constructors do not come across unless asked for explicitly with super())

C - Later, in your main, when you create your two threads, you connect the writer to the reader. Of course, you threw that away by enveloping an additional reader inside your writer and a writer in your reader. So the actual reader/writer pair is connected, but you don’t use them. Rather, you create another reader and another writer that know absolutely nothing about each other.

It’s a rather simple concept: I am the writer, you are the reader, and the internet is the pipe.

Read my code, copy my code, play with my code, see if you can add file reading and writing to my code, and for goodness sake - abandon your hopeless mess, it’s just not worth untangling it.

public class SimpleThread  implements Runnable  {
    private PipedWriter out=new PipedWriter();
    public PipedWriter getPipedWriter(){ return out;}

    public void run() {
        
        FileInputStream in = null;
        InputStreamReader inchar=null;
        BufferedReader a;

        try {
             in = new FileInputStream("myfile1.txt");
             inchar = new InputStreamReader(in);
             a=new BufferedReader(inchar);
        
             int c;
             while ((c = a.read()) != -1) {
                out.write(c);
                            
             }
             out.flush();
            }
             catch(Exception e) {
      System.out.println("Runnable terminating with exception" + e );
        }
              
           }
}


class SimpleThread1  implements Runnable  {
    private PipedReader in;
    int i;

    public SimpleThread1(SimpleThread a) throws IOException{
        in=new PipedReader(a.getPipedWriter());
        }
    public void run() {
       
        try {
            BufferedWriter outc=null;
            OutputStreamWriter out=null;
            outc=new BufferedWriter
                   (new OutputStreamWriter(new FileOutputStream("myfile2.txt"),"UTF8"));
             
           int c;
           while ((c = in.read()) != -1) {
               outc.write((char)c);
            }
           out.flush();
           }
        catch(Exception e) {
                 System.out.println("Runnable terminating with exception  " + e );
                 e.printStackTrace();

                 }

}
}


public class Main {
    public static void main(String[] args) throws IOException{
        SimpleThread a=new SimpleThread();
        SimpleThread1 b=new SimpleThread1(a);
        new Thread(a).start();
        new Thread(b).start();

    }
}

Error

Runnable terminating with exception java.io.IOException: Write end dead
java.io.IOException: Write end dead
at java.io.PipedReader.read(PipedReader.java:224)
at pipecheck.SimpleThread1.run(SimpleThread1.java:29)
at java.lang.Thread.run(Thread.java:619)

You should clean up and call close() on the piped writer before the writing thread dies.

I read that pipe can be used in order to transfer data between JAVA and C or JAVA and perl. Please explain about mechanism. How java can get data from another process by pipe?