Java BufferedReader Copy copyFile(String src, String dst)

Here you can find the source of copyFile(String src, String dst)

Description

copy File

License

Open Source License

Declaration

static public boolean copyFile(String src, String dst) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Main {
    static public boolean copyFile(String src, String dst) {
        if (src == null || dst == null) {
            return false;
        }/*  w  w w . j  a  va  2s .c om*/
        return writeTextToFile(getTextFromFile(src), dst);
    }

    public static boolean writeTextToFile(String text, String filename) {
        if (text == null || filename == null) {
            return false;
        }
        try {
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF8"));
            pw.print(text);
            pw.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String getTextFromFile(String filename) {
        try {
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(filename), "UTF-8"));

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            reader.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. copyFile(File original, File copy)
  2. copyFile(File source, File dest)
  3. copyFile(File src, File dst)
  4. copyFile(String orig, String dest)
  5. copyFile(String source, String dest)
  6. copyFile(String src, String dst)
  7. copyFile(String srcFile, String dstFile)
  8. copyFile(String srcFileName, String destFileName)
  9. copyFile2(String source, String out)