|
I/O ProgrammingThis page summarizes how Java and other computer languages accept and send data to system files. The java.io package reads and writes data to flat files. Class names containing the word “stream,”, such as the FileInputStream class, perform byte by byte I/O Class names containing the word “reader” or “writer”, such as the FileReader class, perform character by character I/O.
|
|
|
File Input/Output
Typically, only the persistence layer should interact with databases. Persistence is the mechanism by which objects are saved and restored between program executions (typically to a database).
|
Windows File System Object Methods
This code instantiates objects from the Microsoft Windows
FileSystemObject object model
under the Scripting type library in the
Scrrun.dll file within the appropriate system directory on the Web server:
Note that only one instance of the FileSystemObject object is created,
regardless of how many times this code is executed.
|
[JScript] var fso; fso = new ActiveXObject("Scripting.FileSystemObject");
Stream Input/OutputMS dot NET StreamsDisk input > FileStream > Memory Stream > Compression Stream > Crypto Stream > outputusing System; using System.IO; class Class1 { static void Main() { using (TextWriter w = File.CreateText("out.txt"); { w.WriteLine ("Log: <{0}> {1:MM.dd.yy HH:MM}", Environment.UserName, DateTime.Now); } using (TextReader r = File.OpenText ("out.txt"); { Console.WriteLine ( r.ReadLine() ); } } } Java StreamsThis sample coding uses the FileInputStream and FileOutputStream classes from the java.io package to read data from a user-specified file to a buffer, and then out to standard output and a user-specified file.import java.io.*; public class myFileStreamsExample { public static void main (String args[]) throws IOException { File myFile = new File(args[0]); File newFile = new File(args[1]); System.out.println(); if(myFile.exists()) { System.out.println(args[0] + " exists!\n"); FileInputStream inputFile = new FileInputStream(myFile); // Copy entire input file to a buffer: int bytesAvailable = inputFile.available(); byte[] buffer = new byte[bytesAvailable]; int bytes = inputFile.read(buffer); System.out.println("Copying contents to " + args[1]); FileOutputStream outputFile = new FileOutputStream(newFile); outputFile.write(buffer); // writes entire file from buffer. outputFile.close(); // Write input file to Standard System.Output once: System.out.println("inputFile named " + args[0] + " has " + bytesAvailable + " bytes in file" ); while(bytes != -1) { System.out.write(buffer, 0, bytes); // 0 = offset bytes = inputFile.read(buffer); } inputFile.close(); } else { System.out.println(args[0] + " does not exist\n"); } System.out.println(); } } The file input methods:
Blocking occurs when there is no data available for a method to read. It forces the thread in which the method is running to pause and wait for data to become available. In multi-threaded programs, this isn't much to be concerned about, but you can see where it would be a problem in a program with only one thread. ExceptionsIOException is a generic I/O exception—the father of all java.io package exceptions.Others:
|
Basic File Output
Example: gos.write('a');
This can be the method of the outermost class if the source for the stream is chained by code such as this:
FileOutputStream fos = new FileOutputStream("myfile.out");
CryptOutputStream cos = new CryptOutputStream(fos); GZIPOutputStream gos = new GZIPOutputStream(cos); or simply:
GZIPOutputStream gos = new GZIPOutputStream(new CryptOutputStream(new FileOutputStream("myfile.out")));
|
Console Buffered IO
import java.io.*;
public class ReadStringFromConsole {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String strInput = null; try {
System.exit(1); System.out.println("Thank you, " + strInput );
|
Resources |
Java Serializable Sockets Programming
The Serializable Interface has no fields or methods; it serves to inform the compiler about the desire to store/transmit objects of this class. When a class extends a non-serializable class, the fields inherited from the superclass will not be stored or transmitted. Fields marked with modifier key word transient are not stored or transmitted.
PORTS: There are a number of pre-assigned ports. If your Java program attempts to start a server at a port already in use, you will get a BindException. Generally ports under 1024 require administrator permission. Most general purpose Java Servers will listen at port 5000 or greater. The maximum port number allowed currently by TCP/IP is 32767.
|
Externalizable VersioningJava provides the java.io.Externalizable Interface to support versioning. This interface requires the class to write its own Object I/O routines, thus controlling what is read and what is written.
|
Related topics:
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |