码迷,mamicode.com
首页 > 编程语言 > 详细

【java IO】使用Java输入输出流 读取txt文件内数据,进行拼接后写入到另一个文件中

时间:2016-11-01 13:36:30      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:art   values   net   line   view   port   public   class   exce   

技术分享
 1 package com.sxd.test.util;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 import java.io.InputStreamReader;
10 import java.io.OutputStreamWriter;
11 
12 import org.junit.Test;
13 
14 public class CreateInsert {
15 
16     @Test
17     public void test() throws IOException{
18         
19         //1.读取文件
20         File file = new File("d:/new4.txt");
21         InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf-8");//考虑到编码格式
22         BufferedReader bu = new BufferedReader(read);
23         //2.拼接字符串
24         String lineText = null;
25         String insert = "INSERT INTO [report].[dbo].[process] ([bgid]) VALUES (‘";
26         String insert2 = "‘);";
27         //3.边读边写
28         OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new File("d:/写出文件.txt")));
29         BufferedWriter wr = new BufferedWriter(out);
30         
31         while((lineText = bu.readLine()) != null){
32             System.out.println(insert+lineText+insert2);
33             wr.write(insert+lineText+insert2+"\r\n");
34         }
35         read.close();
36         wr.close();
37     }
38 }
View Code

 

【java IO】使用Java输入输出流 读取txt文件内数据,进行拼接后写入到另一个文件中

标签:art   values   net   line   view   port   public   class   exce   

原文地址:http://www.cnblogs.com/sxdcgaq8080/p/6018906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!