如何在写入文本文件时使用“标签空间”

2022-09-01 16:31:21
 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;
  }

我需要在每列之间“一个选项卡空间”(写入文本文件时)。喜欢

   manu 25 data1
   manc 35 data3

在我的代码中,我用于在每列之间创建空间。如何在那个地方使用“一个标签空间”,而不是给“空间”。bw.write(" ")


答案 1

您可以使用 在文件中创建选项卡。\t


答案 2

使用 \t 而不是空格。

bw.write("\t"); 

推荐