Java File Read by Charset fixture(String filename, Charset charset)

Here you can find the source of fixture(String filename, Charset charset)

Description

Reads fixture file from classpath, e.g src/test/resources and returns its content as string.

License

Apache License

Parameter

Parameter Description
filename the filename of the fixture file
charset the character set of filename

Exception

Parameter Description
IllegalArgumentException if an I/O error occurs.

Return

content of file as string

Declaration

private static String fixture(String filename, Charset charset) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.google.common.io.Resources;

public class Main {
    /**/*from w  w w  .ja  v a  2  s  . co m*/
     * Reads fixture file from classpath, e.g {@code src/test/resources}
     * and returns its content as UTF-8 string.
     *
     * @param filename the filename of the fixture file
     * @return content of file as UTF-8 string
     * @throws IllegalArgumentException if an I/O error occurs.
     */
    public static String fixture(String filename) {
        return fixture(filename, StandardCharsets.UTF_8);
    }

    /**
     * Reads fixture file from classpath, e.g {@code src/test/resources}
     * and returns its content as string.
     *
     * @param filename the filename of the fixture file
     * @param charset the character set of {@code filename}
     * @return content of file as string
     * @throws IllegalArgumentException if an I/O error occurs.
     */
    private static String fixture(String filename, Charset charset) {
        try {
            return Resources.toString(Resources.getResource(filename), charset);
        } catch (IOException ex) {
            throw new IllegalArgumentException(ex);
        }
    }
}

Related

  1. doTranseFileCharset(File srcFile, File destFile, String srcCharsetName, String destCharsetName)
  2. fileContent(File file, Charset charset)
  3. fileToString(final File f, final Charset c)
  4. fileToString(final Path path, final Charset charset)
  5. findPattern(File file, Charset charset, String patternString)
  6. getContent(File file, String charsetName)
  7. getEOL(File file, Charset charset)
  8. getFileContent(File file, String charsetName)
  9. getFileContents(File file, String charset)