Difference between revisions of "IO"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 42: Line 42:
 
}
 
}
  
 +
</syntaxhighlight>
  
</syntaxhighlight>
+
== Console ==
 +
 
 +
* A console is a unique character based device associated with a JVM. Represented by java.io.Console.
 +
* Whether a JVM has a console or not depends on the platform and the manner in which a JVM is invoked.
 +
* For a JVM started on the cmd line, the console corresponds to the keyboard and the display monitor.
 +
* Console console = System.console();
  
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 07:45, 18 August 2011

Basic Classes

  • The File class is an abstract representation of files and directory pathnames. It works at a higher level, cant be used to read/write data.
  • FileReader - read character levels - variety of read() methods. Usually wrapped in a BufferedReader.
  • BufferedReader - Reads large chunks of files and keeps it in the buffer. Subsequent requests are got from the buffer thus minimizing the number of times time intensive file IO is performed.

Similarly:

  • FileWriter - write to character files.
  • BufferedWriter - Keeps data in buffer and writes large chunks of buffer to file at once, minimizing file IO.
  • PrintWriter - Has a lot of convenience methods and constructors (takes a File or String etc). Methods like format(), printf() and append() make PrintWriters powerful.

Remember:

  • For Characters - Reader and Writer classes are used.
  • For Bytes - Stream classes are used.

File Class

  • A File object can represent a file or a directory.
  • File f = new File("a.txt") just creates an object representing the file, doesnt create the file on the disk.
  • createNewFile() - will attempt to create a file. Returns true/false depending on the success status.
  • Creating a reader/writer/stream will also automatically create a file if it doesnt exist.
  • mkdir() - will create a directory.
  • list() - returns a String[] of directory contents if the File represents a dir, else it will return null.
  • Deleting is done for both files and directories using the delete() method.
  • Directory which is not empty CANNOT be deleted. delete() will just return false.
  • renameTo() works for both files and directories. Non-empty directory can be renamed too.
  • Paths:
public static void main(String[] args) {
		try {
			//Current path : /home/suhridk/eclipsews/OCPJP/bin
			File f = new File("../somefile.txt");
			System.out.println(f.getPath());          // prints: ../somefile.txt
			System.out.println(f.getAbsolutePath());  // Path from the begining : prints: /home/suhridk/eclipsews/OCPJP/../somefile.txt
			System.out.println(f.getCanonicalPath()); // All relative references resolved: prints: /home/suhridk/eclipsews/somefile.txt
		} catch (Exception e) {
			e.printStackTrace();
		}
}

Console

  • A console is a unique character based device associated with a JVM. Represented by java.io.Console.
  • Whether a JVM has a console or not depends on the platform and the manner in which a JVM is invoked.
  • For a JVM started on the cmd line, the console corresponds to the keyboard and the display monitor.
  • Console console = System.console();