【问题标题】:how to use "tab space" while writing in text file在文本文件中写入时如何使用“制表符空间”
【发布时间】:2011-02-04 20:00:27
【问题描述】:
 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(" ") 在每列之间创建空间。如何在那个地方使用“一个标签空间”而不是给“空间”。

【问题讨论】:

    标签: java tabs text-formatting


    【解决方案1】:

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

    【讨论】:

    • “任何问题”是什么意思?如果您遇到问题并且希望帮助解决它,请描述问题。
    • @Manu:有哪些问题? \t 在整个操作系统中应该是相当标准的。
    • 当我在 ubuntu 中使用 \n\t 和 DataInputStream 时,它会丢失。使用 hex-mode 只能找到 0a 而不是 09
    【解决方案2】:

    使用 \t 代替空格。

    bw.write("\t"); 
    

    【讨论】:

    • 先尝试一下,如果遇到任何问题,请在此处报告
    【解决方案3】:

    使用“\t”。那是制表符空格字符。

    您可以在此处找到许多 Java 转义字符的列表:http://java.sun.com/docs/books/tutorial/java/data/characters.html

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      相关资源
      最近更新 更多