JLAN | Tutorial |
The following tutorials will take you through the basics of the JLAN package.
Tutorial 1 - lmDir |
import com.starla.lite.*;
import java.io.*;
/**
* lmDir Application
*/
class lmDir {
/**
* Main
*
* @param args Command
line arguments array
*/
public static final void
main ( String [] args) {
}
}
// Check if we have enough
command line parameters
if ( args.length == 0) {
System.out.println ( "Usage:
lmDir \\\\<node>\\<share>"
+ "[%<username>:<password>"
+ "[\\<path>\\<file>");
return;
}
try {
}
catch ( com.starla.lite.InvalidUNCPathException
ex) {
System.out.println ( ex);
}
catch ( com.starla.lite.SMBException
ex) {
System.out.println ( ex);
}
catch ( java.io.IOException ex) {
System.out.println ( ex);
}
We will use the constructor that takes a UNC
path string to create our PCShare object. If the path specified on the
command line is not a valid UNC path a com.starla.lite.InvalidUNCPathException
will be thrown.
The user may not have specified a file name, or wildcard file spec on the command line. We will add a check for an empty file name string after the PCShare object has been created and the UNC path has been parsed.
Add the following code inside the try {} block.
// Parse the network path
PCShare remPath = new PCShare ( args [ 0]);
// Check if a file name has been specified
if ( remPath.hasFileName () == false)
remPath.setFileName ( "*.*");
// Open a session to the remote
file server
SMBDiskSession sess = SMBSessionFactory.OpenDisk
( remPath);
The StartSeach () method returns an SMBSearchContext
object which is used to return information for each file found within the
search.
// Start a search of the remote directory
SMBSearchContext srch = sess.StartSearch
( remPath.getRelativePath (),
SMBFileAttribute.Directory);
We will use the nextFileInfo () method to return
the full file information. When the end of the search has been reached
a null object will be returned. The SMBFileInfo object has a toString ()
method which displays the file name, file attributes, file size and file
date/time. We will use the default output method to display the file list.
// Get a file from the search
SMBFileInfo finfo = srch.nextFileInfo ();
while ( finfo != null) {
// Output the current file information
System.out.println ( finfo);
// Get the next file from the search
finfo = srch.nextFileInfo
();
}
// Close the session to the server
sess.CloseSession ();
java lmDir \\test\c\*.*
The remote path may be specified using forward slash characters, '/', to prevent having to quote the back slash characters under Unix shells, ie. the above path may also be specified as //test/c/*.*;
Tutorial 2 - lmType |
import com.starla.lite.*;
import java.io.*;
/**
* lmType Application
*/
class lmType {
/**
* Main
*
* @param args Command
line arguments array
*/
public static final void main ( String [] args) {
}
}
// Check if we have enough
command line parameters
if ( args.length == 0) {
System.out.println ( "Usage:
lmType \\\\<node>\\<share>"
+ "[%<username>:<password>"
+ "[\\<path>\\]<file>");
return;
}
try {
// Parse the network path
PCShare remPath = new PCShare ( args [ 0]);
// Open a session to the remote disk share
SMBDiskSession sess = SMBSessionFactory.OpenDisk
( remPath);
We will use the SMBDiskSession OpenInputStream
() method to open the remote file for read access. The OpenInputStream
() method requires two parameters, the remote file name/path specified
relative to the root of the share we are connected to. The PCShare getRelativePath
() method provides this string for us. Secondly the access mode for the
file open must be specified, the possible values are contained in the SMBAccessMode
class. We will open the remote file for read-only access.
// Open the remote file as an input stream
InputStream inStr = sess.OpenInputStream
( remPath.getRelativePath (),
SMBAccessMode.ReadOnly);
// Create a line number reader
to read from the remote file
InputStreamReader strReader = new InputStreamReader
( inStr);
LineNumberReader lnReader = new LineNumberReader
( strReader);
String inRec = null;
while (( inRec = lnReader.readLine ()) != null) {
// Output the current record number and record
System.out.println ( lnReader.getLineNumber
() + ": " + inRec);
}
// Close the input stream
inStr.close ();
java lmType \\test\c\testfile.dat
The remote path may be specified using forward slash characters, '/', to prevent having to quote the back slash characters under Unix shells, ie. the above path may also be specified as //test/c/testfile.dat;