1 of 19

Text Files

dynamic structured data types

Arrays are static data structures and files are dynamic data structures. Why?

2 of 19

Sequential Text Files

The PLAN for writing a text file looks like this:

Write Text File--------------------

Create an empty file, with an appropriate nameConnect to the fileWrite one line of text into the fileWrite another line of text into the fileWrite another line...Keep writing lines of text until you are finishedClose the file (break the connection)

Write

The PLAN for reading a text file looks like this:

Read Text File�----------------

Open a connection to the file�Read one line of text�Read another line of text�Read another line of text�...

Stop reading when you get to the end of the file

Close the file (break the connection)

Read

3 of 19

import java.io.File;

import java.util.Scanner;

import java.io.*; // required for “throws IOException”

public class ReadFile {

public static void main(String[] args) throws IOException

{

File file = new File("myFirstFile.txt"); //open the file

Scanner input = new Scanner(file); //prepare for input

while ( input.hasNextLine() )

{

String country = input.next(); //readLine

System.out.println(country);

int code = input.nextInt(); //readLine

System.out.println(code*1000);

}

input.close(); //close the file!

}

}

China

10

Indonesia

20

Brazil

30

China

40

Russia

50

Mexico

60

India

70

Brazil

80

China

90

China

100

Dominican

110

Armenia

120

myFirstFile.txt

READING A SEQUENTIAL FILE

COPY, PASTE and EXPLORE

4 of 19

import java.io.File;

import java.util.Scanner;

import java.io.*; // required for “throws IOException”

public class WriteTextToFile

{

public static void main(String[] args) throws IOException

{

FileWriter f = new FileWriter(new File("outfile.txt"));

int x = 6;

f.write("Hello " + x + "\n");

f.write(1234 + " goodbye\n");

f.close(); //always close your files so that they save properly.

}

}

WRITING A SEQUENTIAL FILE

COPY, PASTE and EXPLORE

If you have issues with new lines, try

.write( System.lineSeparator() );

If you have issues with integers in text files, try

Integer.toString(number)

5 of 19

petType[] list = new petType[10];

FileWriter f = new FileWriter(new File("outfile.txt"));

int x = 6;

f.write("Hello " + x + "\n");

f.write(1234 + " goodbye\n");

f.close();

  1. Modify the program to create a list of pets and then export list[] to a new text file.
  2. Create a program to read the text file in Q1 and output the contents on screen.
  3. Modify Q1 to produce a text file as comma separated values with country and numVal on each line seperated by a comma.

class petType

{

String petName;

int id;

String tel;

}

WRITING A SEQUENTIAL FILE

COPY, PASTE and EXPLORE

6 of 19

  • Use the code snippet above to explore the use of comma seperated values in the data file.
  • Explore the features and use of https://www.generatedata.com/ to generate large amounts of test data. Use this tool to create a txt file containing 500 records for name, id, email. The format of the file is up to you. Is is easiest to read a CSV or one field per line? Why?
  • Read the file into an array, sort the results on id, display as a table on screen and then export the results to a new sorted file. Tip: Read file line by line using readLine() method. Split each line on comma to get an array of attributes using String.split() method.

7 of 19

TIP

8 of 19

Everyday Use of Text Files.

Inspecting Age Of Empires III, XML Save game file

  1. The use of keyword tags in an XML file is a useful for storing game options and settings. With tags, the order of the file is not important. Why?
  2. Discuss how you could import settings in such an unordered file. How else could you format this file if you wanted to access the same data?

9 of 19

Example Text File

Website Config File with PHP

Controlling file access permissions on text files can be a critical security issue.

Why are these markup areas blanked out?

The permissions for this dangerous file is strictly set to 444.

Explore the chmod command in DOS or Terminal. Try locking a file safely.

10 of 19

  • Open the console app to view your system log files. View the system.log file, read it and analyse the data and its purpose. Describe the data and discuss why it could be useful?
  • Discuss how such a log file could help an end user and ALSO how it can help a developer.

11 of 19

  • The screen shot above shows the dialogue for a crash report.
  • Discuss why Apple offers this feature as part of crash recovery and prevention.
  • Extension: Observe the crashed thread in the log file. Explain the meaning of a multithreaded application in terms of parallel processing?

12 of 19

Sequential File

A computer program makes a sequential file simply by writing data records, one after the other, into a newly created file area. The records may all have the same length, or lengths may vary. If they vary, each record ends with a special character or characters; when a program subsequently reads the file, it uses the characters to determine where one record ends and the next begins. Programs read sequential files the same way they were created: beginning with the first record and continuing, in ascending order, to the end.

Speed

Compared to direct-access files, programs process sequential access files faster. Programs read direct-access file records in any order, but that flexibility comes at the price of slower performance. The positioning mechanism of the computer’s hard drive works much less for sequential files than direct files, and the computer’s central processing unit (CPU) likewise has less work with a sequential file.

Simplicity

Sequential files are easy to read because of their simple organization. It is a simple matter to write new programs to read existing sequential files, since the program reads the records as a simple series until it encounters an end-of-file (EOF) mark. Though the file may have nearly any content, from digitally recorded music to the text of a novel, the file’s structure has no surprises.

Data Sharing

Programs which share data use the sequential access file format as a “common language.” For example, a database program may export a file for a spreadsheet. The database system’s native files are very sophisticated; the spreadsheet program cannot access them directly. However, it easily reads the database’s sequential export file, especially if the database user chooses comma- or tab-delimited formats. Because of the ease of sharing data, many major applications have import and export functions using sequential files.

STUDY: Read the link

13 of 19

Further Reading

Journey into cryptography

https://www.khanacademy.org/computing/computer-science/cryptography

How have humans protected their secret messages through history? What has changed today?

Why Online Banking Is Safe - Iucuonline.org

https://www.iucuonline.org/onlineserv/HB/security_site/privacy.html

The privacy of communications between you (your browser) and our servers is ensured via encryption. Encryption scrambles messages exchanged between …

NSA has cracked encryption protecting your bank account

http://www.digitaltrends.com/web/nsa-has-cracked-the-encryption-protecting-your-bank-account-gmail-and-more/

Encryption techniques used by online banks, email providers, and many other sensitive Internet services to keep your personal data private …

What is 128-Bit Encryption? - Definition from Techopedia

https://www.techopedia.com/definition/29708/128-bit-encryption

128-bit encryption is a data/file encryption technique that uses a 128-bit key to encrypt and decrypt data or files. It is one of the most secure encryption methods …

It would take significantly longer than the age of the universe to crack a 128-bit key. SSL uses public-key encryption to exchange a session key between the client and server; this session key is used to encrypt the http transaction (both request and response).

Advanced Encryption Standard

https://en.wikipedia.org/wiki/Advanced_Encryption_Standard

For AES, NIST selected three members of the Rijndael family, each with a block size of 128 bits, but three different key lengths: 128, 192 and 256 bits. AES has been adopted by the U.S. government and is now used worldwide. It supersedes the Data Encryption Standard (DES), which was published in 1977.

STUDY: Read all links and read around the topic

14 of 19

15 of 19

Creating a Backup File

CHALLENGE

Create a program which produces a backup file.

16 of 19

Encryption Techniques for Credit Cards

How to validate a Credit Card Number?

Most credit card number can be validated using the Luhn algorithm, which is more or a less a glorified Modulo 10 formula!

The Luhn Formula:

  • Drop the last digit from the number. The last digit is what we want to check against
  • Reverse the numbers
  • Multiply the digits in odd positions (1, 3, 5, etc.) by 2 and subtract 9 to all any result higher than 9
  • Add all the numbers together
  • The check digit (the last number of the card) is the amount that you would need to add to get a multiple of 10 (Modulo 10)

http://www.freeformatter.com/credit-card-number-generator-validator.html

Create a file contains thousands of random credit card numbers. Create an algorithm that reads each CC number and verifies if it is true. Output each verified number to a new file.

17 of 19

Challenge

An ordered class file is used to store a permanent record of student’s id, first name, last name and grade.

Design a modular menu driven solution to manage the class file.

Your solution should provide the following features:

  1. display the contents of the file in order of ID
  2. append a student to the end of the file and then sort it by ID
  3. create a backup of the file before adding new students

Help

  • you could modify size() to get to the end of the file and append. Persist, Question and Reflect. Create a prototype for proof of concept. Can you append?
  • think of other strategies to append a new record

----------------------------------

REMOVED

18 of 19

class myType

{

String name;

char gender;

int age;

}

public class ReadFileToArray

{

public static void main(String[] args) throws IOException

{

myType[] myList = new myType[size("DataFile.txt")/3];

fill(myList,"DataFile.txt");

display(myList);

}

public static int size(String text) throws IOException

{

File file = new File(text);

Scanner input = new Scanner(file);

int numLines=0;

while ( input.hasNextLine() )

{

String theLine = input.nextLine(); //consume a line

numLines++; //count each line consumed

}

input.close(); //always close the file

return numLines;

}

public static void fill(myType[] arr, String text ) throws IOException

{

File file = new File(text);

Scanner input = new Scanner(file); //reset to start of file

for (int i=0; input.hasNextLine();i++ )

{

arr[i] = new myType();

arr[i].name = input.next();

arr[i].gender = input.next().charAt(0);

arr[i].age = input.nextInt();

}

input.close(); //always close the file

}

These modules import records from a text file into an array.

Create a suitably formatted txt file to successfully execute this program.

Modify the program to sort the array and then export it to a new file called “backup.txt

REMOVED

19 of 19

import java.util.Scanner;�import java.io.IOException;�import java.util.InputMismatchException;��public class UsingScanner�{� public static void main(String args[]) throws IOException� {� Scanner input = new Scanner(System.in);� boolean accepted = false;� � while(!accepted)� {� try � {� System.out.println("\nPlease enter int 1");�� int in1 = input.nextInt(); � accepted = true;� } � � catch (InputMismatchException e) � {� System.out.println("is not an integer");� input.next(); //stops the infinite loop as exception was thrown and scanner needsto be reset� }� }� }�}