File Streams for Output - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

File Streams for Output

Description:

{ FileOutputStream outputFile = new FileOutputStream('output.data' ... 'Apocalypse Now' 4 7 3 'file:///e:/phw/onto/java/apnow.jpg' 'Bedtime for Bonzo' 8 8 7 ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 33
Provided by: sdl5
Category:

less

Transcript and Presenter's Notes

Title: File Streams for Output


1
File Streams for Output
2
Opening and Closing an Output File
  • Connect to an output file
  • FileOutputStream outputFile
  • new FileOutputStream(" output. data" )
  • output.data file specification
  • Can write 1byte at a time
  • Close output streams
  • outputFile.close()

3
Writing to Output Stream
  • Write strings to that stream
  • PrintStream output new PrintStream(output Fi
    le)
  • output PrintStream variable
  • outputFile FileOutputStream instance
  • Use print and println methods
  • stream_name.print(information_to_be_printed)
  • stream_name.println(information_to_be_printed)

4
Example
  • import java.io.
  • import java.util.
  • public class Demonstrate
  • public static void main(String argv) throws
    IOException
  • FileOutputStream outputFile new
    FileOutputStream("output.data")
  • PrintStream output new PrintStream(outputFile)
  • Vector mainVector
  • mainVector Auxiliaries.readMovieFile("input.da
    ta")
  • int counter
  • int size mainVector.size()
  • for(counter 0counter lt size counter)
  • output.println(
  • ((Movid) mainVector.elementAt(counter)).rating
    ()
  • )
  • outputFile.close()
  • System.out.println("File written")

5
Example ( cont )
  • ---------------Sample Data---------------
  • "Apocalypse Now" 4 7 3 "file///e/phw/onto/ja
    va/apnow.jpg"
  • "Bedtime for Bonzo 8 8 7
  • ------------------Result----------------
  • File written
  • ---------------output.data---------------
  • 14
  • 23

6
Compilation Units and Packages
7
Class Hierarchy
8
Modularized Programs
  • Modularized Programs
  • Programs that are divided into units that can be
    developed and maintained separately
  • File is a Compilation Unit
  • All classes in a file are compiled together
  • All class names accessible in all other classes
    in the same file
  • However, only one of the classes is universally
    accessible

9
Packages
  • Can place compilation units into a common package
  • Use a package statement at the beginning of each
    compilation unit
  • package package_name
  • Package names consist of dot-separated components
    which correspond to the final components of the
    path name of the compilation unit
  • The initial component of the path name are taken
    from the value of CLASSPATH, an OS environment
    variable

10
Packages ( cont )
  • package onto.java.entertainment
  • public abstract class Attraction
  • ...
  • package onto.java.entertainment
  • public class Movie extends Attraction
  • package onto.java.entertainment
  • public class Symphony extends Attraction
  • package onto.java.entertainment
  • import java.io.
  • import java.util.
  • public class Auxiliaries

11
Using Packages
  • The location of compilation units
  • Example

12
Setting CLASSPATH
  • On Windows OS
  • set CLASSPATHc\phw c\javalib c\java\bin
  • On Unix
  • setenv CLASSPATH /usr/phwjava/lib java/bin
  • You must provide for all the directories that
    contain classes in the value of the CLASSPATH
    variable

13
Why Packages for?
  • Access protection
  • High degree of interaction within a package
  • Reduce access/interaction across package
    boundaries
  • Methods/variables are by default accessible
    within the same package
  • Name space management
  • Avoid name clash (e.g., you can define your own
    Vector class if it is placed other than in
    java.util not recommended, though).
  • A class belongs to one package if you omit the
    declaration, the system provides one.

14
Referring to Class
  • The difference between Vector and
    java.util.Vector you need to specify import or
    not.
  • The correspondence between package name and path
    name enables Java to find compilation units
    specified in the import statement
  • Java simply combines CLASSPATH with the package
    name to find it
  • Specify a specific public class and the
    compilation unit
  • import onto.java.entertainment.Attraction
  • You can use Attraction directly

15
Referring to Class ( cont )
  • Import all the package's public classes
  • import onto.java.entertainment.
  • All compilation units are available
  • Although package names are hierarchical, import
    declarations are not (e.g.,import java.awt.
    imports only from java.awt, not from its
    subpackages java.awt.image. )
  • Java automatically imports all classes in the
    java.lang package

16
Examples
  • Load onto.java.entertainment package, and use
    public classes
  • import onto.java.entertainment.
  • import java.io.
  • import java.util.
  • public class Demonstrate
  • public static void main(String argv)
  • Vector mainVector
  • mainVector Auxiliaries.readMovieFile("input.da
    ta")
  • int counter
  • int size mainVector.size()
  • for(counter 0 counter lt size counter)
  • System.out.println(
  • ((Movid) mainVector.elementAt(counter)).rating
    ()
  • )

17
Examples ( cont )
  • Another way, not using import statement,but use
    package names in class
  • names
  • import java.io.
  • import java.util.
  • public class Demonstrate
  • public static void main(String argv)
  • Vector mainVector
  • mainVector
  • onto.java.entertainment.Auxiliaries.readMovief
    ile("input.data")
  • int counter
  • int size mainVector.size()
  • for(counter 0counterlt sizecounter)
  • System.out.println(
  • ((onto.java.entertainment.Movie)
  • mainVector.elementAt(counter)).rating())

18
Another Example
  • If the CLASSPATH includes .
  • package set
  • import java.util.
  • public class Set
  • private Vector v
  • ..
  • import set.
  • public class Main
  • public static void main(String args )
  • Set a new Set(10)
  • ....

19
Protected and Private Variables and Methods
20
Public Instance Variable and Methods
  • Marked by public keyword
  • Universal access to instance variables and
    methods
  • Can read and write instance variables directly
    from any where
  • Example
  • public abstract class Attraction
  • public int minutes
  • public attraction(int d) minutes d
  • Read gt instance.minutes
  • Write gt instance.minutes new.value

21
Limit Access to Instance Variable and Methods
  • Private instance variable and method
  • Marked by private keyword
  • Limit access to instance variables and methods
    to the class in which they are introduced
  • Hiding implementation detail
  • Access indirectly through public getter and
    setter
  • Example
  • public class Attraction
  • public Attraction(int d) minutes d
  • public int getMinutes() return minutes
  • public void setMinutes(int d) minutes d
  • private int minutes

22
Limit Access to Instance Variable and Methods
(cont)
  • Read gt instance.getMinutes()
  • Write gt instance.setMinutes(new.value)
  • Protected instance variable and method
  • Marked by protected keyword
  • Limit access to instance variables and methods in
    the same class, compilation unit, or package.
  • Also accessible from any subclass of the class
    in which they are declared

23
Default Access
  • Access levels for classes/interfaces
  • public keyword accessible to all classes
  • No keyword accessible only to classes in the
    same package
  • Access levels for members of classes/interfaces
  • public keyword accessible to all classes
  • No keyword accessible only to classes in the
    same package
  • protected keyword accessible classes in the same
    package, and to subclasses in other packages
    (with some complex restrictions)
  • Package P contains class X and class subX (X
    defines a protected member M)

24
Default Access ( cont )
  • Package Q contains class Y (a subclass of X) and
    class subY
  • If code in X or subX invokes M on instance of X
    or subX OK
  • If code in X or subX invokes M on instance of Y
    or subY OK
  • If code in Y invokes M on instance of X or subX
    No
  • If code in Y invokes M on instance of Y or subY
    Yes
  • If code in subY invokes M on instance of Y No
  • private keyword not accessible to any other
    classes
  • Public members of non-public class/interface
  • Accessible only inside its package
  • Complex exceptional cases, though

25
Interface for Imposing Requirement
26
Display Rating Information
  • Desired Output
  • The Last House on the Left
  • Gone with the Wind
  • Need a method that produces a string of zero to
    10 stars from an integer
  • public class Display
  • static String starString(int n)
  • String s ""
  • int counter
  • for(counter 0 counter lt 10 counter)
  • if (counter lt n)
  • s s ""
  • else
  • s s " "

27
Display Rating Information ( cont )
  • return s
  • Change to supply starString with an instance
    rather than with an integer
  • Attraction class must have a star-computing
    method
  • public abstract class Attraction
  • public int starCount() return rating()/ 3
  • ...

28
Display Rating Information ( cont )
  • public class Display
  • static String starString(Attraction x)
  • String s ""
  • int counter
  • int n x.starCount()
  • for(counter 0counterlt 10counter)
  • if (counter lt n) s s ""
  • else s s " "
  • return s
  • starString method accepts only Attraction
    instances

29
Multiple Inheritance
  • If java were to support multiple inheritance,
    solution is as below
  • Specify Attraction class to have Object and
    StarProvider class as direct superclasses
  • Attraction class inherits the abstract starCount
    method from the abstract StarProvider class
  • Declare the parameter of the starString method to
    be a StarProvider parameter to ensure that
    starCount method works
  • Java does not provide multiple inheritance.

30
Interface
  • Interface
  • Java allows to impose requirements on a class
    from multiple class like interfaces
  • Difference with abstract class
  • No ordinary methods can be defined
  • Many interfaces can impose requirements on the
    same class
  • Purposes of interface
  • Impose requirements without forcing those
    requirements into ordinary superclass-subclass
    chain
  • Can serve as a type indicator

31
Interface ( cont )
  • Interfaces define only abstract methods, No
    possibility of conflict
  • A class can implement multiple interfaces
  • Make an interface impose requirements on a
    particular class using an interface
  • public class class_name implements
    interface_name
  • ...

32
Example
  • public interface StarInterface
  • public abstract int starCount()
  • public abstract class Attraction implements
    StarInterface
  • public int starCount() return rating()/ 3
  • ....
  • public class Display
  • static String starString(StarInterface i)
  • String s ""
  • int counter
  • int n i.starCount()
  • for(counter 0counter lt 0counter)
  • if (counterlt n) s s ""
  • return s
Write a Comment
User Comments (0)
About PowerShow.com