zoukankan      html  css  js  c++  java
  • 【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+"
    ");
    34         }
    35         read.close();
    36         wr.close();
    37     }
    38 }
    View Code
  • 相关阅读:
    Mac OS X上安装 Ruby运行环境
    MAC 命令行工具(Command Line Tools)安装
    如何快速正确的安装 Ruby, Rails 运行环境
    安裝 Rails 開發環境
    用模块化编程
    阅读技术书籍
    NHibernate构建一个ASP.NET MVC应用程序
    SQL注入
    Redis
    Code digest
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/6018906.html
Copyright © 2011-2022 走看看