File Input - Introduduction to Jave Programming - Lecture Slides, Slides of Java Programming

In the course of the Introduction to Jave Programming, we study the basic syntax and the basic program in java. In these lecture slides the key points are:File Input, Output, Text File Output, Survey, Streams, Download, Streams, Information Flow, Streaming Video, Downloading Video

Typology: Slides

2012/2013

Uploaded on 04/23/2013

sarmistha
sarmistha 🇮🇳

3.8

(22)

112 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
File Input and Output
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download File Input - Introduduction to Jave Programming - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

File Input and Output

Highlights

  • text file output
  • text file input

Streams

Download

“Opening” a file

File output is very similar to terminal output, except we have to open and close files To create a stream between a variable name and file name: Type Variable name PrintWriter constructor Another constructor File nameDocsity.com

Writing to a file

After you have opened a file (stream), you can then write to it This is done in an almost identical manner with println() (or print() or printf()) Terminal: File:

File output imports

You need to import PrintWriter and FileOutputStream from java.io You can also simply import everything from java.io by doing: (See: HelloWorldFile.java)

File I/O exceptions

Whenever you open a file (for write or read), a checked exception is thrown named java.io.FileNotFoundException This means you have to catch it! (See: HelloWorldFileCorrected.java)

Appending to files

What happens if I run HelloWorldFile multiple times? Open file and override: Open file and append: (See: HelloWorldFileAppend.java) Docsity.com

File writing overview

  • You need to open a file before writng to it
  • You should close the file when you are done
  • You need to catch FileNotFoundException
  • You can either override or append to files
  • You cannot go backwards and “replace” or “undo”
  • You cannot “preppend” to a file (must either append from end or override)

Scanner setup

Open: Read: Close: FileInputStream not FileOutputStream (See: HelloWorldReader.java)

End of file (EOF)

What would happen if I tried to have two nextLine() calls in HelloWorldReader? This would in fact crash the program, very similar to if you used nextInt() on a String There is no way for Scanner to tell if it reached the end of the file (EOF)

BufferedReader

BufferedReader can also read from a file To open a file: FileReader not FileInputStream However, BufferedReader only has 2 methods: read() - reads a single character readLine() - reads a line, like nextLine()

EOF in BufferedReader

If the end of file is reached in BufferedReader, then read() will return - (readLine() will return null) Since read() only reads a single character, you have to put together Strings/ints manually (See: BufferedReaderMaxBad) (See: BufferedReaderMaxGood)