Friday, May 20, 2016

Java transfer content from one file to another

http://www.programcreek.com/2011/03/java-transfer-content-from-one-file-to-another/


package com.aaa.fileDataTransfer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileDataModTransfer {
public static void main(String[] args) throws IOException {
String source = "F://fileDataInput.txt";
String dest = "F://fileDataOutput.txt";

File fin = new File(source);
FileInputStream fis = new FileInputStream(fin);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));

FileWriter fstream = new FileWriter(dest, true);
BufferedWriter out = new BufferedWriter(fstream);

String aLine = null;
while ((aLine = in.readLine()) != null) {
// Process each line and add output to Dest.txt file
String[] parts = aLine.split(" ");
System.out.println("First Part: " + parts[0]);
System.out.println("Second Part: " + parts[1]);
String generatedLine = "(" + parts[0] + ", '" + parts[1] + "'),";
System.out.println(generatedLine.toString());
out.write(generatedLine);
out.newLine();
}
// do not forget to close the buffer reader
in.close();
// close buffer writer
out.close();
}
}

INPUT: F://fileDataInput.txt
88 baikaName_88
89 baikaName_89
90 baikaName_90

OUTPUT:F://fileDataOutput.txt
(88, 'baikaName_88'),
(89, 'baikaName_89'),
(90, 'baikaName_90'),

No comments:

Post a Comment