Free £25 Bet!
Free £50 Bet at VCBet!
Free £25 Bet!

In association with Sports-Punter Free Bets Odds Comparison BetHelp Limso

We are the Official Forum of FreeBetting.net & FCBet.com


Sports News Sports Stats Live Scores OddsChecker Place Bets Suggest a Site


Go Back   The Punters Lounge - The World's Best Betting Forum > Systems, Strategy and General Betting Help > Punters Tools & General Betting Help Forum

Punters Tools & General Betting Help Forum Everyone has tools that use to help them bet. Match reports, odds comparison services, tipster services, betting calculators, selection software - discuss them here.

UK & Irish Football Forum | Western European Football Forum | UEFA Cup & Champions League Football Forum | International Football Forum | Eastern & Southern European Football Forum | Nordic & Scandinavian Football Forum | Non European Football Forum | At The Races Forum | At The Races Systems Forum | Other Sports Forum | USA Sports Forum | Fantasy & Fun Comps Forum | Free Bets Forum | Systems & Strategy Forum | Glory Hunter's Forum | Tipster's Challenge Forum | Daily Racing Comp Forum | Euro & Worldwide Comp Forum | Poker Tourneys Forum | Poker Strategy Forum | Poker Chat Forum | Poker Live Forum | Poker Challenges Forum | Poker Staking Forum | e-Sport Poker League Forum | Bookmakers & Exchanges Forum | Punter's Tools/Betting Help Forum | General Chat Forum | Tech & Gaming Forum | Sports Banter Forum | Live Sports Feeds Forum

Closed Thread
 
Thread Tools Display Modes
Old 22-06-2004, 03:00   #1 (permalink)
Pro Punter
 
Datapunter's Avatar
 
Join Date: 23 Oct 2003
Location: Westdorpe
Age: 43
Posts: 5,365
Default Lesson 7 - Language, reading and writing files

Lesson 7 ( Word document )

Punters Lounge JAVA programming course.
Lesson 7 - Learning the language, reading from and writing to files.


A program needs to be able to bring in information from an external place and send it out to an external location again. Where Java gets its info can be quite a few locations such as in a file, on disk, somewhere on the network, in memory, or in another program. Also, the information can be of any type: objects, characters, images, or sounds. Here we are going to deal with reading from and writing to files.

For the SCOLARS: this is the part of the tutorial we are dealing with here:
java.sun.com/docs/books/tutorial/essential/io/index.html



7.1 Reading from a file

What we do to read a file is open a stream to that file, read the stream sequentially, that is bit by bit, until there is nothing more to read. The same the other way, open a stream to a file, send info bit by bit, until there is nothing more to send and we close the stream. Opening a stream is like making a phone call, pick up the horn, wait for a dial tone, dial a number, wait for a response, start talking. The whole action in one is called opening a stream.

First we create a File object and call it FromFile.

File FromFile = new File("in-file.txt");

So we declare we want to use the name FromFile for a File object, then we create a new File object and relate it to the physical file called in-file.txt, and then we assign the name FromFile
( using the = sign ) to that file object.

Next we open an inputstream to that file object.

FileReader ReadFile = new FileReader(FromFile);

Again we create a new object, this time of the type FileReader, call it ReadFile and initialise it with our file object FromFile.

Now we are going to read the file character by character. What we are actually reading is a sequence of integer numbers. Computers are only capable of dealing with 1's and 0's, in computer language there is no such thing as a letter. At the start of the computer age people have agreed that certain numbers correspond with certain letters. For example 65 corresponds to the letter A. Computers are totally unaware of this, to them it's just a series of 1's and 0's. So to read the file we need an integer object to put each character in.

int ReadChar = 0;

Full descriptions of the objects:

File
java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

FileReader
java.sun.com/j2se/1.4.2/docs/api/java/io/FileReader.html



The full program becomes this:


import java.io.*;

public class Lesson7a {
public static void main(String[ ] args) throws Exception {
    File FromFile = new File("in-file.txt");
    FileReader ReadFile = new FileReader(FromFile);
    int ReadChar = 0;

    while ((ReadChar = ReadFile.read()) != -1)
    {
      System.out.println(ReadChar);
    }

    ReadFile.close();
}
}


Before you run this program remember that you must do this with an existing file ! So if it does not exist yet create it first ! I will be using this file consisting of 5 lines as show.

File: in-file.txt

ABC
DEF
abc
123
This is the last line in in-file.txt


This program will show a series of numbers that represent the characters, ending like this:

……..
46 . ( dot )
116 t ( letter t )
120 x ( letter x )
116 t ( letter t )
13 ( invisible character for a carrige return )
10 ( invisible character for a line feed )

If you use the write() method from the System object in stead of the println() method you will see the characters in the file as letters. This is because the write() method will interprete the numerical values back to characters first and then display them.


System.out.println(ReadChar); // to show the characters as their numerical value

System.out.write(ReadChar); // to show the characters as actual letters



So line by line:
import java.io.*;
import the library of classes containing the file objects


public class Lesson7a {
public static void main(String[ ] arguments) throws Exception {

create our program object


You will note here that the words throws Exception have been added compared to previous programs. This is part of JAVA's error handling system. As you run programs many errors can occur. The compiler JAVAC does not know this beforehand. Some possible errors are considered so severe that they must be handled if they occur. Therefore the compiler checks if such severe errors are possible and will only compile if error handling is in place. The words throws Exception simply mean that if an error occurs, and in this case that would most likely be a "File does not exist" error, the program throws an exception error. This thrown exception error can be picked up by an other program part, in such a way that the program does not stop executing because of the error.

To throw the exception is required. But actually having error handling in place is up to you, the programmer. We will see more about error handling later in the course.


File FromFile = new File("in-file.txt");
Create a new File object to identify the actual file we want to read

FileReader ReadFile = new FileReader(FromFile);
Create a new FileReader object to read from the file.

int ReadChar = 0;
Declare an integer to read character per character from the file

while ((ReadChar = ReadFile.read()) != -1)
While another character was read from the file

The part ( ReadChar = ReadFile.read() ) uses the read() method in the FileReader object to read the next character from the file. If we are at the end of the file and there is nothing more to read this method will return -1. Therefore we put this as a condition for the while loop. So we read characters from the file until we reach the end of the file. After reading the end of the file we close the stream from that file with the close() method.

{
System.out.println(ReadChar);

Write the each character to the screen
Use write() in stead of println() to view the characters as letters
}

ReadFile.close();

When the last character was read and there is nothing more to read close the read-stream.

}
}




7.2 Writing to a file

The other way around is writing to a file. Again we create a File object first and then a stream to that object with FileWriter. Inside FileWriter we can use the write() method to write characters to our file. This program copies the contents of in-file.txt to out-file.txt . If out-file.txt already exists it will be overwritten !


import java.io.*;

public class Lesson7b {
public static void main(String[ ] arguments) throws Exception {
    File FromFile = new File("in-file.txt");
    FileReader ReadFile = new FileReader(FromFile);

    File ToFile = new File("out-file.txt");
    FileWriter WriteFile = new FileWriter(ToFile);

    int ReadChar = 0;

    while ((ReadChar = ReadFile.read()) != -1)
    {
      WriteFile.write(ReadChar);
    }

    ReadFile.close();
    WriteFile.close();
}
}


This does the trick reading from and writing to files but there are 2 disadvantages.

One is that we need to do it character by character. Not really usefull when dealing with text and numbers. It takes a lot of IF’s and WHILE’s to filter meaningfull information.

The second drawback is that for each character Java actually needs to access the file on the harddrive of the computer. That is something that takes time compared to using memory. You will not notice this on small files but as your files get larger it does make a difference. To make things more efficient we can use buffering.

Full object descriptions:

File
java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

FileWriter
java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html




7.3 Buffering streams

Buffering is the technique of doing actions in a memory buffer, combining individual actions to grouped actions and thus being more efficient. This for example limits the time it takes to access the hard-drive to a minimal thus saving time overall because accessing memory goes a lot faster. There are 2 objects we can use:

BufferedReader
java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html

BufferedWriter
java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedWriter.html

BufferedReader will execute any underlying read actions in memory as a group of actions. This is more efficient than doing each read action on its own. Its more efficient to read a series of characters until end-of-line is found that to read character by character checking each individual character. This also makes an number of extra methods possible such as readLine() which reads a line from the file instead of character by character.

The same for BufferedWriter, it writes into a buffer in memory. That is a lot faster than accessing the hard-disk character by character. When we are done writing to the buffer it then writes the entire buffer to the hard-drive in one go, saving us quite some disk-accessing time. There is a small danger here and that is that when something happens, like a power failure, before the buffer was written to the hard-drive then there will be no file on the disk and the information will be lost. In case of sensitive info the BufferedWriter object has a flush() method forcing it to write the buffer at that moment to the hard-disk securing the info.

We will only use Buffering for files but it is not limited to files. For a full overview of input and output read the tutorial on I/O: java.sun.com/docs/books/tutorial/essential/io/index.html

To wrap reading a file in a buffer we do this:
BufferedReader inputfilebuffer = new BufferedReader( FileReader object );

We can then read from the file using the read() or readLine() method like this:

int ReadChar = 0;
ReadChar = inputfilebuffer.read();

String ReadLine = "";
ReadLine = inputfilebuffer.readLine();


So to setup reading a file we now have 3 stages:

File FromFile = new File("in-file.txt");
FileReader ReadFile = new FileReader(FromFile);
BufferedReader inputfilebuffer = new BufferedReader(ReadFile);

After this we can access the file using the inputfilebuffer name

And to setup writing to a file we now have:

File ToFile = new File("out-file.txt");
FileWriter WriteFile = new FileWriter(ToFile);
BufferedWriter outputfilebuffer = new BufferedWriter(WriteFile);

After this we can write to the file using the outputfilebuffer name




And so our full program to copy a file becomes this:


import java.io.*;

public class Lesson7c {
public static void main(String[ ] arguments) throws Exception {
    /* create a file object of the file to read
    * create a FileReader object
    * create a BufferedReader object
    * we can access the file with the inputfilebuffer object
    */
    File FromFile = new File("in-file.txt");
    FileReader ReadFile = new FileReader(FromFile);
    BufferedReader inputfilebuffer = new BufferedReader(ReadFile);

    /* create a file object of the file to write
    * create a FileWriter object
    * create a BufferedWriter object
    * we can access the file with the outputfilebuffer object
    */
    File ToFile = new File("out-file.txt");
    FileWriter WriteFile = new FileWriter(ToFile);
    BufferedWriter outputfilebuffer = new BufferedWriter(WriteFile);

    // a String to hold a single line
    String ReadLine = "";

    // loop as long as there are lines to read from the input file
    while ((ReadLine = inputfilebuffer.readLine()) != null)
    {
      // write the line to the output file
      outputfilebuffer.write(ReadLine);
      outputfilebuffer.newLine();
    }

    // when all lines have been read and written close the buffer
    // this will automatically flush the Writer buffer to the hard-disk and close the file
    inputfilebuffer.close();
    outputfilebuffer.close();
}
}


Remember, we have not yet seen any error handling so when you do this make sure the file you wish to read exists and remember the file you write to will be overwritten.



A couple of points to note here.

First there is a change in the line with the while statement. It now checks for null in stead of -1 .
This is because we are no longer reading numerical values, we are actually reading whole lines. So if there is no more line to read the readLine() method returns null to indicate end of file.

while ((ReadLine = inputfilebuffer.readLine()) != null)




Then for beginners i would not advise it but you can combine creating the file objects in one line.

File FromFile = new File("in-file.txt");
FileReader ReadFile = new FileReader(FromFile);
BufferedReader inputfilebuffer = new BufferedReader(ReadFile);


These 3 lines can also be written like this:

BufferedReader infile = new BufferedReader(new FileReader(new File("in-file.txt")));

What about the File and FileReader objects you may ask ? Well JAVA simply takes care of that in the background. If we write it as 3 lines we name the objects ourselves, written as one line then the JAVAC compiler will assign a name to the objects. If so we can of course no longer access those objects directly. It depends on the type of applications you are writing which one to use. We will probably only use the one line version in the remainder of the course.



Also note an extra line when writing to our output file. The method write() in the Bufferobject will write a line to the file BUT it does not write a carrige return or line feed character. This we need to do ourselves with the newLine() method. If we do not use that method to create a new line then all the lines will be written on one single long line. Just try it out and see the result.

outputfilebuffer.write(ReadLine);
outputfilebuffer.newLine();



So far when we write a file then if it already exists it get overwritten. There is a way to continually append to a file. This is done by setting an append flag when we create the FileWriter object like this:

boolean add = true;

File ToFile = new File("out-file.txt");
FileWriter WriteFile = new FileWriter(ToFile ,add );
BufferedWriter outputfilebuffer = new BufferedWriter(WriteFile);

In this case each time we write to the file and it already exists, the info is appended at the end of the file.




Assignment 7.1 ( Lesson71.java )

Write a program that reads a file and counts the number of upper case letters, lower case letters, single digit characters and the remaining number of characters.
The ranges of numerical values are this:

0 - 9 --- 48 - 57
A - Z --- 65 - 90
a - z --- 97 - 122

This is the result you should get when you run the program on this example file.

File: in-file.txt

ABC
DEF
abc
123
This is the last line in in-file.txt


Digits : 3
Upper case : 7
Lower case : 30
Rest : 18

Note that the rest of the characters are the 6 spaces, the dash and dot in in-file.txt and the invisible carrige return and line feed characters on each of the 5 lines. So 18 in total.



Assignment 7.2 ( Lesson72.java )

Write a program that copies a file but whilst doing so it changes all the upper case characters into lower case characters and vice versa, change lower case into upper case. Use a direct method, read a character, check it, switch it, write the character.


Assignment 7.3 ( Lesson73.java )

The same program as above, copy a file and switch upper/lower case characters. This time do it using a buffer to read the file, make the change to upper/lower case, then write the file using a buffer.

Once you have Lesson72 and Lesson73 give this a try with a very large file. See if you can measure the difference in time it takes. I copied in-file.txt until it reached 3 Megabytes, Lesson72 without buffering took 15 seconds, Lesson73 with buffering took only 3 seconds.
Datapunter is offline  
Old 22-06-2004, 03:04   #2 (permalink)
Pro Punter
 
Datapunter's Avatar
 
Join Date: 23 Oct 2003
Location: Westdorpe
Age: 43
Posts: 5,365
Default Re: Lesson 7 - Language, reading and writing files

Assignment solutions :

Assignment 7.1 Lesson71.java

Assignment 7.2 Lesson72.java

Assignment 7.3 Lesson73.java

Click on the filename to open, or use the right-click and then Save Target As... to save the file on your PC.
Datapunter is offline  
Old 29-10-2004, 16:01   #3 (permalink)
Pro Punter
 
Datapunter's Avatar
 
Join Date: 23 Oct 2003
Location: Westdorpe
Age: 43
Posts: 5,365
Default Re: Lesson 7 - Language, reading and writing files

empty post, done to re-order all the sticky topics.
Datapunter is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts



All times are GMT. The time now is 04:48.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

Free £100 Bet!
Online Bookmakers
Free £100 Bet!

Recommended Bookies: Bet365 | BetDirect | | Blue Square | Canbet | Centrebet | Coral | Eurobet | Ladbrokes | Paddy Power | Party Bets | Pinnacle Sports | Skybet | SportingBet | Stan James | ToteSport | VCBet

Recommended Betting Exchanges: | Betfair | WBX

Recommended for Spread Betting: Sporting Index |
Partner Sites
Football Betting Tips Free Bets Australian Free Bets HOT Odds Comparison Soccer Punter
Bookmakers Livescore SoccerVista Asian Handicap Betting Guide

Contact Us | Disclaimer


© 2008 PuntersLounge.Com Ltd | Gambling Problems?

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.