Home arrow static arrow Java Programming [Archive] - FileReader problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - FileReader problem
This topic has 2 replies on 1 page.

Posts:1
Registered: 7/21/04
FileReader problem  
Jul 21, 2004 11:03 PM



 
Can anyone tell me what's wrong with this line? What's wrong with fr.available?
--------------------Configuration: j2sdk1.4.1_06 <Default>--------------------
C:\j2sdk1.4.1_06\bin\CopyFileUsingCharStream.java:36: cannot resolve symbol
symbol : method available ()
location: class java.io.FileReader
fr.available() + " chars");

[code]
// CopyFileUsingCharStream.java: Copy files
import java.io.*;

public class CopyFileUsingCharStream {
/** Main method
@param args[0] for sourcefile
@param args[1] for target file
*/
public static void main(String[] args) {
// Declare input and output file streams
FileReader fr = null;
FileWriter fw = null;

// Check usage
if (args.length != 2) {
System.out.println(
"Usage: java CopyFileUsingCharStream fromfile tofile");
System.exit(0);
}

try {
// Create file input stream
fr = new FileReader(new File(args[0]));

// Create file output stream if the file does not exist
File file = new File(args[1]);
if (file.exists()) {
System.out.println("file " + args[1] + " already exists");
return;
}
else
fw = new FileWriter(args[1]);

// Display the file size
System.out.println("The file " + args[0] + " has "+
fr.available() + " chars");// this line not working

// Continuously read from fr write it to fw
int r;
while ((r = fr.read()) != -1) {
fw.write((char)r);
}
}
catch (FileNotFoundException ex) {
System.out.println("File not found: " + args[0]);
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
finally {
try {
// Close files
if (fr != null) fr.close();
if (fw != null) fw.close();
}
catch (IOException ex) {
System.out.println(ex);
}
}
}
}[\code]

 

Posts:18,384
Registered: 21.03.00
Re: FileReader problem  
Jul 21, 2004 11:19 PM (reply 1 of 2)



 
Hi,
Have you checked the documentation? There is no method name available in FileReader.

/Kaj
 

Posts:192
Registered: 30/05/01
Re: FileReader problem  
Jul 22, 2004 12:32 AM (reply 2 of 2)



 
I think you may be getting your wires crossed with the available() method in the InputStream class.

InputStream.available() tells you how many bytes are available to be read before the next read() will block. Reader.ready() is similar to the available() method, except it does not indicate how many characters are available. (quote from http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/)
 
This topic has 2 replies on 1 page.