List of usage examples for java.io FileWriter FileWriter
public FileWriter(File file, Charset charset) throws IOException
From source file:Main.java
public static void appendLog(String text) { File logFile = new File(Environment.getExternalStorageDirectory() + "/clr_log.file"); if (!logFile.exists()) { try {// w w w . 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:dao.EntryDaoTest.java
@BeforeClass public static void setUpClass() { String fSeparator = File.separator; try {// ww w. jav 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); } }
From source file:csv.FileManager.java
static public boolean appendItems(String fileName, ArrayList<ArrayList<String>> data, boolean firstRowIsHeader) { try {/*w ww .j a va 2s. c om*/ BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true)); CSVWriter writer = new CSVWriter(out); String[] t = new String[0]; for (ArrayList<String> row : data) { if (!firstRowIsHeader) { boolean found = false; for (String str : row) { if (!str.isEmpty()) { found = true; } } if (found) writer.writeNext(row.toArray(t)); } else firstRowIsHeader = false; } out.close(); return true; } catch (Exception e) { System.out.println(e.getMessage()); return false; } }
From source file:it.geosolutions.tools.io.file.writer.Writer.java
/** * Open 'destination' file in append mode and append content of the * 'toAppend' file//from w w w . ja v a2 s . c o m * * @param toAppend * @param destination * @throws IOException */ public static void appendFile(File toAppend, File destination) throws IOException { FileWriter fw = null; BufferedWriter bw = null; LineIterator it = null; try { fw = new FileWriter(destination, true); bw = new BufferedWriter(fw); it = FileUtils.lineIterator(toAppend); while (it.hasNext()) { bw.append(it.nextLine()); bw.newLine(); } bw.flush(); } finally { if (it != null) { it.close(); } if (bw != null) { IOUtils.closeQuietly(bw); } if (fw != null) { IOUtils.closeQuietly(fw); } } }
From source file:Main.java
public static boolean writeFile(String filePath, String content, boolean append) { if (TextUtils.isEmpty(content)) { return false; }// ww w . ja va2 s. co m FileWriter fileWriter = null; try { makeDirs(filePath); fileWriter = new FileWriter(filePath, append); fileWriter.write(content); fileWriter.close(); return true; } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } }
From source file:com.codemage.sql.util.SonarQubeManager.java
public void createJavaFile(String JavaCode) { BufferedWriter bw = null;//from w w w . j av a 2 s .c om try { // APPEND MODE SET HERE bw = new BufferedWriter(new FileWriter( "E:\\Smile24\\Projects\\0005 - CAL\\sampleAnalyseCode\\src\\main\\java\\checkbook.java", false)); bw.write(JavaCode); bw.newLine(); bw.flush(); } catch (IOException ioe) { } finally { // always close the file if (bw != null) { try { bw.close(); } catch (IOException ioe2) { // just ignore it } } } // end try/catch/finally }
From source file:fm.last.commons.io.LastFileUtils.java
/** * Appends the passed string to the passed file. * //from ww w. j a v a 2 s . co m * @param file File to append string to (will be created if it does not exist). * @param string String to append. * @throws IOException If an error occurs appending the string to the file. */ public static void appendStringToFile(File file, String string) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); writer.write(string); writer.close(); }
From source file:com.eftech.wood.controllers.test.Logger.java
protected void printInFile(String fileName, String str) { // For Check File file = new File("d:\\2\\" + fileName); // File file = new File("HMC exeption2.txt"); try (FileWriter fileWriter = new FileWriter(file, true)) { fileWriter.write("-------> " + new Date() + "): \n"); fileWriter.write(str + "\n\n"); } catch (IOException ex) { // Logger.getLogger(ControllerMachine.class.getName()).log(Level.SEVERE, // null, ex); }/*from w w w .j a va2 s . c om*/ }
From source file:com.athena.peacock.common.core.util.SshExecUtil.java
/** * <pre>//from ww w . j a v a 2 s . co m * * </pre> * @param targetHost * @param commandList * @throws IOException */ public static void executeCommand(TargetHost targetHost, List<String> commandList) throws IOException { fstream = new FileWriter(output, false); out = new BufferedWriter(fstream); for (String command : commandList) { out.write("[" + targetHost.getUsername() + "@" + targetHost.getHost() + " ~]$ " + command + "\n"); out.write(executeCommand(targetHost, command)); } IOUtils.closeQuietly(out); }
From source file:com.hys.enterprise.dominoes.reporting.ReportBuilder.java
/** * Writes record to csv file/*from www .j ava2 s. c o m*/ * @param record * @throws IOException */ public void writeRecord(ReportRecord record) throws IOException { fileWriter = new FileWriter(file, true); CSVPrinter csvFilePrinter; csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat); if (!file.exists() || file.length() == 0) { csvFilePrinter.printRecord(record.getHeader()); } csvFilePrinter.printRecord(record.getData()); if (fileWriter != null) { fileWriter.flush(); fileWriter.close(); } csvFilePrinter.close(); }