Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    private static File saveFile(InputStream is, String destDir, String fileName) throws IOException {
        File dir = new File(destDir);
        fileName = getString(fileName);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = null;
        try {
            File saveFile = new File(destDir, fileName);
            bos = new BufferedOutputStream(new FileOutputStream(saveFile));
            int len = -1;
            while ((len = bis.read()) != -1) {
                bos.write(len);
                bos.flush();
            }
            bos.close();
            bis.close();
            return saveFile;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getString(String str) {
        String regex = "[\\/:?*<>|]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
}