File Input/Output in Java

 
Module 6
 
File IO and
Internet Sockets
 
F
i
l
e
 
I
n
p
u
t
 
/
 
O
u
t
p
u
t
 
 
 
Saving to file is a way to make
 data 
persistent
 
 Instead of user input, we can 
read from files
 
 Working with files is conceptually 
the same as
reading/writing to the console
 
 Must be comfortable with try/catch (Exceptions)
 
 
S
t
r
e
a
m
s
 
Streams 
push
 and 
pull
 data to/from a 
source
:
 We can read data from (as 
an input stream)
:
Keyboard (System.in, Console)
Files
Network (called Sockets)
 We can write data (as an output stream):
The display (System.out and Console)
Files
Network (called Sockets)
 
 
 
 
G
e
n
e
r
a
l
 
F
i
l
e
 
P
r
o
c
e
s
s
:
 
1.
Open the file stream in a "mode"
 read, write, append, create…
2.
Do what you need to do (read/write)
3.
Close the file stream
 free it for use by other programs
 
You can imagine that there is an "active cursor" that
is moved around the file as you read/write
 
 
 
F
i
l
e
 
R
e
a
d
i
n
g
 
Must 
know the structure
 of the file:
one element per line?
multiple elements per line?
a combination of the two?
how are the fields divided?
Is there a delimiter such as a space or
comma?
Are the fields defined by a certain number of
bytes?
Perhaps the file is 
binary
!
 
F
i
l
e
 
S
t
r
u
c
t
u
r
e
 
E
x
a
m
p
l
e
 
An airline company could store the following data:
flight number
origin airport
destination airport
number of passengers
average ticket price
Such a file could contain the following data:
   AA123,BWI,SFO,235,239.5
   AA200,BOS,JFK,150,89.3
In this case, the 
delimiter is a comma
.
 
A
i
r
l
i
n
e
 
E
x
a
m
p
l
e
 
-
 
J
a
v
a
 
import
 java.io.*;
import
 java.util.*;
public
 
class
 Main {
  
public
 
static
 
void
 main (String args[]) {
    
try
 {
      String dataline = 
""
;
      File myFile = 
new
 File(
"source.csv"
);
      Scanner scan = 
new
 Scanner(myFile);  
// Note the layering
      
while
 (scan.hasNextLine()) {
        dataline = scan.nextLine();
        
// Split the string by commas
        String[] tokens = dataline.split(
","
);
        System.out.println (dataline);
      }
    }
    
catch
 (IOException ioex) {
      System.out.println (
"Error: "
 + ioex.getMessage());
    }
    
// Don’t forget to close the file at some point
  }
}
 
A
i
r
l
i
n
e
 
E
x
a
m
p
l
e
 
 
C
#
 
using
 
System
;
using
 
System
.
IO
;
class
 Example {
  
public
 
static
 
void
 Main(String[] args) {
    
try
 {
      StreamReader sr = 
new
 StreamReader(
"source.csv"
);
      
string
 dataline = 
""
;
      
while
 (!sr.EndOfStream) {
        dataline = sr.ReadLine();
        
string
[] tokens = dataline.Split(
","
);
        Console.WriteLine(dataline);
      }
    }
catch
 (IOException ioex) {
      Console.WriteLine(
"Error: "
+ioex);
    }
    
// Don't forget to close the file at some point
  }
}
 
W
r
i
t
i
n
g
 
T
e
x
t
 
t
o
 
F
i
l
e
s
 
Similar to printing to the screen
 
In Java, you’ll use a PrintWriter (similar to System.out)
 
In C#, you’ll use StreamWriter
 
 
W
r
i
t
i
n
g
 
T
e
x
t
 
t
o
 
F
i
l
e
 
 
C
#
 
using
 
System
;
using
 
System
.
IO
;
class
 Example {
  
public
 
static
 
void
 Main(String[] args) {
    StreamWriter sw = 
null
;
    
try
 {
      sw = 
new
 StreamWriter(
"output.txt"
);
      sw.WriteLine(
"Bob was here"
);
      sw.WriteLine(
"and he's angry"
);
    }
catch
 (IOException ioex) {
      Console.WriteLine(
"Error: "
+ioex);
    } 
finally
 {
      
if
 (sw != 
null
) sw.Close();
    }
  }
}
 
A
p
p
e
n
d
i
n
g
 
v
s
 
O
v
e
r
w
r
i
t
i
n
g
 
Be aware that StreamWriter will overwrite a file by
default.
Thus any data in that file will be lost when you:
StreamWriter sr = new StreamWriter(“a.txt”);
If you wish to preserve the existing data, you have 2
choices:
Make a new file with a new filename.
Append to the existing file:
StreamWriter sw = new StreamWriter(“a.txt”,true);
The true makes StreamWriter append instead of
overwrite.
 
W
r
i
t
i
n
g
 
T
e
x
t
 
t
o
 
F
i
l
e
 
-
 
J
a
v
a
 
import
 java.io.*;
import
 java.util.*;
 
public
 
class
 Main {
  
public
 
static
 
void
 main (String args[]) {
    File myFile;
    PrintWriter pw = null;
    
try
 {
      myFile = 
new
 File (
"output.txt"
);
      pw = 
new
 PrintWriter(myFile);  
// layering
      pw.println(
"Bob was here"
);
      pw.println(
"and he's angry"
);
    }
catch
 (IOException ioex) {
      System.out.println (
"Error: "
 + ioex.getMessage());
    }
finally
 {
      
if
 (pw != null)  pw.close();
    }
  }
}
 
A
p
p
e
n
d
i
n
g
 
t
o
 
a
 
T
e
x
t
 
F
i
l
e
 
-
 
J
a
v
a
 
If you wish to append to a text file in java, it’s a little
more complex.
Create a File object just like you would for Scanner.
Then create a FileOutputStream object that takes
the File Object as a parameter
The second parameter will be true to indicate you
want append mode.
Finally make a PrintWriter object taking the
FileOutputStream as a parameter.
 
A
p
p
e
n
d
i
n
g
 
t
o
 
a
 
T
e
x
t
 
t
o
 
F
i
l
e
 
-
 
J
a
v
a
 
import java.io.*;
 
class Main {
  public static void main(String[] args) {
    try {
      File f=new File("a.txt");
      FileOutputStream fos=new FileOutputStream(f,true);
      PrintWriter pw = new PrintWriter(fos);
      pw.println("Test Line");
      pw.close();
    }
    catch(Exception e) {
    }
  }
}
 
H
o
w
 
f
i
l
e
 
s
y
s
t
e
m
s
 
a
r
e
 
l
a
i
d
 
o
u
t
 
On Windows, the file system typically starts in C:\
In there you’ll find a users folder, in there you’ll find a folder
with the name that you log in as.
In there you’ll find Documents, Desktop and other folders.
On Unix based systems (Mac, Linux) the file system
starts in \
In there, you’ll find usr, in there you’ll find a folder with the
name you log in as.
In there you’ll find Documents and Desktop also
 
When you access a file you have to say which folder you
want to access the file in.
This is not magical, it doesn’t go find the file.
This is the most common problem students have with
FileIO, where they get “File Not Found” exceptions even
though they have the file on their Desktop.
 
W
h
e
r
e
 
a
r
e
 
m
y
 
F
i
l
e
s
?
 
Replit reads/writes files in the project.
Any files your code creates will appear as files on the left
side.
To read in a file, you’ll first need to upload it to replit.  Click
on the 3 dots next to “Files”, and choose upload files.
Intellij
By default It will read and write all files in the IdeaProjects
folder for your project.
For example, if your project is called Lab99 you’d look in
C:/users/yourName/IdeaProjects/Lab99 or
/usr/yourName/IdeaProjects/Lab99
VisualStudio
By default it will read and write all files in your
C:\users\yourName\source\repos\appname\appname\bin\
Debug\net6.0\
 
U
s
i
n
g
 
f
u
l
l
y
 
q
u
a
l
i
f
i
e
d
 
p
a
t
h
s
 
Instead of relying on the default directories, you can
specify where to read/write files when you open them
in your code:
C#:
StreamWriter sr = new
StreamWriter(“C:\users\bob\Desktop\myFile.txt”);
Java:
File myFile = new
File(“C:\users\bob\Desktop\myFile.txt”);
PrintWriter pw = new PrintWriter(myFile);
The same can be done with StreamReader or
Scanner when reading a file.
 
B
i
n
a
r
y
 
F
i
l
e
s
 
Not all files are text:
Audio files
Images
PDFs
Working with binary files is usually very low level
Most often write an array of bytes
Write primitive data types (chars, ints, bytes)
In C#, use a BinaryReader or BinaryWriter
 
B
i
n
a
r
y
 
F
i
l
e
s
:
 
I
m
a
g
e
s
 
O
n
e
 
o
f
 
t
h
e
 
m
o
s
t
 
c
o
m
m
o
n
 
b
i
n
a
r
y
 
f
i
l
e
s
 
y
o
u
l
l
 
e
n
c
o
u
n
t
e
r
 
a
r
e
i
m
a
g
e
 
f
i
l
e
s
 
Almost every image is really a stream of binary data
There is a written specification for each image format
Programs follow this format when they read in images,
so they know how to display the actual picture
 
Some specifications are very complex, like PNGs and JPEGs
Others are simpler, like Windows BMP files
 
B
i
n
a
r
y
 
F
i
l
e
s
:
 
W
i
n
d
o
w
s
 
B
M
P
 
The program first reads in a “header”, a series of
bytes that define width, height, color mode, and
more
You read each value based on how many bytes
long it is
ints take up 4 bytes, shorts take up 2 bytes, etc.
After that, the rest is image data
Again, just a sequence of bytes
 
Link to the 
Window BMP Header
 specification
 
B
i
n
a
r
y
 
F
i
l
e
s
:
 
W
i
n
d
o
w
s
 
B
M
P
 
You can use a program called a hex editor to examine
the binary data inside an image
Once you know the file format specification, you can
even manually change aspects of the image like width
and height
Changing byte 22 – 25 alters the height
 
B
i
n
a
r
y
 
F
i
l
e
s
:
 
W
i
n
d
o
w
s
 
B
M
P
 
You can use a program called a hex editor to examine
the binary data inside an image
Once you know the file format specification, you can
even manually change aspects of the image like width
and height
You can verify if your program read and wrote the
image file correctly
 
L
a
y
e
r
s
 
While reading text, Scanner is abstracting away some of the
details.  When working with binary data, you’ll use more low level
methods:
 
You typically “layer” streams:
Create a stream (stream1)
Create another stream (stream2) using stream1 as a data source
Create another stream (stream3) using stream2 as a data source
 
The purpose of layers?
Stream1 works at a “low level” like bytes
Stream2 constructs higher level data using stream1 as a source
Stream3 constructs even higher level using stream2 as a source
 
E
x
a
m
p
l
e
 
o
f
 
L
a
y
e
r
i
n
g
 
i
n
 
J
a
v
a
 
import
 java.io.*;
import
 java.util.*;
public
 
class
 Main {
  
public
 
static
 
void
 main (String args[]) {
    
try
 {
      
// Can only read bytes from fis – super low level
      FileInputStream fis = 
new
 FileInputStream(
"source.csv"
);
      
// Can read characters from isr
      InputStreamReader isr = 
new
 InputStreamReader (fis);
      
// Can read entire strings from br
      BufferedReader br = 
new
 BufferedReader (isr);
    }
    
catch
 (IOException ioex) {
      System.out.println (
"Error: "
 + ioex.getMessage());
    }
  }
}
 
W
r
i
t
i
n
g
 
B
i
n
a
r
y
 
d
a
t
a
 
i
n
 
J
a
v
a
 
//This writes a file with the letter A in it, because A in ascii is #65.
 
i
mport
 java.io.FileOutputStream;
public
 
class
 FileOutputStreamExample {
  
public
 
static
 
void
 main(String args[]){
    t
ry
{
      FileOutputStream fout=
new
 FileOutputStream(
"myfile.txt"
);
      fout.write(
65
);
      fout.close();
   }
  
catch
(Exception e)  {
    System.out.println(e);}
  }
}
 
W
r
i
t
i
n
g
 
B
i
n
a
r
y
 
i
n
 
C
#
 
using
 
System
;
using
 
System
.
IO
;
class
 Example {
  
public
 
static
 
void
 Main(String[] args) {
    
try
 {
      FileStream myFile = 
new
 FileStream(
"output2.bin"
, FileMode.Create);
      BinaryWriter bw = 
new
 BinaryWriter(myFile);  
// layering
      
// Create an array of 1000 bytes
      
byte
[] arr = 
new
 
byte
[
1000
];
      
for
 (
int
 i = 
0
; i < 
1000
; i++) {
        arr[i] = (
byte
)i;
      }
      
// Write 1000 bytes, starting at 0
      bw.Write(arr, 
0
, 
1000
);
    }
    catch
 (IOException ioex) {
      Console.WriteLine(
"Error: "
+ioex);
    }
  }
}
 
S
o
f
t
w
a
r
e
 
E
n
g
i
n
e
e
r
i
n
g
 
T
i
p
 
Calling the 
close
 method is optional. When the program
finishes executing, all the resources of any unclosed files
are released (back to the operating system)
It is good practice to call the 
close
 method, however,
especially if you will be opening a number of files (or
opening the same file multiple times.)
 
Do not close the standard
 
input, output, or error devices,
however. They are intended to remain open.
 
W
r
i
t
i
n
g
 
a
n
d
 
T
h
e
 
B
u
f
f
e
r
 
It’s *much* more efficient to write giant blocks of data
instead of individual bytes/characters
When you write (or print), it goes into a buffer:
A buffer is just a chunk of memory used to hold data
You never see the buffer
When that buffer is full, the buffer gets pushed out
When a stream 
is closed
, the buffer gets pushed out
There’s a “flush” method for almost all output streams
Forces everything in the buffer to be forced out of the
stream
 
SO, don’t forget to flush!
 
A
n
 
a
w
e
s
o
m
e
 
n
e
w
 
s
k
i
l
l
 
 
 
 
If all you have to do is change the source, how hard
is it to read from the Internet?
 
R
e
a
d
i
n
g
 
f
r
o
m
 
G
o
o
g
l
e
 
-
 
J
a
v
a
 
import
 java.io.*;
import
 java.util.*;
import
 java.net.*;
// This code reads the homepage of Google and prints it line by line
public
 
class
 Main {
  
public
 
static
 
void
 main (String args[]) {
    
try
 {
      String dataline = 
""
;
      Socket mySock = 
new
 Socket(
"www.google.com"
, 
80
);
      OutputStream os = mySock.getOutputStream(); 
// Note the layering
      PrintWriter pw = 
new
 PrintWriter(os);
      pw.println(
"GET /index.html 
\n\n
"
); 
pw.flush();
      Scanner scan = 
new
 Scanner(mySock.getInputStream());
      
while
 (scan.hasNextLine()) {
        dataline = scan.nextLine();
        System.out.println (dataline);
      }
      scan.close();
    }
    
catch
 (IOException ioex) {
      System.out.println (
"Error: "
 + ioex.getMessage());
    }
  }
}
 
R
e
a
d
i
n
g
 
f
r
o
m
 
G
o
o
g
l
e
 
 
C
#
 
using
 
System
;
using
 
System
.
IO
;
using
 
System
.
Net
.
Sockets
;
class
 Example {
  
public
 
static
 
void
 Main(String[] args) {
    
try
 {
      TcpClient client = 
new
 TcpClient(
"www.google.com"
, 
80
);
      NetworkStream ns = client.GetStream();  
// Note the layering...
      StreamReader sr = 
new
 StreamReader(ns);
      StreamWriter sw = 
new
 StreamWriter(ns);
      sw.WriteLine(
"GET /index.html
\n\n
"
); 
sw.Flush();
      
string
 dataline = 
""
;
      
while
 (!sr.EndOfStream) {
        dataline = sr.ReadLine();
        Console.WriteLine(dataline);
      }
      ns.Close();
    }
    
catch
 (IOException ioex) {
      Console.WriteLine(
"Error: "
+ioex);
    }
  }
}
 
D
o
i
n
g
 
s
o
m
e
t
h
i
n
g
 
u
s
e
f
u
l
 
A lot of time coding will be spent with your computer
needing to get information from or send information
to another computer.
Most apps that you use on your phone communicate
with a central service.
Email/Social Media/Chat clients have to get their info
from a central server
Weather apps pull info from a central server
Most games need to save state and score to a central
server.
Things like photo storage apps need to upload
images to be stored on the cloud.
 
A
P
I
s
 
A very common way people communicate with other
computers is via API calls.
Typically a service provider provides a bunch of API
document which explains what calls you can make
Think of these as method calls, except they are not
happening on your computer, they are happening on
a remote computer
Like method calls, they can:
Take in parameters
Return values
Beyond the scope of this class, but most of the world
has agreed on the format of this data to be either
JSON or XML.  These are just ways of formatting
data as text
 
L
e
t
s
 
s
e
e
 
a
 
r
e
a
l
 
w
o
r
l
d
 
e
x
a
m
p
l
e
 
I’ve decided to write an app that let’s people look up
music information.
The user will be able to type in an artist, and see all
that artists albums, songs and general info.
The user can also enter a song, and see what album
it was released on, who the artist is (there may be
one) etc.
Choice 1:
I write a MASSIVE multidimensional array with all the
info in it.  Then I publish my app.
10 minutes after publishing my app, the data is out of
date.
Choice 2:
Find an internet service I can use which keeps this
data up to date for me, and just query it!
 
P
r
e
s
e
n
t
i
n
g
 
M
u
s
i
c
B
r
a
i
n
z
 
This is an opensource (ie, free) website that contains
the information we just discussed.
Go to musicbrainz.org in a browser to check it out.
Type an artist into the top right corner, click search
and see results.
But wait, there’s more!
They allow you to query this information via an API.
So let’s write some code to access this information.
Warning the information comes back in XML format.
If you’ve ever used HTML, it’s a generic form of HTML.
<variable>value</variable>
<array><cell>value</cell><cell>value2</cell></array>
 
M
a
k
i
n
g
 
a
n
 
A
P
I
 
c
a
l
l
 
Musicbrainz API is simple, and you can use it in a
browser:
musicbrainz.org/ws/2/artist?query=Drake
 
Y
o
u
l
l
 
n
e
e
d
 
s
o
m
e
 
i
m
p
o
r
t
s
 
a
n
d
 
m
a
i
n
:
 
import java.util.Scanner;
import java.net.Socket;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
 
public static void main(String[] args) {
    artist_info("Drake");
  }
 
a
r
t
i
s
t
_
i
n
f
o
 
m
e
t
h
o
d
 
 public static void artist_info(String artistName) {
    try {
      String HostName="musicbrainz.org";
      String QueryString="GET /ws/2/artist?query="+artistName+" HTTP/1.0";
 
      Socket mySocket = new Socket(HostName,80);
      OutputStream myoutputstream=mySocket.getOutputStream();
      PrintWriter pw=new PrintWriter(myoutputstream);
 
      pw.println(QueryString);
      pw.println("Host: "+HostName);
      pw.println("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
      pw.println();
      pw.flush();
      Scanner returnPage = new Scanner(mySocket.getInputStream());
      while (returnPage.hasNextLine())  {
        String result = returnPage.nextLine();
        System.out.println (result);
      }
 
    }
    catch(IOException ioe) {
      System.out.println("Unable to get info about artist "+artistName+" Error:"+ioe);
    }
  }
 
U
s
i
n
g
 
a
n
d
 
m
a
i
n
 
m
e
t
h
o
d
 
using System;
using System.IO;
using System.Net.Sockets;
 
 public static void Main (string[] args) {
    artist_info("Zedd");
  }
 
a
r
t
i
s
t
_
i
n
f
o
 
m
e
t
h
o
d
 
  public static void artist_info(string artistName) {
    try {
      string HostName="musicbrainz.org";
      string QueryString="GET /ws/2/artist?query="+artistName+" HTTP/1.0";
 
      TcpClient mySocket = new TcpClient(HostName, 80);
      NetworkStream myStream = mySocket.GetStream();
      StreamReader readStream = new StreamReader(myStream);
      StreamWriter writeStream = new StreamWriter(myStream);
 
      writeStream.WriteLine(QueryString);
      writeStream.WriteLine("Host: "+HostName);
      writeStream.WriteLine("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
      writeStream.WriteLine();
      writeStream.Flush();
 
      while (!readStream.EndOfStream) {
        string result = readStream.ReadLine();
        Console.WriteLine(result);
      }
      mySocket.Close();
    }
    catch(IOException ioe) {
      Console.WriteLine("Unable to get info about artist "+artistName+" Error:"+ioe);
    }
  }
 
S
u
m
m
a
r
y
 
We write files to keep persistent data
 
Working with files (or the network) is similar to working with
keyboard and console
 
You need to know the format of the data you’re working
with
 
Just like ogres, code for reading and writing files has layers
Slide Note
Embed
Share

Learn about file input/output operations in Java. Explore how to read and write data to files, handle exceptions using try/catch, work with streams, understand the process of file handling, and examine file structure examples like storing airline data. Enhance your Java programming skills by mastering file I/O concepts.

  • Java programming
  • File I/O
  • Exceptions handling
  • Streams
  • Data persistence

Uploaded on Sep 24, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Module 6 File IO and Internet Sockets

  2. File Input / Output Saving to file is a way to make data persistent Instead of user input, we can read from files Working with files is conceptually the same as reading/writing to the console Must be comfortable with try/catch (Exceptions)

  3. Streams Streams push and pull data to/from a source: We can read data from (as an input stream): Keyboard (System.in, Console) Files Network (called Sockets) We can write data (as an output stream): The display (System.out and Console) Files Network (called Sockets)

  4. General File Process: 1. Open the file stream in a "mode" read, write, append, create 2. Do what you need to do (read/write) 3. Close the file stream free it for use by other programs You can imagine that there is an "active cursor" that is moved around the file as you read/write

  5. File Reading Must know the structure of the file: one element per line? multiple elements per line? a combination of the two? how are the fields divided? Is there a delimiter such as a space or comma? Are the fields defined by a certain number of bytes? Perhaps the file is binary!

  6. File Structure Example An airline company could store the following data: flight number origin airport destination airport number of passengers average ticket price Such a file could contain the following data: AA123,BWI,SFO,235,239.5 AA200,BOS,JFK,150,89.3 In this case, the delimiter is a comma.

  7. Airline Example - Java import java.io.*; import java.util.*; public class Main { public static void main (String args[]) { try { String dataline = ""; File myFile = new File("source.csv"); Scanner scan = new Scanner(myFile); // Note the layering while (scan.hasNextLine()) { dataline = scan.nextLine(); // Split the string by commas String[] tokens = dataline.split(","); System.out.println (dataline); } } catch (IOException ioex) { System.out.println ("Error: " + ioex.getMessage()); } // Don t forget to close the file at some point } }

  8. Airline Example C# using System; using System.IO; class Example { public static void Main(String[] args) { try { StreamReader sr = new StreamReader("source.csv"); string dataline = ""; while (!sr.EndOfStream) { dataline = sr.ReadLine(); string[] tokens = dataline.Split(","); Console.WriteLine(dataline); } }catch (IOException ioex) { Console.WriteLine("Error: "+ioex); } // Don't forget to close the file at some point } }

  9. Writing Text to Files Similar to printing to the screen In Java, you ll use a PrintWriter (similar to System.out) In C#, you ll use StreamWriter

  10. Writing Text to File C# using System; using System.IO; class Example { public static void Main(String[] args) { StreamWriter sw = null; try { sw = new StreamWriter("output.txt"); sw.WriteLine("Bob was here"); sw.WriteLine("and he's angry"); }catch (IOException ioex) { Console.WriteLine("Error: "+ioex); } finally { if (sw != null) sw.Close(); } } }

  11. Appending vs Overwriting Be aware that StreamWriter will overwrite a file by default. Thus any data in that file will be lost when you: StreamWriter sr = new StreamWriter( a.txt ); If you wish to preserve the existing data, you have 2 choices: Make a new file with a new filename. Append to the existing file: StreamWriter sw = new StreamWriter( a.txt ,true); The true makes StreamWriter append instead of overwrite.

  12. Writing Text to File - Java import java.io.*; import java.util.*; public class Main { public static void main (String args[]) { File myFile; PrintWriter pw = null; try { myFile = new File ("output.txt"); pw = new PrintWriter(myFile); // layering pw.println("Bob was here"); pw.println("and he's angry"); }catch (IOException ioex) { System.out.println ("Error: " + ioex.getMessage()); }finally { if (pw != null) pw.close(); } } }

  13. Appending to a Text File - Java If you wish to append to a text file in java, it s a little more complex. Create a File object just like you would for Scanner. Then create a FileOutputStream object that takes the File Object as a parameter The second parameter will be true to indicate you want append mode. Finally make a PrintWriter object taking the FileOutputStream as a parameter.

  14. Appending to a Text to File - Java import java.io.*; class Main { public static void main(String[] args) { try { File f=new File("a.txt"); FileOutputStream fos=new FileOutputStream(f,true); PrintWriter pw = new PrintWriter(fos); pw.println("Test Line"); pw.close(); } catch(Exception e) { } } }

  15. How file systems are laid out On Windows, the file system typically starts in C:\ In there you ll find a users folder, in there you ll find a folder with the name that you log in as. In there you ll find Documents, Desktop and other folders. On Unix based systems (Mac, Linux) the file system starts in \ In there, you ll find usr, in there you ll find a folder with the name you log in as. In there you ll find Documents and Desktop also When you access a file you have to say which folder you want to access the file in. This is not magical, it doesn t go find the file. This is the most common problem students have with FileIO, where they get File Not Found exceptions even though they have the file on their Desktop.

  16. Where are my Files? Replit reads/writes files in the project. Any files your code creates will appear as files on the left side. To read in a file, you ll first need to upload it to replit. Click on the 3 dots next to Files , and choose upload files. Intellij By default It will read and write all files in the IdeaProjects folder for your project. For example, if your project is called Lab99 you d look in C:/users/yourName/IdeaProjects/Lab99 or /usr/yourName/IdeaProjects/Lab99 VisualStudio By default it will read and write all files in your C:\users\yourName\source\repos\appname\appname\bin\ Debug\net6.0\

  17. Using fully qualified paths Instead of relying on the default directories, you can specify where to read/write files when you open them in your code: C#: StreamWriter sr = new StreamWriter( C:\users\bob\Desktop\myFile.txt ); Java: File myFile = new File( C:\users\bob\Desktop\myFile.txt ); PrintWriter pw = new PrintWriter(myFile); The same can be done with StreamReader or Scanner when reading a file.

  18. Binary Files Not all files are text: Audio files Images PDFs Working with binary files is usually very low level Most often write an array of bytes Write primitive data types (chars, ints, bytes) In C#, use a BinaryReader or BinaryWriter

  19. Binary Files: Images One of the most common binary files you ll encounter are image files Almost every image is really a stream of binary data There is a written specification for each image format Programs follow this format when they read in images, so they know how to display the actual picture Some specifications are very complex, like PNGs and JPEGs Others are simpler, like Windows BMP files

  20. Binary Files: Windows BMP The program first reads in a header , a series of bytes that define width, height, color mode, and more You read each value based on how many bytes long it is ints take up 4 bytes, shorts take up 2 bytes, etc. After that, the rest is image data Again, just a sequence of bytes Link to the Window BMP Header specification

  21. Binary Files: Windows BMP You can use a program called a hex editor to examine the binary data inside an image Once you know the file format specification, you can even manually change aspects of the image like width and height Changing byte 22 25 alters the height

  22. Binary Files: Windows BMP You can use a program called a hex editor to examine the binary data inside an image Once you know the file format specification, you can even manually change aspects of the image like width and height You can verify if your program read and wrote the image file correctly

  23. Layers While reading text, Scanner is abstracting away some of the details. When working with binary data, you ll use more low level methods: You typically layer streams: Create a stream (stream1) Create another stream (stream2) using stream1 as a data source Create another stream (stream3) using stream2 as a data source The purpose of layers? Stream1 works at a low level like bytes Stream2 constructs higher level data using stream1 as a source Stream3 constructs even higher level using stream2 as a source

  24. Example of Layering in Java import java.io.*; import java.util.*; public class Main { public static void main (String args[]) { try { // Can only read bytes from fis super low level FileInputStream fis = new FileInputStream("source.csv"); // Can read characters from isr InputStreamReader isr = new InputStreamReader (fis); // Can read entire strings from br BufferedReader br = new BufferedReader (isr); } catch (IOException ioex) { System.out.println ("Error: " + ioex.getMessage()); } } }

  25. Writing Binary data in Java //This writes a file with the letter A in it, because A in ascii is #65. import java.io.FileOutputStream; public class FileOutputStreamExample { public static void main(String args[]){ try{ FileOutputStream fout=new FileOutputStream("myfile.txt"); fout.write(65); fout.close(); } catch(Exception e) { System.out.println(e);} } }

  26. Writing Binary in C# using System; using System.IO; class Example { public static void Main(String[] args) { try { FileStream myFile = new FileStream("output2.bin", FileMode.Create); BinaryWriter bw = new BinaryWriter(myFile); // layering // Create an array of 1000 bytes byte[] arr = new byte[1000]; for (int i = 0; i < 1000; i++) { arr[i] = (byte)i; } // Write 1000 bytes, starting at 0 bw.Write(arr, 0, 1000); } catch (IOException ioex) { Console.WriteLine("Error: "+ioex); } } }

  27. Software Engineering Tip Calling the close method is optional. When the program finishes executing, all the resources of any unclosed files are released (back to the operating system) It is good practice to call the close method, however, especially if you will be opening a number of files (or opening the same file multiple times.) Do not close the standard input, output, or error devices, however. They are intended to remain open.

  28. Writing and The Buffer It s *much* more efficient to write giant blocks of data instead of individual bytes/characters When you write (or print), it goes into a buffer: A buffer is just a chunk of memory used to hold data You never see the buffer When that buffer is full, the buffer gets pushed out When a stream is closed, the buffer gets pushed out There s a flush method for almost all output streams Forces everything in the buffer to be forced out of the stream SO, don t forget to flush!

  29. An awesome new skill If all you have to do is change the source, how hard is it to read from the Internet?

  30. Reading from Google - Java import java.io.*; import java.util.*; import java.net.*; // This code reads the homepage of Google and prints it line by line public class Main { public static void main (String args[]) { try { String dataline = ""; Socket mySock = new Socket("www.google.com", 80); OutputStream os = mySock.getOutputStream(); // Note the layering PrintWriter pw = new PrintWriter(os); pw.println("GET /index.html \n\n"); pw.flush(); Scanner scan = new Scanner(mySock.getInputStream()); while (scan.hasNextLine()) { dataline = scan.nextLine(); System.out.println (dataline); } scan.close(); } catch (IOException ioex) { System.out.println ("Error: " + ioex.getMessage()); } } }

  31. Reading from Google C# using System; using System.IO; using System.Net.Sockets; class Example { public static void Main(String[] args) { try { TcpClient client = new TcpClient("www.google.com", 80); NetworkStream ns = client.GetStream(); // Note the layering... StreamReader sr = new StreamReader(ns); StreamWriter sw = new StreamWriter(ns); sw.WriteLine("GET /index.html\n\n"); sw.Flush(); string dataline = ""; while (!sr.EndOfStream) { dataline = sr.ReadLine(); Console.WriteLine(dataline); } ns.Close(); } catch (IOException ioex) { Console.WriteLine("Error: "+ioex); } } }

  32. Doing something useful A lot of time coding will be spent with your computer needing to get information from or send information to another computer. Most apps that you use on your phone communicate with a central service. Email/Social Media/Chat clients have to get their info from a central server Weather apps pull info from a central server Most games need to save state and score to a central server. Things like photo storage apps need to upload images to be stored on the cloud.

  33. APIs A very common way people communicate with other computers is via API calls. Typically a service provider provides a bunch of API document which explains what calls you can make Think of these as method calls, except they are not happening on your computer, they are happening on a remote computer Like method calls, they can: Take in parameters Return values Beyond the scope of this class, but most of the world has agreed on the format of this data to be either JSON or XML. These are just ways of formatting data as text

  34. Lets see a real world example I ve decided to write an app that let s people look up music information. The user will be able to type in an artist, and see all that artists albums, songs and general info. The user can also enter a song, and see what album it was released on, who the artist is (there may be one) etc. Choice 1: I write a MASSIVE multidimensional array with all the info in it. Then I publish my app. 10 minutes after publishing my app, the data is out of date. Choice 2: Find an internet service I can use which keeps this data up to date for me, and just query it!

  35. Presenting MusicBrainz This is an opensource (ie, free) website that contains the information we just discussed. Go to musicbrainz.org in a browser to check it out. Type an artist into the top right corner, click search and see results. But wait, there s more! They allow you to query this information via an API. So let s write some code to access this information. Warning the information comes back in XML format. If you ve ever used HTML, it s a generic form of HTML. <variable>value</variable> <array><cell>value</cell><cell>value2</cell></array>

  36. Making an API call Musicbrainz API is simple, and you can use it in a browser: musicbrainz.org/ws/2/artist?query=Drake

  37. Youll need some imports and main: import java.util.Scanner; import java.net.Socket; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; public static void main(String[] args) { artist_info("Drake"); }

  38. artist_info method public static void artist_info(String artistName) { try { String HostName="musicbrainz.org"; String QueryString="GET /ws/2/artist?query="+artistName+" HTTP/1.0"; Socket mySocket = new Socket(HostName,80); OutputStream myoutputstream=mySocket.getOutputStream(); PrintWriter pw=new PrintWriter(myoutputstream); pw.println(QueryString); pw.println("Host: "+HostName); pw.println("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"); pw.println(); pw.flush(); Scanner returnPage = new Scanner(mySocket.getInputStream()); while (returnPage.hasNextLine()) { String result = returnPage.nextLine(); System.out.println (result); } } catch(IOException ioe) { System.out.println("Unable to get info about artist "+artistName+" Error:"+ioe); } }

  39. Using and main method using System; using System.IO; using System.Net.Sockets; public static void Main (string[] args) { artist_info("Zedd"); }

  40. artist_info method public static void artist_info(string artistName) { try { string HostName="musicbrainz.org"; string QueryString="GET /ws/2/artist?query="+artistName+" HTTP/1.0"; TcpClient mySocket = new TcpClient(HostName, 80); NetworkStream myStream = mySocket.GetStream(); StreamReader readStream = new StreamReader(myStream); StreamWriter writeStream = new StreamWriter(myStream); writeStream.WriteLine(QueryString); writeStream.WriteLine("Host: "+HostName); writeStream.WriteLine("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"); writeStream.WriteLine(); writeStream.Flush(); while (!readStream.EndOfStream) { string result = readStream.ReadLine(); Console.WriteLine(result); } mySocket.Close(); } catch(IOException ioe) { Console.WriteLine("Unable to get info about artist "+artistName+" Error:"+ioe); } }

  41. Summary We write files to keep persistent data Working with files (or the network) is similar to working with keyboard and console You need to know the format of the data you re working with Just like ogres, code for reading and writing files has layers

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#