Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Adacornelius
Java Program
1 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


How to Write a Java program which read in a number of text files. It will use a thread to one file. Each line in the file will be put into a fixed size buffers of strings. The buffer can store at most 10 strings. The buffer is operated as a queue in the first-in-first-out order. There will be the same number of threads as the number of the files to get strings from the buffer. After getting the strings from the buffer, the threads will print them out in the standard output. The program should be started like this:

java readFiles file1 file2 file3

where file1, file2, etc are name of the files to be read. In this example, the program should create three threads for reading the file and three threads for getting the strings out of the queue and printing them


Advertisement
View Answers

December 21, 2010 at 11:53 AM


Hi Friend,

Try the following code:

import java.io.*;
class ReadFileUsingThread
{
    public void readFromFile(final String f1,final String f2,final String f3) {
    Runnable readRun1 = new Runnable() {
      public void run() {
          StringBuffer buffer1=new StringBuffer(10);
          try{
          Thread.sleep(5000);
          File inputFile = new File(f1);
          FileInputStream in = new FileInputStream(inputFile);
          byte bt[] =  new byte[(int)inputFile.length()];
          in.read(bt);
          buffer1.append(new String(bt));
          System.out.println(buffer1.toString());
       } catch(Exception ex) {
       }  
      }
    };
    Thread thread1 = new Thread(readRun1);
    thread1.start();

Runnable readRun2 = new Runnable() {
      public void run() {
          StringBuffer buffer2=new StringBuffer(10);
          try{
          Thread.sleep(5000);
          File inputFile = new File(f2);
          FileInputStream in = new FileInputStream(inputFile);
          byte bt[] =  new byte[(int)inputFile.length()];
          in.read(bt);
          buffer2.append(new String(bt));
          System.out.println(buffer2.toString());
       } catch(Exception ex) {
       }  
      }
    };
    Thread thread2 = new Thread(readRun2);
    thread2.start();

Runnable readRun3 = new Runnable() {
      public void run() {
          try{
          StringBuffer buffer3=new StringBuffer(10);
          Thread.sleep(5000);
          File inputFile = new File(f3);
          FileInputStream in = new FileInputStream(inputFile);
          byte bt[] =  new byte[(int)inputFile.length()];
          in.read(bt);
          buffer3.append(new String(bt));
          System.out.println(buffer3.toString());
       } catch(Exception ex) {
       }  
      }
    };
    Thread thread3 = new Thread(readRun3);
    thread3.start();


  }

    public static void main(String[] args) 
    {
        ReadFileUsingThread files=new ReadFileUsingThread();
        String f1="C:/data.txt",f2="C:/employee.txt",f3="C:/hello.txt";
        files.readFromFile(f1,f2,f3);
    }
}

Hope that it will be helpful for you.

Thanks



Related Tutorials/Questions & Answers:
java program for
java program for   java program for printing documents,images and cards
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
Advertisements
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
java program
java program  write a program to print 1234 567 89 10
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  Write a java program to find the number of Positive numbers in m* n matrix
java program
java program  Write a program to create an applet and display The message "welcome to java
java program
java program  java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers
Java Program
Java Program  java program to insert row in excel sheet after identifying an object
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write java program for constructor,overriding,overriding,exception handling
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  hi friends how to make a java program for getting non prime odd numbers in a given series
java program
java program  Write a program to demonstrate the concept of various possible exceptions arising in a Java Program and the ways to handle them.  ... in Java
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program  write a program to create text area and display the various mouse handling events
java program
java program   Write a program to find the difference between sum of the squares and the square of the sums of n numbers
program in java
program in java  write a reverse program in java using string buffer.the input and out put as follows. input- hi good mornig out put-ih doog ginrom
Java program
Java program  Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
java program
java program  . Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program  . Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
java program
java program  Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program   A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A java program to display above triangle
java program
java program  write a program to create server and client such that server receives data from client using BuuferedReader and sends reply to client using PrintStream
java program
java program  write a program to read 10 numbers from user and store it in a array. display the maximum n minimum number in the array
java program
java program  i want a applet program that accepts two input strings using tag and concatenate the strings and display it in status window. please give mi he code for this in core java
java program
java program  write a java program to create an array of size 10 by taking input from bufferreader and find out the average of array elements from that array
java program
java program  i want a applet program that accepts two input strings using tag and concatenate the strings and display it in status window. please give mi he code for this in core java
java program
java program  i want a applet program that accepts two input strings using tag and concatenate the strings and display it in status window. please give mi he code for this in core java
 

 

 

DMCA.com