Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    private static void saveFileByRequestPath(String requestPath, String result) {
        // TODO Auto-generated method stub
        deleteFileFromLocal(requestPath);
        saveFileForLocal(requestPath, result);
    }

    private static void deleteFileFromLocal(String requestPath) {
        // TODO Auto-generated method stub
        File file = new File(requestPath);
        if (file.exists()) {
            file.delete();
        }
    }

    private static void saveFileForLocal(String requestPath, String result) {
        // TODO Auto-generated method stub
        File file = new File(requestPath);
        if (!file.exists()) {
            try {
                File parentFile = file.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                file.createNewFile();
                FileOutputStream fout = new FileOutputStream(file);
                byte[] buffer = result.getBytes();
                fout.write(buffer);
                fout.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}