Showing posts with label Text File. Show all posts
Showing posts with label Text File. Show all posts

Monday, June 11, 2012

File class

The Scanner & PrintWriter classes can read from & write to text files, respectively.

Each of these classes contains a constructor that accepts a File object.

The File class belongs to the java.io library (io stands for Input Output).

The example below declares a File object that references the io library directly. Notice that a String containing a filepath/filename is passed to the file's constructor. This File object called file, is then passed to the constructor of the Scanner object. In essence we are saying, read from this file at this location.


The next example declares a File object using a shorter declaration. The library reference has been done via an imports statement at the top of the code (not shown). Once again a String filepath/filename has been passed to the constructor. The file object is then passed to the constructor of the PrintWriter object. In short, we are saying, write (print) to this file at this location.


"File (Java)" ( http://bit.ly/JWg0sp )

"java.io (Java Platform SE 6)" ( http://bit.ly/JWhUcw )

Monday, May 28, 2012

PrintWriter


DOWNLOAD THE CODE: PrintToFile.java

The PrintWriter class contains 2 very useful methods:
  • print(String)
  • println(String)
These methods write a String to a file.
The println method adds the end-of-line character after the string.

The constructor of the PrintWriter class accepts a file to write to. As such, a File object has been declared. The constructor of the File object accepts a String file path of the file's location. It is interesting to note that if the file has not already been created, the PrintWriter will create it automatically.

The example below contains 2 loops. The first loop writes the numbers 1-10 (using the loop counter) to data.txt using the print() method. As such, each number is written immediately after the last. The second loop also writes the numbers 1-10 to data.txt but uses the println() method so that each number is written on a new line.


Here is the output:




Throws FileNotFoundException. If you do not implement exception handling for this exception, the compiler will force you to rethrow it.

And if you want to read text file from a file, here's a post about the Scanner class.

"PrintWriter (Java Platform SE 7 )" ( http://bit.ly/LsOVki )

Here's a cat:


Sunday, March 18, 2012

Scanner class - Part 1

DOWNLOAD THE CODE: Scanner.zip

The Scanner class has methods for reading a number of different types of data such as String, int, Double & Boolean.

By default, the Scanner class uses whitespace to delineate tokens (lines, words, phrases, values, etc).

We can use the Scanner class to read input from keyboard input or from txt files.

Use Scanner to read keyboard input
The example below uses the Scanner class to read keyboard input. Scanner contains several methods for reading different types of data. In this example, the next() & nextInt() methods are used to read the input String and the input int, respectively.


The output:




Using Scanner to read from a text file
The following shoppinglist.txt file contains a number of shopping items. Each item is followed by a number representing the quantity required.


Declare an instance of File and then declare an instance of the Scanner class. Pass the file instance to the scanner instance.

The Scanner class contains a number of Boolean functions to determine whether there is another token of a specified type. The example below uses a while loop in conjunction with the Scanner's hasNext() method of Scanner to determine if another token of any type exists.  In effect, the loop has been primed to 'continue looping while there are more tokens to read'. When there are no more toekn to read, the loop will stop.




The output: 



The final token in this example is the character 1.

By default, the Scanner's next() method is using the whitespace separating the characters in the file, to delineate is string. As such, any string separated by whitespace, is being read in each pass of the loop. The statement input.next() reads the next token in the file.

Let's change the code a little to get these values onto the same line.

The while loop body now looks like this:


The first call to next() reads in the name of the shopping item and is printed to screen via the print() method. The second call to next() is used to read in the quantity is appended to the next line via the println() method. 

The output now looks like this:


Read on to learn about using a nominated deliminator character with the Scanner class.



"Scanner (Java Platform SE 7 )" ( http://bit.ly/ABSuGl )


Simple tutorial using the Scanner class: "Java For Complete Beginners - user input" ( http://bit.ly/Lkr7uN )

"Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)" ( http://bit.ly/LkD03X )

"File (Java Platform SE 7 )" ( http://bit.ly/Jxdc9T )

"The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/LkGjIm )