Results 1 to 8 of 8
  1. #1
    mukeshpaliwal is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default problem regarding FileReader

    my program is compiled and executed but output is getting different.
    instead of reading file it is reading file infinetly.plz help me.not understanding hw to solve dis problem .
    i m submmiting my program here.






    import java.io.*;
    class filedemos
    {
    public static void main(String args[])
    {
    try
    {
    FileReader fr=new FileReader(args[0]);
    try
    {
    FileWriter fw=new FileWriter(args[1]);
    try
    {
    char b[]=new char[128];
    String line=String.valueOf(fr.read(b));

    while(!line.equals(1))
    {
    fw.write(b,0,line.length());
    line=String.valueOf(fr.read(b));
    }
    }
    finally
    {
    fw.close();
    }
    }
    finally
    {
    fr.close();
    }
    }
    catch(IOException e)
    {
    e.printStackTrace();
    }
    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Posting your code in [code] tags helps preserve formatting to make things easier to read:
    Java Code:
    import java.io.*;
    class filedemos 
    {
    	public static void main(String args[]) 
    	{
    		try
    		{
    			FileReader fr=new FileReader(args[0]);
    			try
    			{
    				FileWriter fw=new FileWriter(args[1]);
    				try
    				{
    					char b[]=new char[128];
    					String line=String.valueOf(fr.read(b));
    
    					while(!line.equals(1))
    					{
    						fw.write(b,0,line.length());
    						line=String.valueOf(fr.read(b));
    					}
    				}
    				finally
    				{
    					fw.close();
    				}
    			}
    			finally
    			{
    				fr.close();
    			}
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    As for the issue, what are the contents of the file you are reading?

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

    Default

    it is reading file infinetly
    What code do you have to stop the program from looping forever? When will the condition be false to let the code exit the loop?
    Try debugging you program by printing out the contents of line inside of the while loop.

    NOTE TO ALL: Here is an example of autoboxing that is confusing the OP and others about how the code is compiling. The int in the equals is being changed to an Integer object. Compile it in an older version of the compiler and you get:

    Value for argument 1 cannot be converted from 'int' in call to 'boolean String.equals(Object)'

  4. #4
    mukeshpaliwal is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default problem in filereader

    actually the problem is my program is reading the file and also writing it to new .txt file file but output i m getting is different
    i will give u one example:if my file named d.txt contain data like "i am very good person".now i want to write this file into another file. during runtime i have specified name of two file like:java filedemos d.txt f.txt.but what output i m getting is as follow:when i check file f.txt it contains "i m very good person" writing repeatedly means writing content infinitely in f.txt.it is not finishing its process.output is as follow:

    "i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person""i m very good person"............going infinitely not stopping...dis is the problem..

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

    Default

    Did you read my post about the loop in your program?

  6. #6
    mukeshpaliwal is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    i just debugged my program as u said but havent understud wat is happening.
    program just copied first 3 letter of file to b read.and on compiler it is giving true.
    so please make me explain in detail or sort out the problem yourself

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Aside from the fact that read returns -1 and not 1, the number inside that equals should be
    Java Code:
    "-1"
    and not just
    Java Code:
    -1
    . When it is just a plain number then it is an int and not a String. That int automatically gets autobxed into an Integer since it is being used in a place where an Object is needed. Also, since the equals method signature excepts all object types and not just Strings an Integer is valid in that position. But, since an Integer is not a String that equals method will always return false. So the expression always returns true because of the ! at the beginning.

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

    Default

    sort out the problem yourself
    Its not my problem.

    Did you read my post about the loop in your program?

Similar Threads

  1. FileReader problems
    By Maikl in forum New To Java
    Replies: 3
    Last Post: 12-14-2009, 11:51 AM
  2. FileReader help
    By emp in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 04:41 AM
  3. add FileReader to GUI
    By VinTiger in forum New To Java
    Replies: 8
    Last Post: 05-11-2009, 12:23 AM
  4. FileReader / Buffered Reader
    By sepaht in forum New To Java
    Replies: 9
    Last Post: 07-10-2008, 08:05 PM
  5. Help with filereader in java
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 09:55 AM

Posting Permissions

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