Java Tutorial – Using JCIFS to copy files to shared network drive using username and password

This is a quick and dirty example of how you can copy the files to shared network drive. We have tested this under Microsoft Windows, Mac and Linux platforms. To do the actual file copy, we have used an readily available and highly popular API called JCIFS. JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java. CIFS is the standard file sharing protocol on the Microsoft Windows platform. Visit their website at: if you want to download this API.

As you can see in the sample program below, it allows you to even connect to protected network shares using userid and password.


 * @author Kushal Paudyal
 * Create on 2012-10-12
 * Last Modified On 2012-10-12
 * JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java.
 * CIFS is the standard file sharing protocol on the Microsoft Windows platform
 * Visit their website at:
 * Uses: jcifs-1.1.11.jar
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

public class NetworkShareFileCopy {
	static final String USER_NAME = "admin";
    static final String PASSWORD = "password";
    //e.g. Assuming your network folder is: \my.myserver.netsharedpublicphotos
    static final String NETWORK_FOLDER = "smb://";

	public static void main(String args[]) {
		String fileContent = "This is a test file";

		new NetworkShareFileCopy().copyFiles(fileContent, "testFile.text");

	public boolean copyFiles(String fileContent, String fileName) {
		boolean successful = false;
	            String user = USER_NAME + ":" + PASSWORD;
	            System.out.println("User: " + user);

	            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
	            String path = NETWORK_FOLDER + fileName;
	            System.out.println("Path: " +path);

	            SmbFile sFile = new SmbFile(path, auth);
	            SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);

	            successful = true;
	            System.out.println("Successful" + successful);
	        } catch (Exception e) {
	            successful = false;
		return successful;

Reading / Writing File in Java and String Manipulation

I wrote this piece of java code because I encountered a situation where I had a list of some strings from some table in database and needed to create an in clause. Since the number of items was huge, it was not possible to manually add quote the string and add a comma.

Here was my requirement.

I had string in the following format:


And I needed the string formatted something like the following so that I could put that in ‘in clause’ in one of the SQL.

‘14685’,  ‘14701’,  ‘14707’,  ‘14720’,  ‘14738’,  ‘14741’,  ‘14743’,  ‘14745’,  ‘14747’,  ‘14748’,
‘14749’,  ‘14750’,  ‘14751’,  ‘14761’,  ‘14764’,  ‘14767’,  ‘14768’,  ‘14771’,  ‘14775’,  ‘14776’

So, to save my time doing the impossible manual change (I had around 2000 of these items), I wrote this simple program. This program features File Read, File Write operation in Java and also shows simple Java String manipulation techniques.

package com.kushal.sql.utilities;
 * @author Kushal Paudyal
 * Created on 10/02/2008
 * Demonstrates the following Java features
 * --Reading a File in Java
 * --Simple String manipulations
 * --Writing to a File in Java

public class FormInStringForSQL {

	public static void main(String args [])
			String inputFileName="C:/items_that_were_marked.txt";
			String outputFileName="C:/myoutputfile.txt";

			BufferedReader in = new BufferedReader(new FileReader(inputFileName));
			String line="";
			int count=0;
			String formattedStringForInClause="";

			/**Read the file and do the necessary formatting**/
			while ((line = in.readLine()) != null)
				if(count%10>0) /**Formatting to 10 columns**/
					formattedStringForInClause+=" '"+line+"', ";
					formattedStringForInClause+="n '"+line+"', ";

			/**Removing the trailing final comma (')**/
			if(formattedStringForInClause.endsWith(", "))

			/**Writing the formatted string to file**/
			BufferedWriter out = new BufferedWriter(new FileWriter(outputFileName));
		catch (Exception e)


How To Read DOC file Using Java and Apache POI

One of the visitors of my blog asked me write how to read a document file using Java. I wrote the following program to demonstrate how Apache POI can be used for this purpose.

I have used the following API to write this program. If you have downloaded the Apache POI, you should fine this jar file within the bundle.

  • poi-scratchpad-3.2-FINAL-20081019.jar

The tutorial demonstrates the following features:

–How to read a simple Microsoft word document file using Java and Apache POI (.docx not supported)
–This includes the ability to read total number of paragraph and the paragraph content
–How to read the document headers
–How to read the document footers
–How to read the document summary

Apache POI is not robust yet. It has a long way to go through to handle complex document formats. Moreover I figured out that from one version to another, the classes are moving from one package to another. So if you are using the older/newer version of POI, in case of any compilation error for imports, try finding the classes in some other packages.

 * @author Kushal Paudyal
 * Last Modified On: 03/23/2009
package com.kushal.utils;

import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hwpf.*;
import org.apache.poi.hwpf.extractor.*;
import org.apache.poi.hwpf.usermodel.HeaderStories;


public class ReadDocFileFromJava {

	public static void main(String[] args) {
		/**This is the document that you want to read using Java.**/
		String fileName = "C:\Documents and Settings\kushalp\Desktop\Test.doc";

		/**Method call to read the document (demonstrate some useage of POI)**/

	public static void readMyDocument(String fileName){
		POIFSFileSystem fs = null;
		try {
			fs = new POIFSFileSystem(new FileInputStream(fileName));
			HWPFDocument doc = new HWPFDocument(fs);

			/** Read the content **/

			int pageNumber=1;

			/** We will try reading the header for page 1**/
			readHeader(doc, pageNumber);

			/** Let's try reading the footer for page 1**/
			readFooter(doc, pageNumber);

			/** Read the document summary**/

		} catch (Exception e) {

	public static void readParagraphs(HWPFDocument doc) throws Exception{
		WordExtractor we = new WordExtractor(doc);

		/**Get the total number of paragraphs**/
		String[] paragraphs = we.getParagraphText();
		System.out.println("Total Paragraphs: "+paragraphs.length);

		for (int i = 0; i < paragraphs.length; i++) {

			System.out.println("Length of paragraph "+(i +1)+": "+ paragraphs[i].length());



	public static void readHeader(HWPFDocument doc, int pageNumber){
		HeaderStories headerStore = new HeaderStories( doc);
		String header = headerStore.getHeader(pageNumber);
		System.out.println("Header Is: "+header);


	public static void readFooter(HWPFDocument doc, int pageNumber){
		HeaderStories headerStore = new HeaderStories( doc);
		String footer = headerStore.getFooter(pageNumber);
		System.out.println("Footer Is: "+footer);


	public static void readDocumentSummary(HWPFDocument doc) {
		DocumentSummaryInformation summaryInfo=doc.getDocumentSummaryInformation();
		String category = summaryInfo.getCategory();
		String company = summaryInfo.getCompany();
		int lineCount=summaryInfo.getLineCount();
		int sectionCount=summaryInfo.getSectionCount();
		int slideCount=summaryInfo.getSlideCount();

		System.out.println("Category: "+category);
		System.out.println("Company: "+company);
		System.out.println("Line Count: "+lineCount);
		System.out.println("Section Count: "+sectionCount);
		System.out.println("Slide Count: "+slideCount);