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

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

技术分享
 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文件内数据,进行拼接后写入到另一个文件中