1 / 11

Writers

Writers. a means of writing text. What are Writers?. Outputs a string of unicode chars Outputs a String Unicode is produced from ASCII input Manage the translation from non ASCII input ASCII – American Standard Code for Information Interchange

india
Download Presentation

Writers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writers a means of writing text

  2. What are Writers? • Outputs a string of unicode chars • Outputs a String • Unicode is produced from ASCII input • Manage the translation from non ASCII input • ASCII – American Standard Code for Information Interchange • EBCDIC – Extended Binary Coded Decimal Interchange Code.

  3. Java IO Character Streams - Reader

  4. Java IO Character Streams - Writer PrintWriter

  5. I/O Superclasses • Writer and OutputStream define similar APIs but for different data types. • Writer defines these methods for writing characters and arrays of characters: • int write(int c) • int write(char cbuf[]) • int write(char cbuf[], int offset, int length) • OutputStream defines the same methods but for bytes: • int write(int c) • int write(byte cbuf[]) • int write(byte cbuf[], int offset, int length)

  6. Class Writer • All character stream writers are based on the class Writer (they extend Writer) • Some of the methods: • void write(int c) • void write(char[] cbuf) • void write(String str) • void flush() • void close()

  7. Text Files • Information can be read from and written to text files by declaring and using the correct I/O streams • The FileReader class represents an input file containing character data • The FileReader and BufferedReader classes together create a convenient text file output stream • The FileWriter class represents a text output file, but with minimal support for manipulating data • Therefore, the PrintWriter class provides print and println methods

  8. How do I build a writer? BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f))); .... bw.writeln(“this is a test”);

  9. Text File I/O Streams • Input BufferedReader in = new BufferedReader(new FileReader(“foo.in”)); • Output PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter("foo.out"))); • BufferedWriter is not necessary but makes things more efficient • Output streams should be closed explicitly out.close();

  10. import java.io.*; public class Copy { public static void main(String[] args) { String line; try { BufferedReader in = new BufferedReader(new FileReader(args[0])); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(args[1]))); while ((line = in.readLine()) != null) { out.println(line); } in.close(); out.close(); } catch (FileNotFoundException e) { System.out.println("The file " + args[0] + " was not found."); } catch (IOException e) { System.out.println(e); } } }

  11. Writing a String to a file (WriteUtil) • public static void writeString(String s) { • FileWriter fw = getFileWriter("enter output file"); • try { • fw.write(s); • fw.close(); • } catch (IOException e) { • e.printStackTrace(); • } • }

More Related