Example usage for java.io BufferedWriter close

List of usage examples for java.io BufferedWriter close

Introduction

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

Prototype

@SuppressWarnings("try")
    public void close() throws IOException 

Source Link

Usage

From source file:localization.split.java

public static boolean checkLPU(String filepath, String passoloPath) {
    File logfile = new File(filepath.substring(0, filepath.lastIndexOf("\\") + 1) + "error.log");
    try {//from  w  ww  .  j  a va 2  s. c  om
        String cmd = "cmd.exe /c " + passoloPath + " /openproject:" + filepath;
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(cmd);
        InputStreamReader isr = new InputStreamReader(proc.getInputStream());
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.contains("Opening in read-only mode")) {
                if (!logfile.exists()) {
                    logfile.createNewFile();
                }
                String content = filepath
                        + " is not able to process because it is a type of Passolo 2011. Please process it with Passolo 2011."
                        + "\n";
                FileWriter fw = new FileWriter(logfile.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(content);
                bw.close();
                return false;
            }
        }
        int exitVal = proc.waitFor();
        if (exitVal == 10) {
            if (!logfile.exists()) {
                logfile.createNewFile();
            }
            String content = filepath
                    + " is not able to process because it is a type of Passolo 2011. Please process it with Passolo 2015."
                    + "\n";
            FileWriter fw = new FileWriter(logfile.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            return false;
        }

    } catch (Exception e) {
        try {
            String content = e.getMessage();
            FileWriter fw = new FileWriter(logfile.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        } catch (Exception e1) {

        }

        return false;
    }
    return true;
}

From source file:Main.java

public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory() + "/clr_log.file");
    if (!logFile.exists()) {
        try {/* www .  j  av a  2 s .c  o m*/
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        BufferedReader buf = new BufferedReader(new FileReader(logFile));

        String line = "";
        ArrayList<String> lines = new ArrayList<String>();

        while ((line = buf.readLine()) != null) {
            lines.add(line);
        }

        int size = lines.size();

        //Every time the number of lines go over 1000, only add the last 500. This will keep its size to a minimum
        int i = lines.size() - 500;
        if (i < 0) {
            i = 0;
        }

        buf.close();

        BufferedWriter bufW = new BufferedWriter(new FileWriter(logFile, true));

        if (size > 1000) {
            bufW.write("");
            for (; i < lines.size(); i++) {
                bufW.append(line);
            }
        }

        bufW.append(text + "\n");
        bufW.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.caorongjin.pingguo.Pingguo.java

private static void _cleanup(BufferedWriter bw) {
    if (bw != null) {
        try {//ww  w.ja va2  s. com
            bw.close();
        } catch (Exception e) {
        }
    }
}

From source file:eu.sisob.uma.footils.File.FileFootils.java

/**
 *
 * @param content//w ww  .  j a  v a 2 s. co  m
 * @param name
 * @param encode
 */
public static void writeFile(String content, String name, String encode)
        throws FileNotFoundException, UnsupportedEncodingException, IOException {
    File fField = new File(name);
    FileOutputStream fileOS;
    fileOS = new java.io.FileOutputStream(fField, false);
    OutputStreamWriter writer = new java.io.OutputStreamWriter(fileOS, encode);
    BufferedWriter bw = new java.io.BufferedWriter(writer);
    String sOut = content;
    bw.write(sOut);
    bw.close();
}

From source file:com.testmax.util.FileUtility.java

public static void writeToFile(String path, String text) {
    try {//ww  w.j a  v  a2 s. c om
        // Create file 
        FileWriter fstream = new FileWriter(path);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(text);
        //Close the output stream
        out.close();
    } catch (Exception e) {//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

From source file:ch.epfl.bbp.uima.uimafit.CpeBuilder.java

/**
 * Writes a temporary file containing a xml descriptor of the given
 * resource. Returns the file.//from  w w w  .j a  va  2 s .co m
 * 
 * @param resource
 *            A resource specifier that should we materialized.
 * @return The file containing the xml representation of the given resource.
 * @throws IOException
 * @throws SAXException
 */
private static File materializeDescriptor(ResourceSpecifier resource) throws IOException, SAXException {
    File tempDesc = File.createTempFile("desc", ".xml");
    // tempDesc.deleteOnExit();

    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempDesc), "UTF-8"));
    resource.toXML(out);
    out.close();

    return tempDesc;
}

From source file:Main.java

public static boolean saveFile(String str, File path) {
    BufferedWriter bw = null;

    try {/*  w  w w .j  a va  2s .  c o m*/
        bw = new BufferedWriter(new FileWriter(path));
        bw.write(str);
        bw.flush();
        return true;

    } catch (IOException e) {
        e.printStackTrace();
        return false;

    } finally {
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:com.qmetry.qaf.automation.util.FileUtil.java

public static void appendFile(String fileName, StringBuffer sb) throws IOException {
    FileWriter fileWritter = new FileWriter(fileName, true);
    BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
    bufferWritter.write(sb.toString());/*from w w  w  .j  a  va  2 s  . c  o  m*/
    bufferWritter.close();
}

From source file:com.khepry.utilities.GenericUtilities.java

public static void generateOutFile(String outFilePath, String outputText, Handler handler) {
    Logger.getLogger("").addHandler(handler);
    File outFile = new File(outFilePath);
    if (outFile.getParentFile().exists()) {
        try {//from ww w. j a v  a2  s.  co m
            BufferedWriter bw = new BufferedWriter(new FileWriter(outFilePath));
            bw.write(outputText);
            bw.close();
        } catch (IOException ex) {
            Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE,
                "Output path: " + outFile.getParent() + " does not exist!", new FileNotFoundException());
    }
}

From source file:dao.EntryDaoTest.java

@BeforeClass
public static void setUpClass() {
    String fSeparator = File.separator;
    try {/*  w  w w  .j a v a 2 s.c  o  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);
    }
}