I/O in java reading and writing

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

byte file I/O

- FileInputStream and FileOutputStream are unbuffered byte-oriented streams - for every read/write, the JV< contacts the OS, slowing things down - and read byte by byte - these are low-level and not generally recommended in apps - but everything else is built on top of this

System.outandSystem.errarePrintStreamobjects, but basically have the same formatting capabilities

- System.out (Standard Output) is normally the screen but can be redirected at the OS level - System.err (Standard Error )is also normally the screen, can also be redirected, and is used for things like traceback stacks. Some apps redirect this to a file so that the user doesn't see it (normally), but it's there for developers to help debug

PrintWriter

- The PrintWriter class is a child of Writer• But it has constructors to wrap File, OutputStream, or Writer • Various overloads of print( ) and println( ), plus format( )

basic I/O hierarchy - byte streams

- read/write single bytes(8 bits) - inputstream, outputstream hierarchies

if you have the choice, use buffered character streams (faster, UTF-oriented) and wrap them inside some useful class to make your life easier

- scanner for input - printwriter for output

basic I/O hierarchy - random access

- stands alone from the other I/O classes

Other I/O classes

-> The next example uses an InputStreamReader wrapping a ByteArrayInputStream to interpret some UTF-8 characters -> You can see what encoding is the default by looking at the Properties- >Resource page for a project or data file

Scanner code example

//This read a file of int data into an arrayList public static void readInts(String filename){ try(Scanner scanner = new Scanner(new BufferedReader(new FileReader(filename)))){ while(scanner.hasNext()){ intList.add(scanner.nextInt()); } }catch (IOException e){ System.out.println(e.getMessage());} }

• BufferedReader and BufferedWriter use buffering: local storage of previously read/written data to speed things up

And you can use more useful member methods• Typically, you wrap these around the character read/write classes: BufferedReader input = new BufferedReader( new FileReader(filename)); - BufferedWriter and FileWriter work similarly

what is a previous example of scanner used to wrap other class?

Scanner keyboard = new Scanner( System.in );// Wraps an InputStream • System.in (Standard input) is normally the keyboard, but it can be redirected from a file or command output at the OS level • But it has constructors to wrap File, Path, and any Readable (FileReader, BufferedReader)

wrapping file I/o

Wrapping one class around another class allows you to use the outer class's member methods, which are implemented via calls to the inner class's member methods To enable this, the outer class needs an overloaded constructor with a parameter of the inner class type. From the docs: "Constructor and Description BufferedReader(Reader in) Creates a buffering character-input stream that uses a default-sized input buffer." • Reader <- InputStreamReader <- FileReader

buffered versus unbuffered : unbuffered

each read or write request is handled directly by the operating system

basic I/O hierarchy - stream

fancy name for an input source or output destination; include files, keyboard/screen, network devices

character file I/O

filereader and filewriter are unbuffered character-oriented streams - again, very low level, but read/write 2 bytes per call

character file I/O code example with double wrap

public static String read( String filename ) { try ( BufferedReader input = new BufferedReader( new FileReader(filename)) ){ String s; StringBuilder sb = new StringBuilder(); while ( (s = input.readLine()) != null ) { sb.append(s+"\n"); } } catch (IOException e) { System.out.println(e.getMessage()); } return sb.toString(); }

basic I/O hierarchy - characters streams

reader, writer hierarchies -> These were added for internationalization and speed

buffered versus unbuffered : buffered

the JVM reads more data from a file than it currently needs, so that th next read(s) will be faster - it's already in memory

byte file I/O code example

try ( FileOutputStream out = new FileOutputStream(filename); FileInputStream in = new FileInputStream("src/mycode.java") ) { int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); }

character file I/o code example

try ( FileWriter out = new FileWriter(filename); FileReader in = new File®eader("src/mycode.java") ) { int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); }

Scanner

• The Scanner class is not part of the I/O library (it's in java.util), but it can be used to wrap other classes for parsing out data from a buffered stream


Ensembles d'études connexes

Chapter 8 Quiz :) (study guide), Chapter 9 Study

View Set

Ch 2 smart book practice quiz BA 370 SDSU

View Set

Chp 8 Intrapartum assessment & intervention :)

View Set

10 Most Commom Financial Mistakes

View Set

Indiana Life & Health Insurance Exam Review

View Set