Text Files
dynamic structured data types
Arrays are static data structures and files are dynamic data structures. Why?
Sequential Text Files
The PLAN for writing a text file looks like this:
Write Text File�--------------------
Create an empty file, with an appropriate name�Connect to the file�Write one line of text into the file�Write another line of text into the file�Write another line...�Keep writing lines of text until you are finished�Close 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
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
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)
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();
class petType
{
String petName;
int id;
String tel;
}
WRITING A SEQUENTIAL FILE
COPY, PASTE and EXPLORE
TIP
Everyday Use of Text Files.
Inspecting Age Of Empires III, XML Save game file
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.
Sequential Access File Organisation
Read more : http://www.ehow.com/info_12116528_advantages-sequential-access-files.html
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
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
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).
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
Link 1 - Sequential VS Random Access
Creating a Backup File
CHALLENGE
Create a program which produces a backup file.
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:
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.
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:
Help
----------------------------------
REMOVED
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
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� }� }� }�} |