TXT文書の生成されたJava

コードは以下の通りである:PS: "GBK" は、別の符号化フォーマットは、 "UTF-8"、 "GBK"、 "UTF-8" のように、変更してもよいです...

1  インポートorg.apache.commons.lang.StringUtils。
2  
3  輸入 java.io. * ;
4  インポートjava.util.ArrayListの。
5  輸入はjava.util.List;
6  
7  パブリック クラスTestMain1 {
 8      パブリック 静的 ボイドメイン(文字列[]引数){
 9          リストの<string> STRINGLIST = 新規のArrayList <> ();
10          stringList.add( "1哈哈黑" )。
11          stringList.add( "2嘿嘿黑" )。
12          writeDataHubData(STRINGLIST、 "111");
13          のSystem.out.println(111 )。
14      }
 15  
16      / ** 
17       *写入TXT文件
 18       *
 19       * @paramの結果
 20       * @param fileNameに
 21       * @return 
22       * / 
23      パブリック 静的 ブール writeDataHubData(リストの<string> 結果、文字列filename){
 24          ロング START = のSystem.currentTimeMillis();
25          文字列filePathに= "D:\\ TEMP \\ TXT" 26         StringBuilderの内容= 新しいStringBuilderの();
27          ブールフラグ= 28          BufferedWriterのアウト= nullを29          トライ{
 30              場合(結果!= NULL && result.isEmpty()&&!StringUtils.isNotEmpty(filename)で){
 31                  fileNameに+ = "_" +のSystem.currentTimeMillis()+ ".txtファイル" ;
32                  ファイルpathFile = 新しいファイル(filePathに)。
33                  であれば(!pathFile.exists()){
 34                      pathFile.mkdirs()。
35                 }
 36                  文字relFilePath = filePathに+ File.separator + ファイル名;
37                  ファイルファイル= 新しいファイル(relFilePath)。
38                  であれば(!file.exists()){
 39                      file.createNewFile()。
40                  }
 41                  アウト= 新しい BufferedWriterの(新規のOutputStreamWriter(たFileOutputStream(ファイル)、 "GBK" ));
42  //                 // 标题头
 43  //                out.write( "curr_time、LINK_ID、TRAVEL_TIME、速度、reliabilitycode、link_len、adcode、TIME_STAMP、状態、public_rec_time、DS")。
44  //                 out.newLine()。
45                  のために(文字列情報:結果){
 46  
47                      out.write(情報)。
48                      out.newLine()。
49                  }
 50                  フラグ= 51  //                 logger.info( "写入文件耗时:*********************************" +( System.currentTimeMillis() -スタート)+ "毫秒"); 
52                 System.out.println( "写入文件耗时:*********************************" +(システム.currentTimeMillis() -スタート)+ "毫秒" );
53              }
 54          } キャッチ(IOExceptionを電子){
 55              e.printStackTrace();
56          } 最後に{
 57              であれば(!アウト= NULL ){
 58                  のtry {
 59                      out.flush()。
60                      out.close()。
61                  } キャッチ(IOExceptionを電子){
 62                      e.printStackTrace();
63                 }
 64              }
 65              リターンフラグ。
66          }
 67      }
 68 }

参考リンク:

https://blog.csdn.net/fenfenguai/article/details/78920711

 

おすすめ

転載: www.cnblogs.com/m-yb/p/11528022.html