26
 SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy_HHmmSS");
 String strCurrDate = formatter.format(new java.util.Date());
 String strfileNm = "Cust_Advice_" + strCurrDate + ".txt";
 String strFileGenLoc = strFileLocation + "/" + strfileNm;
 String strQuery="select name, age, data from basetable";
 try {

     stmt = conn.createStatement();
     System.out.println("Query is -> " + strQuery);
     rs = stmt.executeQuery(strQuery);

     File f = new File(strFileGenLoc);
     OutputStream os = (OutputStream)new FileOutputStream(f);
     String encoding = "UTF8";
     OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
     BufferedWriter bw = new BufferedWriter(osw);

     while (rs.next() ) {

         bw.write(rs.getString(1)==null? "":rs.getString(1));
         bw.write("  ");
         bw.write(rs.getString(2)==null? "":rs.getString(2));
         bw.write("  ");

     }
     bw.flush();
     bw.close();
 } catch (Exception e) {
     System.out.println(
         "Exception occured while getting resultset by the query");
     e.printStackTrace();
 } finally {
     try {
         if (conn != null) {
             System.out.println("Closing the connection" + conn);
             conn.close();
         }
     } catch (SQLException e) {
         System.out.println(
             "Exception occured while closing the connection");
         e.printStackTrace();
      }
    }
         return objArrayListValue;
  }

i need "one tab space" in between each column(while writing to text file). like

   manu 25 data1
   manc 35 data3

in my code i use bw.write(" ") for creating space between each column. how to use "one tab space" in that place instead of giving "space".

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
Manu
  • 3,179
  • 23
  • 57
  • 69

3 Answers3

60

You can use \t to create a tab in a file.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
tmeisenh
  • 1,504
  • 1
  • 13
  • 11
  • 1
    what does "any problem" mean? If you are having a problem and you would like help solving it, please describe the problem. – matt b Apr 06 '10 at 14:09
  • @Manu: Which problems? `\t` should be pretty standard throughout the OS'. – Felix Kling Apr 06 '10 at 14:09
  • When I use \n\t in ubuntu with DataInputStream, it miss.Just could find 0a but not 09 with hex-mode – jerry Mar 07 '14 at 09:24
10

use \t instead of space.

bw.write("\t"); 
Amsakanna
  • 12,254
  • 8
  • 46
  • 58
5

Use "\t". That's the tab space character.

You can find a list of many of the Java escape characters here: http://java.sun.com/docs/books/tutorial/java/data/characters.html

Jacinda
  • 4,932
  • 3
  • 26
  • 37