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

    Default File i/o help needed

    hey, geeks, i am new to java. i need to write this parogram.

    write a program to read id, name, address and salary of twenty employees from keyboard and write it into emp.doc. again read the records of employees and display records of those students whose salary is more than 25000.

    please help me filter out this question.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: File i/o help needed

    Have you discovered The Java™ Tutorials ? There's a section on basic I/O

    Also, this is a technical forum. Please use uppercase letters where appropriate. And what's an "emp.doc."?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sureshHARDIYA is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: File i/o help needed

    EMP.DOC is a document fie. I have tried using following codes but its showing error.

    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class TestTwo{

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


    try {


    //create a buffered reader that connects to the console, we use it so we can read lines
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    //create an print writer for writing to a file
    PrintWriter out = new PrintWriter(new FileWriter("EMP.DOC"));


    for(int i =0;i<1;i++){

    System.out.println("Enter id:");
    int id = Integer.parseInt(in.readLine());
    out.println(id);


    System.out.println("Enter name:");
    String lineFromInput = in.readLine();
    out.println(lineFromInput);


    System.out.println("Enter address:");
    String address = in.readLine();
    out.println(address);


    System.out.println("Enter salary:");
    Double salary = Double.parseDouble(in.readLine());
    out.println(salary);

    }
    //close the file (VERY IMPORTANT!)
    out.close();
    }
    catch(IOException e1) {
    System.out.println("Error during reading/writing");
    }
    try {

    FileInputStream fin = new FileInputStream("EMP.DOC");
    DataInputStream din = new DataInputStream(fin);
    FileReader fr = new FileReader("output.txt");
    BufferedReader br = new BufferedReader(fr);
    String s;
    while((s=br.readLine())!=null)
    {
    Double i = din.readDouble();
    if(i>300)
    {
    System.out.println("salary grater than 300 : " + i);
    }
    }

    } catch (FileNotFoundException ex) {
    Logger.getLogger(TestTwo.class.getName()).log(Leve l.SEVERE, null, ex);
    }

    }
    }


    Enter id:
    1
    Enter name:
    suresh
    Enter address:
    new baneshowr
    Enter salary:
    4566.0
    salary grater than 300 : 5.033349636986289E180
    Exception in thread "main" java.io.EOFException
    at java.io.DataInputStream.readFully(DataInputStream. java:180)
    at java.io.DataInputStream.readLong(DataInputStream.j ava:399)
    at java.io.DataInputStream.readDouble(DataInputStream .java:451)
    at TestTwo.main(TestTwo.java:57)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 10 seconds)



    THE DATA IS WRITTEN INTO FILE.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default Re: File i/o help needed

    Exception in thread "main" java.io.EOFException
    ...
    at java.io.DataInputStream.readDouble(DataInputStream .java:451)
    at TestTwo.main(TestTwo.java:57)
    At line 57 the code calls the readDouble() method when there is no more data available to be read so the readDouble() method threw the exception.
    The code is unusual, it reads a line into the String s which it ignores and then tries to read a double using another class object.
    Can you explain why it is doing that?

    Please edit your post and wrap the code in code tags. See following:
    http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    BLuFeNiX is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: File i/o help needed

    I'm going to try my best here...


    Java Code:
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class test {
    
    	public static void main(String args[]) throws IOException{
    
    		try {
    
    			//create a buffered reader that connects to the console, we use it so we can read lines
    			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    			//create an print writer for writing to a file
    			PrintWriter out = new PrintWriter(new FileWriter("EMP.DOC"));
    This looks reasonable, but you might want to call the file something else, since the .doc extension implies a rich-text format.

    Java Code:
    			for(int i = 0; i < 1; i++){
    
    				System.out.println("Enter id:");
    				int id = Integer.parseInt(in.readLine());
    				out.println(id); 
    
    
    				System.out.println("Enter name:");
    				String lineFromInput = in.readLine();
    				out.println(lineFromInput);
    
    
    				System.out.println("Enter address:");
    				String address = in.readLine();
    				out.println(address);
    
    
    				System.out.println("Enter salary:");
    				Double salary = Double.parseDouble(in.readLine());
    				out.println(salary);
    			}
    You do not need a for loop here, since you are only doing it once. "for(int i = 0; i < 1; i++)" essentially means "do this one time."

    Java Code:
    			//close the file (VERY IMPORTANT!)
    			out.close();
    		}
    		catch(IOException e1) {
    			System.out.println("Error during reading/writing");
    		}
    		try {
    
    			FileInputStream fin = new FileInputStream("EMP.DOC");
    			DataInputStream din = new DataInputStream(fin);
    			FileReader fr = new FileReader("output.txt");
    			BufferedReader br = new BufferedReader(fr);
    			String s;
    			while((s=br.readLine())!=null)
    			{
    				Double i = din.readDouble();
    				if(i>300)
    				{ 
    					System.out.println("salary grater than 300 : " + i);
    				}
    			}
    
    		} catch (FileNotFoundException ex) {
    			Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
    		}
    
    	}
    }
    It seems like you are attempting to read the data you just created, and decide if the salary is greater than 300. The problem is you are trying to read the wrong file (output.txt), instead of the file you created (EMP.DOC). Please tell us exactly what you are trying to do so that I may have a clear picture.

  6. #6
    sureshHARDIYA is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: File i/o help needed

    HELLO
    BLuFeNiX

    i WANT TO SOLVE FOLLOWING QUESTION.

    write a program to read id, name, address and salary of twenty employees from keyboard and write it into emp.doc. again read the records of employees and display records of those students whose salary is more than 25000.

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

    Default Re: File i/o help needed

    Quote Originally Posted by sureshHARDIYA View Post
    HELLO
    BLuFeNiX

    i WANT TO SOLVE FOLLOWING QUESTION.

    write a program to read id, name, address and salary of twenty employees from keyboard and write it into emp.doc. again read the records of employees and display records of those students whose salary is more than 25000.
    Consider answering Norm's questions.

Similar Threads

  1. Replies: 9
    Last Post: 07-30-2012, 04:45 PM
  2. Help needed with reading a text file
    By newguy11 in forum New To Java
    Replies: 4
    Last Post: 12-09-2011, 11:45 AM
  3. File Reading..Help Needed
    By spartan in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 02:06 PM
  4. Truncated class file [HELP NEEDED]
    By Tylers Pur3 in forum New To Java
    Replies: 8
    Last Post: 08-03-2010, 06:48 PM
  5. Help needed Clearing the contents of a File
    By sandeepsai17 in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 02:31 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
  •