Example usage for java.io FileWriter close

List of usage examples for java.io FileWriter close

Introduction

In this page you can find the example usage for java.io FileWriter close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:com.daphne.es.maintain.icon.web.controller.tmp.GenCssSql.java

private static void readClass() throws IOException {
    String fromFile = "C:\\Documents and Settings\\Administrator\\?\\a.txt";
    String toFile = "C:\\Documents and Settings\\Administrator\\?\\b.sql";
    String template = "insert into `maintain_icon` (`id`, `identity`, `css_class`, `type`) values(%1$d, '%2$s', '%2$s', 'css_class');;";

    List<String> cssClassList = FileUtils.readLines(new File(fromFile));
    List<String> hasReadList = Lists.newArrayList();
    FileWriter writer = new FileWriter(toFile);

    for (int i = 0, l = cssClassList.size(); i < l; i++) {
        if (!hasReadList.contains(cssClassList.get(i))) {
            writer.write(String.format(template, i + 1, cssClassList.get(i).trim()));
            writer.write("\r\n");
            hasReadList.add(cssClassList.get(i));
        }/*  w  w w . j  av a  2 s . c o  m*/
    }

    writer.close();
}

From source file:dao.EntryDaoTest.java

@BeforeClass
public static void setUpClass() {
    String fSeparator = File.separator;
    try {/*  w w w .j a v  a2  s  . co m*/
        File folder = new File(
                System.getProperty("user.dir") + fSeparator + "MyDiaryBook" + fSeparator + "Users" + fSeparator
                        + "Panagiwtis Georgiadis" + fSeparator + "Entries" + fSeparator + "Texnologia2");
        folder.mkdirs();

        File textFolder = new File(folder.toString() + fSeparator + "Texts");
        textFolder.mkdirs();
        File textFile = new File(textFolder.toString() + fSeparator + "test.txt");
        BufferedWriter bw;
        FileWriter fw;
        fw = new FileWriter(textFile, true);
        bw = new BufferedWriter(fw);
        bw.write("test0123456789");
        if (bw != null)
            bw.close();
        fw.close();
        File imageFolder = new File(folder.toString() + fSeparator + "Images");
        imageFolder.mkdirs();
        String imageSourcePath = System.getProperty("user.dir") + fSeparator + "src" + fSeparator + "test"
                + fSeparator + "java" + fSeparator + "resources" + fSeparator + "testImg.jpg";
        File imageSourceFile = new File(imageSourcePath);
        FileUtils.copyFileToDirectory(imageSourceFile, imageFolder);

        String videoSourcePath = System.getProperty("user.dir") + fSeparator + "src" + fSeparator + "test"
                + fSeparator + "java" + fSeparator + "resources" + fSeparator + "testVideo.mp4";
        File videoSourceFile = new File(videoSourcePath);
        File videoFolder = new File(folder.toString() + fSeparator + "Videos");
        videoFolder.mkdirs();
        FileUtils.copyFileToDirectory(videoSourceFile, videoFolder);
    } catch (IOException ex) {
        Logger.getLogger(EntryDaoTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.dongli.model.MyJSONData.java

public static String createObject(MyJSONObject mjo) throws MyRESTException {

    // generate uid for this object
    String uid = UIDGenerator.getUID();
    mjo.setUID(uid);//from  ww  w  .  ja va2 s  .c  om

    // tmp path of data file to store this object. The file will to sent to S3 later.
    String path = "/tmp/" + uid;

    try {
        FileWriter fw = new FileWriter(path, false);
        PrintWriter pw = new PrintWriter(fw);
        pw.println(mjo.toString());
        pw.close();
        fw.close();
    } catch (IOException e) {
        // e.printStackTrace();
        // failed to create the new object
        File nf = new File(path);
        nf.delete();
        throw new MyRESTException("Failed to create the object " + uid + ".");
    }

    // create the new object on AWS S3 
    try {
        File uploadFile = new File(path);
        MyAWSStorage.getInstance().s3client
                .putObject(new PutObjectRequest(MyConfiguration.getInstance().bucket, uid, uploadFile));
    } catch (AmazonServiceException ase) {
        throw new MyRESTException("Failed to create the object " + uid + ".");
    } catch (AmazonClientException ace) {
        throw new MyRESTException("Failed to create the object " + uid + ".");
    }

    return uid;
}

From source file:no.uib.tools.OnthologyHttpClient.java

public static List<String> getTermDescendants(String onthologyName, String term, String relation) {
    List<String> result = new ArrayList<>();
    String queryContent = "";

    onthologySize = getOntologySize(onthologyName);
    String uri = createUri(onthologyName, term, relation);
    //BufferedReader br = getContentBufferedReader(uri);
    //String queryContent = convertBufferedReader(br);

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet(uri);
    getRequest.addHeader("accept", "application/json");

    HttpResponse response;//ww  w.j  a va2s  .  c  o m
    try {
        response = httpClient.execute(getRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed to get the PSIMOD onthology : HTTP error code : "
                    + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
        String line;
        FileWriter fw = new FileWriter("./mod.json");

        while ((line = br.readLine()) != null) {
            fw.write(line);
            queryContent += line;
        }

        fw.close();
        httpClient.getConnectionManager().shutdown();

        char c;
        int matched = 0;
        String mod = "";
        int pos = 0;

        while (pos < queryContent.length()) {
            c = queryContent.charAt(pos);
            pos++;
            if (c == pattern.charAt(matched)) {
                matched++;
                if (matched == pattern.length()) {
                    for (int I = 0; I < 5; I++) {
                        mod += queryContent.charAt(pos);
                        pos++;
                    }
                    result.add(mod);
                    mod = "";
                    matched = 0;
                }
            } else {
                matched = 0;
            }
        }

    } catch (IOException ex) {
        Logger.getLogger(OnthologyHttpClient.class.getName()).log(Level.SEVERE, null, ex);
    }

    return result;
}

From source file:index.IncrementIndex.java

/**
 * id//from   w w  w .  j ava2  s .c o  m
 * @param path
 * @param storeId
 * @return 
 */
public static boolean writeStoreId(String path, String storeId) {
    boolean b = false;
    try {
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(path);
        PrintWriter out = new PrintWriter(fw);
        out.write(storeId);
        out.close();
        fw.close();
        b = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return b;
}

From source file:com.abiquo.appliancemanager.ApplianceManagerAsserts.java

/**
 * //from w ww. j a  v a2  s  .  c  om
 * 
 * */

protected static void createBundleDiskFile(final String ovfId, final String snapshot) throws Exception {
    EnterpriseRepositoryService er = ErepoFactory.getRepo(String.valueOf(idEnterprise));

    final String ovfpath = TemplateConventions.getRelativePackagePath(ovfId);
    final String diskFilePathRel = er.getDiskFilePath(ovfId);
    // final String diskFilePathRel = diskFilePath.substring(diskFilePath.lastIndexOf('/'));

    final String path = FilenameUtils.concat(FilenameUtils.concat(er.path(), ovfpath),
            (snapshot + "-snapshot-" + diskFilePathRel));

    // "/opt/testvmrepo/1/rs.bcn.abiquo.com/m0n0wall/000snap000-snapshot-m0n0wall-1.3b18-i386-flat.vmdk"

    File f = new File(path);
    f.createNewFile();
    f.deleteOnExit();

    FileWriter fileWriter = new FileWriter(f);
    for (int i = 0; i < 1000; i++) {
        fileWriter.write(i % 1);
    }
    fileWriter.close();
}

From source file:eu.liveandgov.ar.utilities.OS_Utils.java

/**
 *         Save a string into a file in SD 
 *///from ww  w.j  a  v a 2  s. c  o m
public static boolean write2File(String filename, String string) {
    try {

        FileWriter fstream = new FileWriter(filename);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(string);
        out.close();
        fstream.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:co.foxdev.foxbot.utils.Utils.java

public static boolean addCustomCommand(String channel, String command, String text) {
    String filePath = "data/custcmds/" + channel.substring(1);
    File path = new File(filePath);

    try {/*w ww . ja v a  2 s  .  c o  m*/
        if (!path.exists() && !path.mkdirs()) {
            foxbot.getLogger().warn("Error occurred while creating custom command folders!");
        }

        File file = new File(filePath + "/" + command);

        if (file.exists() && !file.delete()) {
            foxbot.getLogger().warn(
                    String.format("Error occurred while deleting command '%s' for %s!", command, channel));
        }

        if (text.isEmpty() || text.equalsIgnoreCase("delete")) {
            if (file.delete()) {
                foxbot.getLogger().warn(String.format("Command '%s' deleted for %s!", command, channel));
            }
            return false;
        }

        FileWriter fw = new FileWriter(filePath + "/" + command);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(text);
        bw.flush();
        bw.close();
        fw.flush();
        fw.close();
        foxbot.getLogger()
                .warn(String.format("Command '%s' set for %s at %s", command, channel, file.getAbsolutePath()));
    } catch (IOException ex) {
        foxbot.getLogger().error("Error occurred while adding custom command", ex);
    }
    return true;
}

From source file:fr.inria.oak.paxquery.xparser.client.XClient.java

private static void printDOTFile(String filePath, String dotString) {
    String filePathDot = filePath;
    String filePathPNG = filePath;
    if (filePathDot.startsWith("file://")) {
        filePathDot = filePathDot.substring("file://".length());
        filePathPNG = filePathPNG.substring("file://".length());
    }/*from www.j  a v  a2  s  .c  o  m*/
    /*
     * int extensionIndex = filePathDot.lastIndexOf("."); if(extensionIndex > -1) { filePathDot =
     * filePathDot.subSequence(0, extensionIndex) + "-pact.dot"; filePathPNG =
     * filePathPNG.subSequence(0, extensionIndex) + "-pact.png"; } else { filePathDot = filePathDot
     * + "-pact.dot"; filePathPNG = filePathPNG + "-pact.png"; }
     */
    if (filePath.endsWith("/") == true) {
        filePathDot = filePathDot + "xoutput-pact.dot";
        filePathPNG = filePathPNG + "xoutput-pact.png";
    } else {
        filePathDot = filePathDot + "/xoutput-pact.dot";
        filePathPNG = filePathPNG + "/xoutput-pact.png";
    }

    try {
        // print the dot file
        FileWriter writer = new FileWriter(filePathDot, false);
        writer.write(dotString);
        writer.close();
        Runtime r = Runtime.getRuntime();
        String com = new String("dot -Tpng " + filePathDot + " -o " + filePathPNG);
        Process p = r.exec(com);
        p.waitFor();
        // System.out.println("PACT plan drawn.");
    } catch (IOException ioe) {
        System.out.println("Error with pact file: " + ioe.getMessage());
    } catch (InterruptedException ie) {
        System.out.println("Error with pact file: " + ie.getMessage());
    }
}

From source file:com.yahoo.dba.tools.myperfserver.App.java

private static void writePid() {
    if (pid < 0)
        return;//not supported
    try {//from w w  w.  j a  v a 2s  .co  m
        java.io.FileWriter fw = new java.io.FileWriter(PID_FILE);
        fw.write(String.valueOf(pid));
        fw.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}