Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.

Similar presentations


Presentation on theme: "Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output."— Presentation transcript:

1 Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output (concentrate on text I/O) Winter 2015CMPE212 - Prof. McLeod1

2 Pointers – A Question So, which way is better to declare a 3 by two-dimensional array?: int[][] wayOne = new int[3][10000]; int[][] wayTwo = new int[10000][3]; Or, it makes no difference? Winter 2015CMPE212 - Prof. McLeod2

3 How Much Memory Does a Pointer Use? String aString = “Hello!”; How much memory does aString consume? Not the string itself, just the pointer to the string. 32 bits for 32 bit Java, 64 bits for 64 bit Java (unless you are using compressed pointers…) Winter 2015CMPE212 - Prof. McLeod3

4 null Pointer or null Reference null is not a keyword in Java – more like a literal constant. What is a null pointer? What is a null pointer error? Does null have a type? Can you test a pointer to see if it is null ? How? Why would you want to? Winter 2015CMPE212 - Prof. McLeod4

5 null Pointer or null Reference, Cont. What is the difference between string1, string2 and string3 ?: String string1 = ""; String string2 = null; String string3; See TestNull.java. Is an unassigned primitive type variable null ? What are the contents of an uninitialized array? Winter 2015CMPE212 - Prof. McLeod5

6 null References, Cont. The idea of a null reference was first introduced into ALGOL W back in 1965 by C.A.R. Hoare (also known as the inventor of Quicksort). See: References-The-Billion-Dollar-Mistake-Tony-Hoare Winter 2015CMPE212 - Prof. McLeod6

7 Using null in Java You can test to see if a pointer is null (See file input code, for example. This is how you detect the end of the file.) Sometimes, to widen the scope of a variable you need to declare it before you can instantiate it. In this case you must assign the variable pointer to null. “Bad things” will happen if you do not! A NullPointerException is probably the most frustrating error to encounter in Java! Winter 2015CMPE212 - Prof. McLeod7

8 Fall 2014CISC124 - Prof. McLeod8 File I/O Files provide a convenient way to store and re- store to memory larger amounts of data. Three kinds of file I/O: –Text –Binary –Random access

9 File I/O, Cont. Text –ASCII characters written to a text file (*.txt for example) that is readable by a text editor such as Notepad. –Text files can be used to interface with other programs like Word and Excel (use *.csv, for example). Binary –Stored exactly as in memory, in binary. –Not readable with a text editor. Random Access –Like Binary, but can read and write any byte in the file in any order. Fall 2014CISC124 - Prof. McLeod9

10 File I/O and Exceptions Since you are “reaching out” through the OS to read and write files, errors may occur that Java cannot do anything about. Such as: –When reading: file does not exist. –When reading: no permission to read file. –When reading: part of file is corrupt. –When writing: folder cannot be found. –When writing: no permission to write to folder. –When writing: failure during write. –Etc.! Fall 2014CISC124 - Prof. McLeod10

11 File I/O and Exceptions, Cont. Most file I/O methods will indicate that one of these kinds of errors has been encountered by throwing an exception and then halting execution. If not caught, an exception will cause your program to crash. Fall 2014CISC124 - Prof. McLeod11

12 Java 7 File I/O or “NIO” Best tutorial and reference: In older Java versions files could not be closed properly if an exception was thrown leading to escalating problems and a program crash. In Java 7+ file closing takes place “behind the scenes” and files should always be closed properly. Fall 2014CISC124 - Prof. McLeod12

13 Problem with Java <7 File I/O Exceptions How to close the file when things are going wrong? // Declare fileWriter object = null try { // instantiate the fileWriter object } catch (IOException e) { // try to do something about the problem? } finally { // executes whether an exception is thrown or not fileWriter.close(); // but this can throw an // exception! } Fall 2014CISC124 - Prof. McLeod13

14 Fall 2014CISC124 - Prof. McLeod14 Exceptions – Cont. Syntax of a Java 7 “try-with-resources block”: try (instantiation; instantiation; …) { // other statements that might // generate an exception }[ catch (exception_type identifer) { // block of statements }][ catch (exception_type identifer) { // block of statements … }][ finally { // block of statements }]

15 Try-With-Resources Syntax The instantiation(s) inside the set of ( ) immediately after the try keyword are declared resources that must be local to the try/catch block. Note that there is no ; at the end of the list and that there does not have to be any catch blocks. These resources must all implement the AutoCloseable interface, which means that the try block can close the resource when it is finished. Resources will be closed whether or not an exception is thrown because their scope is forced to be in the try block only. As a result, all use of the resource must also take place in the try block. Fall 2014CISC124 - Prof. McLeod15

16 Fall 2014CISC124 - Prof. McLeod16 Exceptions – Cont. Text file opening example in older Java versions: FileReader fileIn = null; try{ fileIn = new FileReader(filename); } catch (FileNotFoundException e) { System.out.println("Cannot open file!"); System.exit(0); } Scanner fileInput = new Scanner(fileIn); Then you would have more try/catch blocks for reading and then closing the file.

17 Fall 2014CISC124 - Prof. McLeod17 Exceptions – Cont. Note that you can use the code e.getMessage() in the catch block to get an error description string from the exception inside the catch clause. When used with some exceptions, the e.getMessage() method just returns null, as they do not contain a message. (You can also get a stack dump, but this is not too useful…)

18 Java 7 File Open and Read Example try (BufferedReader reader = Files.newBufferedReader(file, charset)) { do { line = reader.readLine(); if (line != null) textArray[lineCount] = line; lineCount++; } while (line != null); } catch (IOException err) { System.out.println("Unable to read file: " + filename); System.exit(0); } Fall 2014CISC124 - Prof. McLeod18

19 Java 7 File Read – Cont. In this case everything is carried out in the try block. That’s because the resource ( reader on the previous slide) is scoped only inside the try block – so you have to do everything there – you have no choice! As soon as you move outside the try block (outside the scope of the resource) the resource is closed automatically. Fall 2014CISC124 - Prof. McLeod19

20 Java 7 File Read – Cont. Where do the file and charset objects come from? Before the try-with-resources block: Charset charset = Charset.forName("US-ASCII"); Path file = Paths.get(filename); Charset is not too exciting and we will talk more about the Java 7 Path object later. Fall 2014CISC124 - Prof. McLeod20


Download ppt "Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output."

Similar presentations


Ads by Google