Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.File;
- import java.io.Writer;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- public class FileMerger {
- /* By Chris Clarke, 13.09.2017 */
- public void writeFile(String filename, String text) {
- Writer writer = null;
- try {
- File file = new File(filename);
- writer = new BufferedWriter(new FileWriter(file));
- writer.write(text);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (writer != null) {
- writer.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void alternate(String fname, String t1, String t2) {
- String[] text1 = t1.split("\r\n");
- String[] text2 = t2.split("\r\n");
- String result = "";
- for (int i=0;i<text1.length; i++) {
- result += text1[i] + "\r\n";
- if (i<text2.length) result += text2[i]+"\r\n";
- }
- writeFile(fname, result);
- }
- public static void main(String[] args) {
- FileMerger fm = new FileMerger();
- String text1 = "Hello\r\nHow are you?\r\nBye";
- fm.writeFile("File1.dat", text1);
- String text2 = "Welcome to the Computer Lab\r\nThere are 2 Labs in the Senior School";
- fm.writeFile("File2.dat", text2);
- fm.alternate("File3.dat", text1, text2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement