Java 在文本文件中写入时如何使用“制表符空间”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2585337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:30:42  来源:igfitidea点击:

how to use "tab space" while writing in text file

javatabstext-formatting

提问by Manu

 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".

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

采纳答案by tmeisenh

You can use \tto create a tab in a file.

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

回答by Jacinda

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

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

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

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

回答by Amsakanna

use \t instead of space.

使用 \t 而不是空格。

bw.write("\t");