WebNFS Developer's Guide

File Interface Examples

Extending existing programs to network filesystems should be straightforward using the extensions provided. You simply replace declarations of type java.io.FileXYZ with the counterpart java.io.XFileXYZ. Here are a few simple examples of applications that use the APIs.


Example 2-1 Using Streams Interface

                import java.io.*;

                import java.net.*;

                import com.sun.xfile.*;


                XFile xf = new XFile("file.txt");

                if (xf.isFile()) {

                    System.out.println("file is file");

                }

                if (xf.isDirectory()) {

                    System.out.println("file is directory");

                }

                XFileInputStream nfis = null;

                nfis = new XFileInputStream(xf);

                for (int count = 0; ; count++) {

                    int val = (byte) nfis.read();

                    if (val == -1)

                        break;

                    System.out.write(val);

                }

                System.out.println("read " + count + " bytes ");


Example 2-2 Using XRandomAccessFile Interface

                import java.io.*;

                import java.net.*;

                import com.sun.xfile.*;

                // create connection to host

                XFile xf = new XFile("nfs://ian/simple.html");

                XRandomAccessFile xraf = new XRandomAccessFile(xf,"r");

                int count = 10;

                xraf.seek(count);

                System.out.println("Value at position " + count

                    + " is " + (byte)xraf.read());

                System.out.println("Current file position is "

                    + xraf.getFilePointer());

                xraf.seek(0);

                for (count = 0 ; count < 100 ; count++) {

                    int val = xraf.read();

                    if (val == -1)

                        break;

                    System.out.print(val);

                }