Results 1 to 7 of 7
  1. #1
    akronjava is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Unhappy Threads reading a file and writing

    HI, I am trying to write a java program that reads data from one file and writes it to another file. So one thread reads data from say file a.txt, then second thread writes the data read by thread1 into say file b.txt

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;

    class MyThread extends Thread{

    private int a;

    FileInputStream in = null;
    FileOutputStream out = null;
    int c;

    char p;



    public MyThread(int a){

    this.a = a;


    }

    public void run(){
    try{
    in = new FileInputStream("homo.txt");
    out = new FileOutputStream("myth.txt");

    if(this.a==5)
    {
    while((c=(in.read()))!=-1)

    {
    p = (char)c;

    System.out.println(p);

    }
    }
    if (this.a==10)
    {
    for(int i=0;i<60;i++)
    {
    out.write(p);
    System.out.println('e');

    }

    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    }

    }



    class MainMyThread{

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

    MyThread thr1, thr2;


    thr1 = new MyThread(5);

    thr2 = new MyThread(10);

    thr1.start();


    thr2.start();

    }

    }


    Thread2 doest seem to work....do i need ot use synchronization etc

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why are you trying to use threads for this?
    The thread that you create with 10 never has a character to write because it never reads any. The two threads are different objects and therefore they do not share instance variables.
    Quite frankly your code doesn't make any sense at all.
    Perhaps if you state the problem that you are trying to solve, you can get advice on how to solve it correctly.

  3. #3
    akronjava is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default Java Threads and Synchronization

    Well. ....... I am just trying to write a program involving threads where in one thread reads a file while another thread writes the information that was read by the earlier thread into a different file.....

    Is a program like this possible.....

    Thanks for the reply...I just realized that each instnance of a class has a seperate variaables... i am relatively new to java

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If you are new to Java then don't start by learning threads.
    Get Sun's Java tutorial and read it from the beginning. By the time you get to the threads section you'll be ready to learn and understand them.

  5. #5
    akronjava is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    well... i know...but i am a second year college student,..... i just shifted my major from electronics to cs....I am not that bad at java either...i mean i do understand most of the stuff like interfaces,packages, access specifiers. etc....i am kinda out of touch nw

    I need this program for my intro to OS class......My professor says implement two threads one reads a file and other writes the read info to a different file...he says not to wry abt synchronization....

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    So you have a simple producer/consumer scenario. The thread reading the file is the producer.
    Let it produce the text and add it to a List (perhaps a Queue is better). The thread doing the writing is the consumer, let it read text from that list and write it wherever it needs to.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Akronjava, welcome to the forum, but I ask you kindly not to cross-post the same question in multiple fora without notifying us that you are doing this and providing links. If not, you risk our duplicating efforts that have already been expended elsewhere, which is not quite fair of volunteers. Thanks for your cooperation.

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  2. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 10:44 AM
  3. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 02:05 PM
  4. writing/reading a Structure to/from a file
    By Farzaneh in forum New To Java
    Replies: 1
    Last Post: 08-31-2008, 02:21 PM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •