Java tutorial
/** ======================================================================== */ /** */ /** @copyright Copyright (c) 2010-2015, S2S s.r.l. */ /** @license http://www.gnu.org/licenses/gpl-2.0.html GNU Public License v.2 */ /** @version 6.0 */ /** This file is part of SdS - Sistema della Sicurezza . */ /** SdS - Sistema della Sicurezza is free software: you can redistribute it and/or modify */ /** it under the terms of the GNU General Public License as published by */ /** the Free Software Foundation, either version 3 of the License, or */ /** (at your option) any later version. */ /** SdS - Sistema della Sicurezza is distributed in the hope that it will be useful, */ /** but WITHOUT ANY WARRANTY; without even the implied warranty of */ /** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /** GNU General Public License for more details. */ /** You should have received a copy of the GNU General Public License */ /** along with SdS - Sistema della Sicurezza . If not, see <http://www.gnu.org/licenses/gpl-2.0.html> GNU Public License v.2 */ /** */ /** ======================================================================== */ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package s2s.file.io; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.rtf.RtfWriter2; import java.io.InputStream; import java.io.FileInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * * @author Dario */ public class BynaryFileReader { public BynaryFileReader() { // } public byte[] getBytesFromFile(String filePath) throws IOException { return this.getBytesFromFile(new File(filePath)); } public byte[] getBytesFromFile(File file) throws IOException { // Se il file non esiste o vuoto esco dal metodo. if (file.exists() == false || file.length() == 0) { return null; } InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[(int) length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } // Close the input stream and return bytes is.close(); return bytes; } public boolean fileExists(String srFile) { File f = new File(srFile); return f.exists(); } public boolean copyfile(String srFile, String dtFile) { try { File f1 = new File(srFile); File f2 = new File(dtFile); InputStream in = new FileInputStream(f1); //For Append the file. //OutputStream out = new FileOutputStream(f2,true); //For Overwrite the file. OutputStream out = new FileOutputStream(f2); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); return true; } catch (FileNotFoundException ex) { System.out.println(ex.getMessage() + " in the specified directory."); return false; } catch (IOException e) { System.out.println(e.getMessage()); return false; } } public void createWordFile(String filePath, String fileContent) throws FileNotFoundException, DocumentException { Document document = new Document(PageSize.A4); RtfWriter2.getInstance(document, new FileOutputStream(filePath)); document.open(); Paragraph para = new Paragraph(fileContent); document.add(para); document.close(); } public void createWordFile(String filePath) throws FileNotFoundException, DocumentException { this.createWordFile(filePath, ""); } }