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:

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

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
 * www.sanjaal.com/java
 *
 * Demonstrates the following Java features
 * --Reading a File in Java
 * --Simple String manipulations
 * --Writing to a File in Java
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class FormInStringForSQL {

	public static void main(String args [])
	{
		try{
			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+"', ";
				else
					formattedStringForInClause+="n '"+line+"', ";
				count++;
			}

			/**Removing the trailing final comma (')**/
			if(formattedStringForInClause.endsWith(", "))
				formattedStringForInClause=
                                formattedStringForInClause.substring(
				0,formattedStringForInClause.length()-2);

			/**Writing the formatted string to file**/
			BufferedWriter out = new BufferedWriter(new FileWriter(outputFileName));
	        out.write(formattedStringForInClause);
	        out.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}

  1. Adding a MySQL Remote User / Accessing MySQL Database Remotely [Simple Solution]
  2. How to extract plain Text from HTML Website easily in Java
  3. How to read / write UTF8 and Non-UTF8 files in Java
  4. How To Print A Text File In Java
  5. Inserting Values From Text File To MySQL
  6. How to read a tab separated or tab delimited file in Java program and print the content to console
  7. How To Find What Java Version You Are Using?
  8. Computing the total, free and usable disk space easily using JDK 1.6
  9. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  10. Creating java.util.date and java.util.sql objects from Calendar object
  11. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  12. Bar Code Generation In Java Using iText API
  13. Simple Tag Generator In Java For Music Blogs
  14. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  15. Calling URL Browser From Java Application
  16. Java Reflection Tutorial – List Methods Of A Class
  17. Cleaning Punctuation And White Spaces From A String
  18. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  19. Prime Number Finder In Java
  20. Utility Class For Getting SQL Connection and Closing It
  21. Java Design Pattern – Singleton Pattern
  22. Reading / Writing File in Java and String Manipulation
  23. How to capture a screenshot using Java and save it to a file?
  24. Calculating The Difference Between Two Dates In Terms of Months
  25. Getting The Name Of Currently Logged In User In The System
  26. How To Set And Get System Clipboard Contents In Java
  27. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  28. Generating Nepali Unicode Sequence For Characters
  29. How To Read DOC file Using Java and Apache POI
  30. Writing To Excel File Using Apache POI
  31. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  32. Replacing non-ASCII characters using Java Regular Expressions
  33. Reading Excel File Using Java And Apache POI
  34. Calculating Folder Size In Java
  35. How To Rename A File In Java
  36. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

4 Responses to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

  2. d says:

    would that help you tocreate the file from ubunt server

  3. komal gaikwad says:

    in this code i got problem of array out of bound plz tell me solution.

  4. komal gaikwad says:

    In this code i got error
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    plz send me proper code….

Leave a Reply

Your email address will not be published. Required fields are marked *