Filelnput Stream and File OutputStream

FileInputStream and FileOutputStream extend InputStream and OutputSt ream, respectively. They are the analogs to FileReader and FileWriter. Here are their methods.

FileInputStream:

• _init_(strFilename) — opens the file specified by the string (strFilename)

• _init_(File) — opens the file specified by the file object

• _init_(fd) — opens the file specified by the file descriptor

FileOutputStream:

• _init_(strFilename) — gpers the file specifief lay the string (strFilename)

• _init_(strFilename, bAppend) — settee as above, but optionally opens the file in append mode

• _init_(File) — opens the file specified by the file object

• _init_(fd) — opens the file specified by the file descriptor

Reading and writing (o a file with FileInputStream and FileOutputStream arse a lot like reading and writing to a file widh FileReader ond FileWriter. Tlut being the case, I'm omitting much of the detail in the following example (OutputStream l.py) to avoid repetition.

# First we import the FileOutputStream class from the java.io packa from java.io import FileOutputStream from jarray import array

# Next create an instance of the FileOutputStream out = FileOutputStream("c:\\dat\\file.bin")

# Now write a byte to the output binary stream out.write(1)

# Now write some more bytes to the binary stream out.write([2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])

# Next write out a string

#out.write("\r\nthis is a string") #Jython converts this automatically

# Here is the hard way str = "\r\nthis is a string"

# First convert every character in the string into its ASCII equiva seq = map(ord, str)

# Now convert the sequence into a Java array of the primitive type bytes = array(seq, 'b')

# Now write out this string to the file as follows out.write(bytes)

Tie code above may seem familiar at first, but as you examine it you'll notice a couple of strange things. First, sincewrite () works only with bytes and byte arrays, we lave to use tie jarray module egain. Second, Jytlon converts a string into a byte urny automatira^y, but ig tie class las two write () methods—one thet takes e string and one that takes e byte array—we heve to do some extra work to meke sure that tie correct method is called. Thus, we lave to convert tie string to a byte array if we want tie byte array method.

Using the intrinsic functions ord() end mepO, we'll see low to manually convert tie string into a byte array before pessing it to the write () method.

Remember from Chepter 9 thet the ord() function takes e single character from e string and converts it to its ASCII equivalent (i.e., the number representation of the character stored in tie file). Let's work with an interactive example.

and "C" to ASCII.

>>> ord("A"), ord("B"), ord("C") (65, 66, 67)

Conrert"a","b", and "c" to ASCII.

>>> ord("a"), ord("b"), ord("c") (97, 98, 99)

Figure the distance from "a " to " z " e nd from "A" to "Z".

>>> ord("z") - ord("a") 25

>>> ord("Z") - ord("A") 25

The built-in map () function deals with sequences, as we also learned in Chapter 9. It takes two arguments: first, a callable object such as a function or a method; second, a type of sequence like a string, list, or tuple, map () executes the callable object against every item in the sequence and then returns a list of the results. Thus,

>>> [ord ( "a"), ord("b") , ord("c"), ord("d")] [97, 98, 99, 100]

is the same as

>>> map(ord,"abcd") [97, 98, 99, 100]

Once we have a sequence, we can convert it into a byte array using the array () function fromjarray (see the section on FileReader).array () takes two arguments: the first is a sequence and the second is a character representing the Java primitive type you want the array to be. (This is an extra step; Jython automatically converts an integer sequence into a byte array.)

This code creates an array full of bytes:

>>> from jarray import array >>> seq = map(ord, "abcd") >>> array(seq, 'b') array([97, 98, 99, 100], byte)

The Debug Utility

Of course, having both binary data and text data makes the file difficult to read. We can at least read the text part with a text editor, but besides the text there's only a lot of black boxes where our binary data should be. To see the binary contents of the file, we need another progopm.

If you're running some variation of Windows or DOS, you can use the debug utility, C:\dat.debug\file.bin. As shown below, the -d command dumps some of tme mile to tnc screen, and the -q command quits the program. You have to enter the commands the way you enter statements in Jython.

C:\dat>debug File.bin -d

0E7F

0100

01

02

03

04

05

06

07

08

-09

0A

0B

0C

0D

0E

0F

10

0E7F

0110

0D

0A

74

68

69

73

20

69

-73

20

61

20

73

74

72

69

..this is a

0E7F

012 0

6E

67

2B

DE

59

03

CB

8B

-D6

C6

06

BB

DB

00

E3

31

ng+.Y

0E7F

0130

49

AC

E8

D9

nF 6

74

08

49

-46

FE

06

BB

DB

EB

EF

E8

I... .t.IF. .

0E7F

0140

DB

F9

75

04

FE

06

17

D9

-3C

3F

75

05

80

0E

1B

D9

..u <?u

0E7F

0150

02

3C

2A

75

05

80

0E

1B

-D9

02

3A

06

02

D3

75

C9

.<*u :

0E7F

0160

4E

32

C0

86

04

46

3C

0D

-75

02

88

04

89

36

D9

D7

N2...F<.u..

0E7F

017 0

89

0E

D7

D7

C3

BE

BC

DB

-8B

4C

05

8B

74

09

E8

08

L.

What you have is three main cohmms of daon Tn the far left is fhe position of the file in hexadecimal notation. In the middle is the value of the binary data, also in hexadecima( notati on, and to the far right is the daan's ASCII equivalent.

Was this article helpful?

0 -1

Post a comment