Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

I/O

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, guys:
The following code can read a text file and copy it to another file.
//A simple program for copying files
import java.io.*;
public class Copy {
public static void main (String [] args) throws IOException {
File inputFile = new File("ToBeCopied.txt");
File outputFile = new File("Copied.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
int count = 0;
while((c=in.read())!= -1){
System.out.print(c + " ");
if (count++%10==1)
System.out.println();
out.write(c);
}
in.close();
out.close();
}
}
my question is:
int c = in.read() is an integer which can be displayed in System.out. But when you write c to file "copied.txt', it actually is what appears in file 'ToBeCopied.txt", like characters. what is the mechanism behind this read/write operation?
thankx
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read method returns an int when you read it. I don't understand a question , you want to know the mechanism how it's structured ? Try opening the source file for this and see what it says the source is located in your jdk directory there is an src jar file unzip it and you can view how they did this.
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to know why read() returns an int instead of a char, it's because read() uses a -1 to indicate end of file. However, since char is an unsigned type, it cannot contain a negative number, so an int is used instead. If you want to get the character back out of it, you can cast the int to a char.
 
Weigang Gu
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply and i am sorry for not making the question clear. What i really get confused is the process of writing. The code: out.write(c) writes "c" to file "ToBeCopied.txt". My question is why the output is not a series of int?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call out.write(c), the write(int c) method drops the 16 high-order bits of the int and converts it to a char. That's why you see chars in the file and not ints.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is FYI:
When you invoke out.write(c), the write() method cast the int back to a char type.

Notice the cbuf[0] = (char) c; line.
What is even more interesting, is this method is pretty expensive, because you're creating a new char[] of one element each time you write out one character at a time. To make your code run faster, you should use the write(char cbuf[], int off, int len) version.
-Peter
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic