Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    /**
     * From http://stackoverflow.com/questions/9292954/how-to-make-a-copy-of-a-file-in-android
     */
    public static void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

    public static String read(File src) throws IOException {
        long length = src.length();

        // Probably not the best way to do it, but I'm lazy.
        char[] chars = new char[(int) length];
        FileReader fileReader = new FileReader(src);
        fileReader.read(chars);
        fileReader.close();
        return new String(chars);
    }

    public static void write(String text, File dst) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(dst));
        bufferedWriter.write(text);
        bufferedWriter.flush();
        bufferedWriter.close();
    }
}