Simple File I/O

advertisement
Objectives
1

File I/O: using Scanner with File

Inserting into Partially Filled Array

Deleting from Partially Filled Array

Static methods and variables
Fall 2012 CS2302: Programming Principles
File I/O
Use Scanner with File:
Scanner inFile =
new Scanner(new File(“in.txt”));

Similar to Scanner with System.in:
Scanner keyboard =
new Scanner(System.in);

2
Fall 2012 CS2302: Programming Principles
Reading in int’s
Scanner inFile = new Scanner(new File(“in.txt"));
int number;
while (inFile.hasInt())
{
number = inFile.nextInt();
// …
}
3
Fall 2012 CS2302: Programming Principles
Reading in lines of characters
Scanner inFile = new Scanner(new File(“in.txt"));
String line;
while (inFile.hasNextLine())
{
line = inFile.nextLine();
// …
}
4
Fall 2012 CS2302: Programming Principles
Multiple types on one line
5
// Name, id, balance
Scanner inFile = new Scanner(new File(“in.txt"));
while (inFile.hasNext())
{
name = inFile.next();
id = inFile.nextInt();
balance = inFile.nextFloat();
// … new Account(name, id, balance);
}
-------------------String line;
while (inFile.hasNextLine())
{
line = inFile.nextLine();
Scanner parseLine = new Scanner(line) // Scanner again!
name = parseLine.next();
id = parseLine.nextInt();
balance = parseLine.nextFloat();
// … new Account(name, id, balance);
}
Fall 2012 CS2302: Programming Principles
Summary

Create a Scanner object
Scanner inFile = new Scanner(new File(“in.txt"));

Use the methods next(), nextByte(), nextShort(),
nextInt(), nextLong(), next Float(), nextDouble(), or
nextBoolean() to obtain to a string, byte, short, int,
long, float, double, or boolean value. For example,
double d = inFile.nextDouble();
6
Fall 2012 CS2302: Programming Principles
My suggestion

Use Scanner with File
–

Use hasNext…() to check for EOF
–

7
while (inFile.hasNext…())
Use next…() to read
–

new Scanner(new File(“in.txt”))
inFile.next…()
Simpler and you are familiar with methods for
Scanner
Fall 2012 CS2302: Programming Principles
Ending a Loop with a Sentinel Value
Often the number of times a loop is executed is
not predetermined. You may use an input value
to signify the end of the loop. Such a special input
values is known as a sentinel value.
8
Fall 2012 CS2302: Programming Principles
Example
double array[] = new double[100];
// counter for how many are stored
int arraySize = 0;
Scanner inFile = new Scanner(new File(“in.txt"));
double value, sentinel = -1;
value = inFile.nextDouble();
while (value != sentinel) {
array[arraySize] = value;
arraySize++;
value = inFile.nextDouble();
}
System.out.println("Array has " + arraySize + " elements");
inFile.close();
9
Fall 12
CS2302: Programming Principles
Insertion
10
public void insertAt(double[] data, double val, int index) {
if(index < 0 || index > arraySize) {
System.out.println("Insert index out of bounds");
}
else if(arraySize >= data.length) {
System.out.println("Partial array is full");
}
else {
//move elements to the right
for(int j = arraySize; j > index; j--) {
data[j] = data[j-1];
}
// put the new value in place
data[index] = val;
// one more element stored
arraySize ++;
}
}
Fall 2012 CS2302: Programming Principles
Deletion
11
public double removeAt(double[] data, int index) {
double value;
if(index < 0 || index > arraySize) {
System.out.println("Insert index out of bounds");
}
else if(arraySize >= data.length) {
System.out.println("Partial array is full");
}
else {
value = data[index];
//move elements to the left
for(int j = index; j <= arraySize; j++) {
data[j] = data[j+1];
}
arraySize --;
}
return value;
}
Fall 2012 CS2302: Programming Principles
The static keyword

Java methods and variables can be declared
static

These exist independent of any object

This means that a Class’s
–
–
12
static methods can be called even if no objects of
that class have been created and
static data is “shared” by all instances
Fall 2012 CS2302: Programming Principles
Example
class StaticTest {
static int i = 47;
public static void main(String[] args){
i++;
System.out.println(“Before call increase, i = ” + i);
increase();
System.out.println(“After call increase, i =” + i);}
public static void increase(){
i++;
System.out.println(“Inside increase, i = ” + i);
}
}
14
Fall 2012 CS2302: Programming Principles
Download