aspose file tools*
The moose likes Beginning Java and the fly likes Reading from file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading from file" Watch "Reading from file" New topic
Author

Reading from file

Marc Kryzhan
Greenhorn

Joined: Mar 17, 2004
Posts: 14
I've successfully read from a file; proven because I wrote to a text file by copying from a file. How do I do a System.out.println of the contents of a file? Here's my code that I used to write a file and inwhich I've also attempted to output the encoded contents of my file with println. The file I read from is called farrago.txt (from sun's site). I modified it to contain two lines of text.
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
{out.write(c);
System.out.println("ToString " + out.toString());
System.out.println("Read " + in.read());
System.out.println("Encoding " + in.getEncoding());
System.out.println("Can Read? " + inputFile.canRead());
System.out.println("Path " + inputFile.getPath());
System.out.println("Length " + inputFile.length());

//System.out.println("..." + getEncoding();
}
in.close();
out.close();
}
}


Newbee to java.
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Hi Marc,
in.read() returns an int - the character code of the char you read. You need to cast this to a char when printing out.
With minimal changes to your code, this works:

However, you'll find that I/O in Java can be made much easier (or more complicated, depending on your point of view) by combining the various classes available for reading and writing.
Thus some equivalent code is:

The advantage of the code above is that you get a whole line at a time - this is often very useful. Other classes are available for reading from zip files, from sockets, from just about anything...and writing back. :-)
Cheers,
--Tim
[ March 29, 2004: Message edited by: Tim West ]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
You can read the contents of the file into a buffer of bytes (ie byte[]) and then just System.out.print(buffer), over and over until you reach the end of the file. Is that what you are thinking?


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the I/O and Streams forum...
[ March 29, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from file
 
Similar Threads
Reading from file
reding in csv file
program reagarding reading and writing to a file in java
Copy file
Image transfer through sockets