• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

java.io with UTF8

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm stumpted... Which one is correct and why?
Which method implementations will write the given string to a file named "file", using UTF8 encoding?
IMPLEMENTATION A:
public void write(String msg) throws IOException {
FileWriter fw = new FileWriter(new File("file"));
fw.write(msg);
fw.close();
}
IMPLEMENTATION B:
public void write(String msg) throws IOException {
OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream("file"), "UTF8");
osw.write(msg);
osw.close();
}
IMPLEMENTATION C:
public void write(String msg) throws IOException {
FileWriter fw = new FileWriter(new File("file"));
fw.setEncoding("UTF8");
fw.write(msg);
fw.close();
}
IMPLEMENTATION D:
public void write(String msg) throws IOException {
FilterWriter fw = FilterWriter(new FileWriter("file"), "UTF8");
fw.write(msg);
fw.close();
}
IMPLEMENTATION E:
public void write(String msg) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(
new OutputStream(new File("file")), "UTF8"
}
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Monty,
I think only OutputStreamWriter and InputStreamReader have constructors that allow you to specify the encoding.Its subclasses, like FileReader and FileWriter DO NOT and when you use them, always the default encoding of your system is used.So to specify an encoding, you need to create your own reader or writer by using the OutputStreamWriter or InputStreamReader. With this in mind, then in your question we can immediately rule out A,C and D. That leaves us with B and E. But if you look closely in implementation E you see that a File onject is passed to an OutputStream constructor and apparently OutputStream does not have a constructor that takes a File object as its argument.So that one is not correct.Therefore the only correct answer is B IMNSHO.
Others may need to verify this.
Herbert
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks right, Herbert. I suppose A would work if you're on a system where the system default is already UTF8, but that's generally not going to be the case. Certainly for purposes of the exam we should assume it isn't the case.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
E is incorrect because OutputStream is an abstract class, and therefore cannot be initialized.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
monty6,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic