Results 1 to 8 of 8
Thread: problem regarding FileReader
- 09-30-2010, 04:29 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
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();
}
}
}
- 09-30-2010, 05:51 PM #2
Posting your code in [code] tags helps preserve formatting to make things easier to read:
As for the issue, what are the contents of the file you are reading?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(); } } }
- 09-30-2010, 06:08 PM #3
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?it is reading file infinetly
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)'
- 10-01-2010, 02:31 PM #4
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
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..
- 10-01-2010, 02:47 PM #5
Did you read my post about the loop in your program?
- 10-01-2010, 03:02 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
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
- 10-01-2010, 03:06 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Aside from the fact that read returns -1 and not 1, the number inside that equals should be
and not justJava 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.Java Code:-1
- 10-01-2010, 03:15 PM #8
Similar Threads
-
FileReader problems
By Maikl in forum New To JavaReplies: 3Last Post: 12-14-2009, 11:51 AM -
FileReader help
By emp in forum New To JavaReplies: 1Last Post: 07-28-2009, 04:41 AM -
add FileReader to GUI
By VinTiger in forum New To JavaReplies: 8Last Post: 05-11-2009, 12:23 AM -
FileReader / Buffered Reader
By sepaht in forum New To JavaReplies: 9Last Post: 07-10-2008, 08:05 PM -
Help with filereader in java
By zoe in forum Advanced JavaReplies: 2Last Post: 07-26-2007, 09:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks