• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading from file

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
[ March 29, 2004: Message edited by: Dirk Schreckmann ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic