Lec 06 David Presentation on Advanced Spring

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
Java I/O and Java Networking (Client Side) Yoshi.
פיתוח מונחה עצמים – שפת JAVA קבצים. References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה.
Java I/O Input: information brought to program from an external source
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Richiami di Java Input/Output. import java.io.*; public class Simple { public static void main(String a[]){ new Simple(); } public Simple() { byte buffer[]=new.
I / O in java. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture.
File IO Basics By Dan Fleck Coming up: Data Streams.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
IST 256 Application Programming for Information Systems Xiaozhong Liu Yatish Hegde
Two Ways to Store Data in a File  Text format  Binary format.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Company Input/Output Stream –. Input/Output Stream Data Program Input Stream Output Stream.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
java.io supports console and file I/O
The Java IO System Different kinds of IO Different kinds of operations
CSC1351 Class 6 Classes & Inheritance.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Files and Streams The material in this chapter is not tested on the AP CS exams.
BINARY I/O IN JAVA CSC 202 November What should be familiar concepts after this set of topics: All files are binary files. The nature of text files.
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Exception Handling, Reading and Writing in Files, Serialization,
OO Design and Programming II I/O: Reading and Writing
CMSC 202 Text File I/O.
Ch14 Files and Streams OBJECTIVES
University of Central Florida COP 3330 Object Oriented Programming
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Java Methods ("Chapter %d", 14); Streams and Files A & AB
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
I/O Streams- Basics Byte Streams and Character Streams
Programming in Java Files and I/O Streams
Java’s Hierarchy Input/Output Classes and Their Purpose
Java Programming Course
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Stream Oriented I/O Computer Programming II Dr. Tim Margush
Files and Streams in Java
Web Design & Development Lecture 8
David Davenport Spring 2005
Presentation transcript:

Lec 06 David Presentation on Advanced Spring Build Sports Van application in class File I/O in Java $git fetch gerber updates $git fetch gerber updates_ws $git checkout -b lec06 gerber/updates $git checkout -b lec06_ws gerber/updates_ws

Themyleaf http://www.thymeleaf.org/

Introducing Thymeleaf It's a Java Template Engine Can be used as view layer in Spring MVC First stable release: July 2011 Elegant, configurable, extensible 21st-century feature set

How does it look like?

Standard and SpringStandard Dialects XML, XHTML 1.0, XHTML 1.1 and HTML5 support. Complete substitute for view-layer web technologies such as JSP. Easy-to-use, elegant syntax based on attributes only (these dialects include no tags). Great for web templates: <input type="text" th:field="*{name}" /> instead of <mylib:text field="name" /> Natural templating for easy prototyping: display your templates statically in a browser (without running your app server). Expression evaluation: OGNL (Standard) and Spring Expression Language (SpringStandard). Complete integration with Spring MVC (SpringStandard): form binding, property editors, internationalization, etc. Spring WebFlow support (including AJAX events). Fully-featured template logic: iteration, conditional evaluation, context variable declaration, etc. Several options for layout (Composite View pattern): A builtin mechanism (Thymeleaf's native fragment inclusion). Specific Apache Tiles integration (through extras package). Can be used along with SiteMesh. Internationalization support: easy inclusion of externalized messages into templates. URL rewriting capabilities for adding context and session information to URLs. JavaScript and Dart inlining: intelligent evaluation of expressions inside javascript/dart code. Template validation support for XML, XHTML 1.0 and XHTML 1.1.

Can Jsp do it?

Can JSP+JSTL Do it?

Can Velocity do it?

Can FreeMarker Do it?

...Can Thymeleaf do it?

How do we evaluate it? “valid document, don't break structure” Templates should be statically displayable Static = Open in browser, no web server Templates should work as prototypes

Now Let’s write templates!

Texts th:text HTML-escaped text (default) th:utext unescaped text

Formatting #dates, #calendars, #numbers, #strings...

URLs @{...} syntax Automatic URL-rewriting is performed

Iteration th:each

Iteration status th:each

Conditionals th:if th:unless th:swithc/th:case

Forms and bean-binding th:object, th:field

Page compostion Declare fragment with th:fragment Resuse th:include

Who is using Thymeleaf?

Vans with Thymeleaf >thymeleaf example (see router at ~/posts/) Examine POM for starters added -web and thyme Web, thyme, jpa, hal, h2 Other possible apps: make/model : movie/actor : restaurant/dish : country/attraction

Cars with Thymeleaf >thymeleaf example (see router at ~/posts/) thymeleaf.org/ >thymeleaf example (see router at ~/posts/) Examine POM for starters added -web and thyme Web, thyme, jpa, hal, h2 Other possible apps: make/model : movie/actor : restaurant/dish : country/attraction

I/O (input and output)

File I/O Read Memory (Primary Memory) Hard Disk (Secondary Memory) Process Write

Java - Streams JAVA provides 2 types of streams Text streams - containing ‘characters‘ Program I ‘ M A S T R I N G \n Device Binary Streams - containing 8 – bit information Program 01101001 11101101 00000000 Device

FilterInputStream is superclass 29

You will read in bytes but store them in int -128 ~ +127 -1 implies end of stream 30

See ByteStreamDriver ByteStream

Stream with Unicode int b1 = in.read(); int b2 = in.read(); char c = (char) (b1*256 + b2); Difficult to handle That is why Java introduced Reader/Writer in Java 1.1 32

Character Stream The Java platform stores character values using Unicode conventions Character stream I/O automatically translates this internal format to and from the local character set. Character streams are often wrappers for byte streams. The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes. See CharStreamDriver 33

What is a Buffer You can use buffered I/O streams for an overhead reduction (overhead generated by each such request often triggers disk access, network activity, or some other operation that is relatively expensive). Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. See BuffStreamDriver 34

Streams in I/O http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html Java8 stream is like a collection, only arrayed in time. Java I/O stream is linked to a physical device and either consumes or produces a stream of data.

What is an i/o Stream Java programs perform I/O through streams. A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner, even if the actual physical devices to which they are linked differ. Thus, the same I/O classes and methods can be applied to any type of device. This means that an input stream can abstract many different kinds of input: from a disk file, a keyboard, or a network socket. Likewise, an output stream may refer to the console, a disk file, or a network connection. Streams are a clean way to deal with input/output without having every part of your code understand the difference between a keyboard and a network, for example. Java implements streams within class hierarchies defined in the java.io package. 36

See StreamExample and others in package. Stream Concept Input Stream Output Stream See StreamExample and others in package. 37

java.io.InputStream/OutputStream BufferedInputStream ByteArrayInputStream DataInputStream FileInputStream FilterInputStream ObjectInputStream PipedInputStream PushbackInputStream SequenceInputStream StringBufferedInputStr eam LineNumberInputStrea m BufferedOutputStream ByteArrayOutputStream DataOutputStream FileOutputStream FilterOutputStream ObjectOutputStream PipedOutputStream PrintStream 38

Examine Object Hierarchy in IDE File Operations FileInputStream FileOutputStream Examine Object Hierarchy in IDE 39

What is a Pipe A pipe consists of a pair of channels: A writable sink channel and a readable source channel. Once some bytes are written to the sink channel they can be read from source channel in exactly the order in which they were written. Whether or not a thread writing bytes to a pipe will block until another thread reads those bytes, or some previously-written bytes, from the pipe is system-dependent and therefore unspecified. Many pipe implementations will buffer up to a certain number of bytes between the sink and source channels, but such buffering should not be assumed. See PipeExample 40

IOException IOException is everywhere! Note that it is a checked exception We need to try/catch it Why do we need to deal with IO Exception? 41

IOException java.io.InputStream public abstract int read() throws IOException java.io.OutputStream public abstract void write(int b) throws IOException 42

See StreamRedirection System.setOut System.setOut(new PrintStream(new FileOutputStream(“output.txt”))); System.setIn System.setIn(new FileInputStream(“input.txt”)); See StreamRedirection 43

Reader/Writer BufferedReader CharArrayReader FileReader FilterReader InputStreamReader PipedReader StringReader PushbackReader LinedNumberReader BufferedWriter CharArrayWriter FileWriter FilterWriter OutputStreamWriter PipedWriter StringWriter PrintWriter 44

Line-Oriented Reader/Writer public class CopyLines { public static void main(String[] args) throws IOException { BufferedReader inputReader = null; PrintWriter outputWriter = null; inputReader = new BufferedReader(new FileReader(“input.txt")); outputWriter = new PrintWriter(new FileWriter("output.txt")); String line; while ((line = inputReader.readLine()) != null) { outputWriter.println(line); } inputReader.close(); outputWriter.close(); 45

Reading Files public static void readFile() { import java.io.*; public static void readFile() { BufferedReader ins = new BufferedReader(new FileReader(“test.txt”)); while(ins.ready()) String s = ins.readLine(); System.out.println(s); } ins.close(); Important: Place the code inside Try{…} Catch{….} Wherever necessary

Writing File public static void writeFile() { import java.io.*; public static void writeFile() { String s = “I love programming”; BufferedWriter outs = new BufferedWriter(new FileWriter(“test_out.txt”)); outs.write(s); } outs.close(); Important: Place the code inside Try{…} Catch{….} Wherever necessary

Reading tokens from file import java.io.*; Import java.util.Scanner; Public static void readFile() { BufferedReader ins = new BufferedReader(new FileReader(“test.txt”)); Scanner scanner = new Scanner(ins); while(scanner.hasNext()) System.out.println(scanner.next()); } scanner.close(); Important: Place the code inside Try{…} Catch{….} Wherever necessary

Reading and Writing Files Create a stream object and associate it with a disk-file Give the stream object the desired functionality while there is more information read(write) next data from(to) the stream close the stream

Browse Files using GUI private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser fc = new JFileChooser(); if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) File file = fc.getSelectedFile(); String inputFilePath = file.getAbsolutePath().toString(); jTextField1.setText(inputFilePath); }