Example usage for org.apache.commons.io FilenameUtils removeExtension

List of usage examples for org.apache.commons.io FilenameUtils removeExtension

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils removeExtension.

Prototype

public static String removeExtension(String filename) 

Source Link

Document

Removes the extension from a filename.

Usage

From source file:com.fileOperations.WordToPDF.java

/**
 * Creates PDF from DOCX, does this by opening file and "SaveAs" within
 * Microsoft Office itself and closing out.
 *
 * @param filePath String/*ww  w  .j av a 2 s.c om*/
 * @param fileName String
 * @return String - new File Name
 */
public static String createPDF(String filePath, String fileName) {
    ActiveXComponent eolWord = null;
    String docxFile = filePath + fileName;
    String pdfFile = filePath + FilenameUtils.removeExtension(fileName) + ".pdf";

    File attachmentLocation = new File(filePath);
    if (!attachmentLocation.exists()) {
        attachmentLocation.mkdirs();
    }

    eolWord = JacobCOMBridge.setWordActive(true, false, eolWord);
    if (eolWord != null) {
        try {
            //Open MS Word & Save AS
            Dispatch document = eolWord.getProperty("Documents").toDispatch();
            Dispatch.call(document, "Open", docxFile).toDispatch();
            Dispatch WordBasic = Dispatch.call(eolWord, "WordBasic").getDispatch();
            Dispatch.call(WordBasic, "FileSaveAs", pdfFile, new Variant(17));
            Dispatch.call(document, "Close", new Variant(false));
            Thread.sleep(250);

            //Close out MS Word
            JacobCOMBridge.setWordActive(false, false, eolWord);
            Dispatch.call(eolWord, "Quit");
            eolWord.safeRelease();
            File oldDoc = new File(docxFile);
            oldDoc.delete();
            return FilenameUtils.getName(pdfFile);
        } catch (InterruptedException ex) {
            ExceptionHandler.Handle(ex);
            return "";
        }
    }
    return "";
}

From source file:com.stam.batchmove.BatchMoveUtils.java

public static File findFileByName(List<File> files, String fileName) {
    File file = null;/*from  w  w  w  .j  av  a2  s  .c o m*/

    for (File f : files) {
        if (f.isFile()) {
            String fn = f.getName();
            String fileNameWithoutExt = FilenameUtils.removeExtension(fn);
            if (fileName.equals(fn)) {
                file = f;
                break;
            } else if (fileName.equals(fileNameWithoutExt)) {
                file = f;
                break;
            }
        }
    }

    return file;
}

From source file:com.fileOperations.TXTtoPDF.java

/**
 * creates PDF from text document//from  w  ww. jav a 2  s.co m
 *
 * @param filePath String
 * @param fileName String
 * @return String - new File Name
 */
public static String createPDF(String filePath, String fileName) {
    String txtFile = filePath + fileName;
    String pdfFile = filePath + FilenameUtils.removeExtension(fileName) + ".pdf";

    File attachmentLocation = new File(filePath);
    if (!attachmentLocation.exists()) {
        attachmentLocation.mkdirs();
    }

    makePDF(pdfFile, getTextfromTXT(txtFile));

    new File(txtFile).delete();

    return FilenameUtils.getName(pdfFile);
}

From source file:com.fileOperations.ImageToPDF.java

/**
 * create PDF from image and scales it accordingly to fit in a single page
 *
 * @param folderPath String/*from ww  w.j  a va 2s  .co m*/
 * @param imageFileName String
 * @return String new filename
 */
public static String createPDFFromImage(String folderPath, String imageFileName) {
    String pdfFile = FilenameUtils.removeExtension(imageFileName) + ".pdf";
    String image = folderPath + imageFileName;
    PDImageXObject pdImage = null;
    File imageFile = null;
    FileInputStream fileStream = null;
    BufferedImage bim = null;

    File attachmentLocation = new File(folderPath);
    if (!attachmentLocation.exists()) {
        attachmentLocation.mkdirs();
    }

    // the document
    PDDocument doc = null;
    PDPageContentStream contentStream = null;

    try {
        doc = new PDDocument();
        PDPage page = new PDPage(PDRectangle.LETTER);
        float margin = 72;
        float pageWidth = page.getMediaBox().getWidth() - 2 * margin;
        float pageHeight = page.getMediaBox().getHeight() - 2 * margin;

        if (image.toLowerCase().endsWith(".jpg")) {
            imageFile = new File(image);
            fileStream = new FileInputStream(image);
            pdImage = JPEGFactory.createFromStream(doc, fileStream);
            //            } else if ((image.toLowerCase().endsWith(".tif")
            //                    || image.toLowerCase().endsWith(".tiff"))
            //                    && TIFFCompression(image) == COMPRESSION_GROUP4) {
            //                imageFile = new File(image);
            //                pdImage = CCITTFactory.createFromFile(doc, imageFile);
        } else if (image.toLowerCase().endsWith(".gif") || image.toLowerCase().endsWith(".bmp")
                || image.toLowerCase().endsWith(".png")) {
            imageFile = new File(image);
            bim = ImageIO.read(imageFile);
            pdImage = LosslessFactory.createFromImage(doc, bim);
        }

        if (pdImage != null) {
            if (pdImage.getWidth() > pdImage.getHeight()) {
                page.setRotation(270);
                PDRectangle rotatedPage = new PDRectangle(page.getMediaBox().getHeight(),
                        page.getMediaBox().getWidth());
                page.setMediaBox(rotatedPage);
                pageWidth = page.getMediaBox().getWidth() - 2 * margin;
                pageHeight = page.getMediaBox().getHeight() - 2 * margin;
            }
            Dimension pageSize = new Dimension((int) pageWidth, (int) pageHeight);
            Dimension imageSize = new Dimension(pdImage.getWidth(), pdImage.getHeight());
            Dimension scaledDim = PDFBoxTools.getScaledDimension(imageSize, pageSize);
            float startX = page.getMediaBox().getLowerLeftX() + margin;
            float startY = page.getMediaBox().getUpperRightY() - margin - scaledDim.height;

            doc.addPage(page);
            contentStream = new PDPageContentStream(doc, page);
            contentStream.drawImage(pdImage, startX, startY, scaledDim.width, scaledDim.height);
            contentStream.close();
            doc.save(folderPath + pdfFile);
        }
    } catch (IOException ex) {
        ExceptionHandler.Handle(ex);
        return "";
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException ex) {
                ExceptionHandler.Handle(ex);
                return "";
            }
        }
    }
    if (fileStream != null) {
        try {
            fileStream.close();
        } catch (IOException ex) {
            ExceptionHandler.Handle(ex);
            return "";
        }
    }
    if (bim != null) {
        bim.flush();
    }
    if (imageFile != null) {
        imageFile.delete();
    }
    return pdfFile;
}

From source file:edu.co.usbcali.ir.util.ExtractReutersNews.java

public void extractNewsFromXml(String path) {
    SAXBuilder builder = new SAXBuilder();
    File xmlFile = new File(path);

    String baseFileName = FilenameUtils.removeExtension(xmlFile.getName());
    String lineSeparator = System.getProperty("line.separator");

    try {/*ww  w. j av a  2s. com*/
        Document document = (Document) builder.build(xmlFile);
        Element rootNode = document.getRootElement();
        List listReuters = rootNode.getChildren("REUTERS");

        for (Object listElement : listReuters) {
            Element reuters = (Element) listElement;

            String newId = reuters.getAttributeValue("NEWID");
            String date = reuters.getChildText("DATE");

            List listText = reuters.getChildren("TEXT");
            Element text = (Element) listText.get(0);

            String title = text.getChildText("TITLE");
            String body = text.getChildText("BODY");

            String reuterContent = title + lineSeparator + date + lineSeparator + lineSeparator + body;
            String reuterPath = "reuters/" + baseFileName + "-" + newId + ".txt";

            WriteFile.writeFileContent(reuterContent, reuterPath);
        }
    } catch (JDOMException | IOException ex) {
        Logger.getLogger(ExtractReutersNews.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:androidassetsgenerator.AssetGenerator.java

public void scaleThumbnailator(File input, String fileName, int baseIndex, String outputDir)
        throws IOException {
    asset = ImageIO.read(input);//  w  w  w  . j a v  a 2 s. c o  m

    String file = FilenameUtils.removeExtension(fileName);
    String fileExtension = FilenameUtils.getExtension(fileName);

    System.out.println(baseIndex);

    BufferedImage outputLdpi, outputMdpi, outputHdpi, outputXhdpi, outputXxhdpi, outputXxxhdpi;

    switch (baseIndex) {
    // Write from ldpi to ldpi -> kind of useless
    case 0:
        writeOutput(asset, path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from mdpi to ldpi
    case 1:
        writeOutput(scale34(asset), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from hdpi to mdpi, ldpi
    case 2:
        outputMdpi = scale23(asset);
        // Write to mdpi
        writeOutput(outputMdpi, path(fileName, outputDir, Constants.DRAWABLE_MDPI));
        // Write to ldpi
        writeOutput(scale34(outputMdpi), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from xhdpi to hdpi, mdpi, ldpi
    case 3:
        outputHdpi = scale34(asset);
        outputMdpi = scale23(outputHdpi);
        // Write to hdpi
        writeOutput(outputHdpi, path(fileName, outputDir, Constants.DRAWABLE_HDPI));
        // Write to mdpi
        writeOutput(outputMdpi, path(fileName, outputDir, Constants.DRAWABLE_MDPI));
        // Write to ldpi
        writeOutput(scale34(outputMdpi), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from xxhdpi to xhdpi, hdpi, mdpi, ldpi
    case 4:
        outputXhdpi = scale23(asset);
        outputHdpi = scale34(outputXhdpi);
        outputMdpi = scale23(outputHdpi);
        // Write to xhdpi
        writeOutput(outputXhdpi, path(fileName, outputDir, Constants.DRAWABLE_XHDPI));
        // Write to hdpi
        writeOutput(outputHdpi, path(fileName, outputDir, Constants.DRAWABLE_HDPI));
        // Write to mdpi
        writeOutput(outputMdpi, path(fileName, outputDir, Constants.DRAWABLE_MDPI));
        // Write to ldpi
        writeOutput(scale34(outputMdpi), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    // Write from xxxhdpi to xxhdpi, xhdpi, hdpi, mdpi, ldpi
    case 5:
        outputXxhdpi = scale34(asset);
        outputXhdpi = scale23(outputXxhdpi);
        outputHdpi = scale34(outputXhdpi);
        outputMdpi = scale23(outputHdpi);
        // Write to xxhdpi
        writeOutput(outputXxhdpi, path(fileName, outputDir, Constants.DRAWABLE_XXHDPI));
        // Write to xhdpi
        writeOutput(outputXhdpi, path(fileName, outputDir, Constants.DRAWABLE_XHDPI));
        // Write to hdpi
        writeOutput(outputHdpi, path(fileName, outputDir, Constants.DRAWABLE_HDPI));
        // Write to mdpi
        writeOutput(outputMdpi, path(fileName, outputDir, Constants.DRAWABLE_MDPI));
        // Write to ldpi
        writeOutput(scale34(outputMdpi), path(fileName, outputDir, Constants.DRAWABLE_LDPI));
        break;
    }

}

From source file:com.fileOperations.TXTtoPDF.java

/**
 * creates PDF from text document/*from  w w  w.  ja  v  a2s .c o m*/
 *
 * @param filePath String
 * @param fileName String
 * @return String - new File Name
 */
public static String createPDFNoDelete(String filePath, String fileName) {
    String txtFile = filePath + fileName;
    String pdfFile = filePath + FilenameUtils.removeExtension(fileName) + ".pdf";

    File attachmentLocation = new File(filePath);
    if (!attachmentLocation.exists()) {
        attachmentLocation.mkdirs();
    }

    makePDF(pdfFile, getTextfromTXT(txtFile));
    return FilenameUtils.getName(pdfFile);
}

From source file:joachimeichborn.geotag.io.writer.kml.KmzWriter.java

public void write(final Track aTrack, final Path aOutputFile) throws IOException {
    final String documentTitle = FilenameUtils.removeExtension(aOutputFile.getFileName().toString());

    final GeoTagKml kml = createKml(documentTitle, aTrack);

    kml.marshalAsKmz(aOutputFile.toString());

    logger.fine("Wrote track to " + aOutputFile);
}

From source file:net.rwx.maven.asciidoc.backends.AsciidocBackendTransformation.java

public String getOutputFile(String inputName) throws IOException {

    String atomicName = FilenameUtils.removeExtension(inputName);
    return net.rwx.maven.asciidoc.utils.FileUtils.getAsciidocTemporaryPath(atomicName, extension);
}

From source file:com.ariht.maven.plugins.config.FileInfo.java

public FileInfo(final File file) {
    this.file = file;
    this.nameWithoutExtension = FilenameUtils.removeExtension(file.getName());
}