Console I/O in Java 2

Files and File I/O:-

  • The Java technology includes a rich set of I/O streams which are described in the previous section.
  • In this it examines several simple technologies for reading and writing to files with a focus on the character data, including:
  1. Creating File Objects
  2. Manipulating File Objects
  3. Reading and Writing to file streams

Creating a New File Object:-

  • The File class provides several utilities for handling files and obtaining information about them.
  • In java technology, a directory is just another file. We can create a File object that represents a directory and then use it to the identify other files, as shown in 3rd bullet.
  1. File myFile;

myFile = new File(“myfile234.txt”);

  1. myFile = new File(“MyDocs”,”myfile234.txt”);
  2. File myDir = new File(“MyDocs”);

myFile = new File(“myDir”,”myfile234.txt”);

  • The constructor that you often depends on the other file objects that you can access.
  • For example, if you use only one file in your application, use the first constructor.
  • However, if you use several files from a common directory, using the second or third constructors may be easier.
  • The class File does define platform independent methods for manipulating a file maintained by a native file system.
  • However, it does not permit you to access the contents of the file.

Note: – you can use a File object as the constructor arguments for FileReader and FileWriter objects in place of a string. This gives you independence from the local file system conventions and is recommended in general.

The File Tests and Utilities:-

  • After you create a File object, you can use any methods in the following sections to gather information about the file.


File Names:-

The following method does return file names:

  1. String getName()
  2. String getPath()
  3. String getAbsolutePath()
  4. String getParent()
  5. String renameTo(File newName)


Directory Utilities:-

The following method does provide directory utilities:

  1. boolean mkdir()
  2. String [] list()

General File information and Utilities:-

The following methods return general file information:

  1. long longModified()
  2. long length()
  3. boolean delete()

Console I/O in JAVA 

File Tests:-

The following methods return information about file attributes:

  1. boolean exists()
  2. boolean canWrite()
  3. boolean canRead()
  4. boolean isFile()
  5. boolean isDirectory()
  6. boolean isAbsolute()
  7. boolean isHidden()

File Streams I/O:-

The java SE development Kit supports file input in two forms:

  • Use a FileReader class to read characters
  • Use a BufferedReader class to use the readLine method

The java SE development Kit supports file output in two forms:

  • Use a FileWriter class to write characters
  • Use a PrintWriter class to use the print and println methods

File Input Example:-

In thhe code given below reads a text file and echoes each line to standard output, which prints the file.

Reading from a File

  1. import*;
  2. public class ReadFile
  3. {
  4. public static void main(String[] args)
  5. {
  6. // create file
  7. File file = new File(args[0]);
  8. try{
  9. // create a buffered reader
  10. // to read each line from a file.
  11. BufferedReader in = new BufferedReader(new FileReader(file));
  12. String s;
  13. try{
  14. // read each line from the file
  15. s = in.readLine();
  16. while(s!=null)
  17. {
  18. System.out.println(“Read: ”+s);
  19. s = in.readLine();
  20. }
  21. }finally{
  22. // close the buffered reader
  23. in.close();
  24. }
  25. }catch(FileNotFoundException e1){
  26. // if this file does not exist
  27. System.err.println(“File not found: ”+file);
  28. }catch(IOException e2){
  29. // catch any other IO Exceptions.
  30. e2.printStackTrace();
  31. }
  32. }
  33. }
  34. }
  • Line 7 creates a new File object based on the first command line argument to the program.
  • Line 11 creates a buffered reader that wraps around a file reader. This code throws a FileNotFoundException if the file does not exist.
  • The code after line 13 reads a text file and echoes each line to standard output, thus printing the file.
  • In a given code, the while loop in Lines 16-20 is exactly the same as in the KeyboardInput program, it reads each text line in the buffered reader and echoes it to standard output.
  • Line 23 closes the buffered reader, which in turn closes the file reader that the buffered reader object decorates.
  • The exception handling code in Line 25-28 catches the FileNotFoundException that may be thrown by the FileReader constructor.
  • Lines 28-31 handle any other I/O based exception that may be thrown (by the readLine and close methods).

File Output Example:-

The cod egiven below reads input lines from the keyboard and echoes each line to a file.

File Output Example

  1. import*;
  2. public class WriteFile
  3. {
  4. public static void main(String[] args)
  5. {
  6. // create file
  7. File file = new File(args[0]);
  8. try{
  9. // create a buffered reader
  10. InputStreamReader isn = new InputStreamReader(;
  11. BufferedReader in = new BufferedReader(isr);
  12. // create a print writer on this file.
  13. PrintWriter out = new PrintWriter(new FileWriter(file));
  14. String s;
  15. System.out.println(“Enter file text. ”);
  16. System.out.println(“[type ctrl-d to stop.]”);
  17. // read each input line
  18. s = in.readLine();
  19. while(s!=null)
  20. {
  21. out.println(s);
  22. }
  23. // close the buffered reader
  24. in.close();
  25. out.close();
  26. }catch(IOException e){
  27. // catch any other IO Exceptions.
  28. e2.printStackTrace();
  29. }
  30. }
  31. }
  32. }
  • Just as in code before, line 8creates a File object based on the first command line argument.
  • Line 12 creates a character reader stream from the binary stream (
  • Line 13 creates a buffered reader for the standard input. Line 15 creates a print writer that decorates a file writer for the created in Line 8.
  • Lines 18-19 prompt the user to enter lines of text to be placed in the file and to type control-d stop.
  • The Lines 23-26 read from the input stream and prints to the file, one line at a time.

Note: – the control-d character (which represents end of file) must be used in this example and not control-c, because control-c terminates the JVM before the program closes the file stream properly.

  • Line 28 and line 29 close the input and output streams. Line 30-33 handles any I/O exceptions that may be thrown.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc.

Leave a Reply