Files - PowerPoint PPT Presentation

About This Presentation
Title:

Files

Description:

Users were frustrated by their lack of intimacy with the computer. ... 1962 The first video game (Spacewar) is invented by MIT grad student Steve Russel ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 26
Provided by: thaddeusf
Category:
Tags: files | russel

less

Transcript and Presenter's Notes

Title: Files


1
Files
  • CSC 171 FALL 2001
  • LECTURE 18

2
History Operating Systems
  • Operating systems (originally called monitors or
    supervisors) had been developed in the late 1950s
  • Users were frustrated by their lack of intimacy
    with the computer.
  • 1961 Fernando Corbató, MIT, produced CTSS
    (Compatible Time Sharing System) for the IBM
    7090/94, the first effective time-sharing system
    and coincidentally the first practical means of
    remote access.

3
History
  • By the mid-1960's the jet airliner (and Boeing)
    had revolutionized the airline travel business
    but the reservation process was inadequate. After
    remote access had been proven by the CTSS system,
    IBM produced the first large scale, on-line,
    real-time reservation tracking system, named
    SABRE for American Airlines, and soon to be
    copied by others.

4
History III
  • 1962 The first video game (Spacewar) is
    invented by MIT grad student Steve Russel

5
File IO
  • We have seen character I/O
  • Sometimes, we want to have other data types
    stored in file
  • Its all just bits

6
Consider saving an array
  • a new float55
  • for(int i 0 ilta.lengthi)
  • for(int j 0 jltai.lengthj)
  • aij ((float)i1) /
  • ((float)j1)

7
Open the File
  • private void saveArray()
  • try
  • JFileChooser chooser new JFileChooser()
  • PrintWriter out1 null
  • if (chooser.showOpenDialog(null)
  • JFileChooser.APPROVE_OPTION)
  • File selectedFile chooser.getSelectedFile()
  • FileWriter writer new FileWriter(selectedFile)
  • out1 new PrintWriter(writer)

8
Write the array
  • for(int i 0 ilta.lengthi)
  • for(int j 0 jltai.lengthj)
  • out1.println(aij " ")
  • System.out.println(" " aij)
  • writer.close()
  • catch (IOException e)
  • System.out.println("problem")

9
Open the file
  • private void restoreArray()
  •  
  • try
  • JFileChooser chooser new JFileChooser()
  • BufferedReader in1 null
  • if (chooser.showOpenDialog(null)
  • JFileChooser.APPROVE_OPTION)
  • File selectedFile chooser.getSelectedFile()
  • FileReader reader new FileReader(selectedFile)
  • in1 new BufferedReader(reader)

10
Read the array
  • for(int i 0 ilta.lengthi)
  • for(int j 0 jltai.lengthj)
  • String s1 in1.readLine()
  • System.out.println(s1)
  • aij (float) Double.parseDouble(s1)
  • reader.close()
  • catch (IOException e) System.out.println("
    problem")
  • repaint()

11
Text File
  • 3.0
  • 1.5
  • 1.0
  • 0.75
  • 0.6
  • 4.0
  • 2.0
  • 1.3333334
  • 1.0
  • 0.8
  • 5.0
  • 2.5
  • 1.6666666
  • 1.25
  • 1.0
  • Text File
  • 1.0
  • 0.5
  • 0.33333334
  • 0.25
  • 0.2
  • 2.0
  • 1.0
  • 0.6666667
  • 0.5
  • 0.4

12
Byte by Byte Text file 303 bytes
  • 0000000 1 . 0 \r
    \n 0 . 5
  • 0000020 \r \n 0 . 3 3
    3 3 3 3 3 4
  • 0000040 \r \n 0 .
    2 5
  • 0000060 \r \n 0 . 2
    \r \n 2
  • 0000100 . 0 \r \n
    1 . 0
  • 0000120 \r \n 0 . 6 6 6
    6 6 6 7
  • 0000140 \r \n 0 . 5
    \r
  • 0000160 \n 0 . 4
    \r \n 3 . 0
  • 0000200 \r \n 1 . 5
  • 0000220 \r \n 1 . 0
    \r \n 0 . 7
  • 0000240 5 \r \n 0
    . 6
  • 0000260 \r \n 4 . 0
    \r \n 2
  • 0000300 . 0 \r \n
    1 . 3 3 3 3
  • 0000320 3 3 4 \r
    \n 1 . 0
  • 0000340 \r \n 0 . 8
    \r
  • 0000360 \n 5 . 0
    \r \n 2 . 5
  • 0000400 \r \n 1 . 6
    6 6 6 6 6 6
  • 0000420 \r \n 1 .
    2 5
  • 0000440 \r \n 1 . 0
    \r \n

13
Open the file (binary)
  • private void saveArray2()
  • try
  • JFileChooser chooser new JFileChooser()
  • DataOutputStream out1 null
  • if (chooser.showOpenDialog(null)
  • JFileChooser.APPROVE_OPTION)
  • File selectedFile chooser.getSelectedFile()
  • FileOutputStream writer new
  • FileOutputStream(selectedFile)
  • out1 new DataOutputStream(writer)

14
Write the file
  • for(int i 0 ilta.lengthi)
  • for(int j 0 jltai.lengthj)
  • out1.writeFloat(aij)
  • System.out.println(" " aij)
  • writer.close()
  • catch (IOException e) System.out.println("
    problem")

15
Open the file
  • private void restoreArray2()
  • try
  • JFileChooser chooser new JFileChooser()
  • DataInputStream in1 null
  • if (chooser.showOpenDialog(null)
  • JFileChooser.APPROVE_OPTION)
  • File selectedFile chooser.getSelectedFile()
  • FileInputStream reader new FileInputStream(sel
    ectedFile)
  • in1 new DataInputStream(reader)

16
Read the file
  • for(int i 0 ilta.lengthi)
  • for(int j 0 jltai.lengthj)
  • aij in1.readFloat()
  • reader.close()
  • catch (IOException e) System.out.println("
    problem")
  • repaint()

17
Same data - 100 Bytes
  • 0000000 1.0000000e00 5.0000000e-01
    3.3333334e-01 2.5000000e-01
  • 0000020 2.0000000e-01 2.0000000e00
    1.0000000e00 6.6666669e-01
  • 0000040 5.0000000e-01 4.0000001e-01
    3.0000000e00 1.5000000e00
  • 0000060 1.0000000e00 7.5000000e-01
    6.0000002e-01 4.0000000e00
  • 0000100 2.0000000e00 1.3333334e00
    1.0000000e00 8.0000001e-01
  • 0000120 5.0000000e00 2.5000000e00
    1.6666666e00 1.2500000e00
  • 0000140 1.0000000e00

18
Data Streams
  • close()
  • readBoolean()
  • readByte()
  • readChar()
  • readDouble()
  • readFloat()
  • readInt()
  • readLong()
  • readShort()
  • readLine()
  • flush()
  • close()
  • writeByte(int b)
  • writeBoolean(boolean v)
  • writeBytes(String s)
  • writeChar(int v)
  • writeChars(String s)
  • writeDouble(Double v)
  • writeFloat(Float v)
  • writeInt(int v)
  • writeLong(long v)
  • writeShort(int v)

19
Serializable Objects
  • We can write whole objects to files

20
  • public AccountRecord( int acct, String first,
  • String last, double bal )
  • setAccount( acct )
  • setFirstName( first )
  • setLastName( last )
  • setBalance( bal )
  • public void setAccount( int acct ) account
    acct
  • public int getAccount() return
    account
  • public void setFirstName( String first )
    firstName first
  • public String getFirstName() return
    firstName
  • public void setLastName( String last )
    lastName last
  • public String getLastName() return
    lastName
  • public void setBalance( double bal )
    balance bal
  • public double getBalance() return balance
  • public String toString()
  • return "Account Record "
  • " " account

21
  • // Java core packages
  • import java.io.
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • public class CreateSequentialFile extends
    JFrame
  • private ObjectOutputStream output
  • public CreateSequentialFile()
  • AccountRecord record
  • openFile()
  • try
  • // create new record
  • record new AccountRecord( 1,
    "Ted","Pawlicki",100)
  • System.out.println(record.toString())
  • output.writeObject( record )

22
  • // allow user to specify file name
  • private void openFile()
  • // display file dialog, so user can choose
    file to open
  • JFileChooser fileChooser new
    JFileChooser()
  • fileChooser.setFileSelectionMode(
  • JFileChooser.FILES_ONLY )
  • int result fileChooser.showSaveDialog(this
    )
  • // get selected file
  • File fileName fileChooser.getSelectedFile(
    )
  • // display error if invalid
  • if ( fileName null
  • fileName.getName().equals( "" ) )
  • System.err.println( "Invalid File
    Name")
  • return

23
  • // close file and terminate application
  • private void closeFile()
  • // close file
  • try
  • output.close()
  • System.exit( 0 )
  • // process exceptions from closing file
  • catch( IOException ioException )
  • System.err.println( "Error closing
    file")
  • System.exit( 1 )
  • public static void main( String args )

24
  • public ReadSequentialFile()
  • AccountRecord record
  • openFile()
  • try
  • // create new record
  • record (AccountRecord) input.readObject()
  • System.out.println(record.toString())
  • record (AccountRecord) input.readObject()
  • System.out.println(record.toString())
  • record (AccountRecord) input.readObject()
  • System.out.println(record.toString())
  • catch(ClassNotFoundException e)
  • System.err.println(" Problem with class ")
  • System.exit(1)
  • catch(IOException e)

25
  • 0000000 254 355 \0 005 s r \0 \r A c
    c o u n t R
  • 0000020 e c o r d G 346 306 343 235
    233 005 \r 002 \0 004
  • 0000040 I \0 007 a c c o u n t
    D \0 007 b a l
  • 0000060 a n c e L \0 \t f i r
    s t N a m e
  • 0000100 t \0 022 L j a v a / l
    a n g / S t
  • 0000120 r i n g L \0 \b l a
    s t N a m e
  • 0000140 q \0 \0 001 x p \0 \0 \0
    001 _at_ Y \0 \0 \0
  • 0000160 \0 \0 \0 t \0 003 T e d t
    \0 \b P a w l
  • 0000200 i c k i s q \0 \0 \0
    \0 \0 \0 002 _at_ 237
  • 0000220 _at_ \0 \0 \0 \0 \0 t \0 004 A
    d a m t \0 005
  • 0000240 F r a n k s q \0 \0
    \0 \0 \0 \0 003 A
  • 0000260 c 022 320 \0 \0 \0 \0 t \0 003
    J o e t \0 005
  • 0000300 S c h m o
  • 0000305
Write a Comment
User Comments (0)
About PowerShow.com