本文整理匯總了Java中org.apache.commons.io.input.BOMInputStream.hasBOM方法的典型用法代碼示例。如果您正苦於以下問題:Java BOMInputStream.hasBOM方法的具體用法?Java BOMInputStream.hasBOM怎麽用?Java BOMInputStream.hasBOM使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.io.input.BOMInputStream
的用法示例。
在下文中一共展示了BOMInputStream.hasBOM方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getFileContent
import org.apache.commons.io.input.BOMInputStream; //導入方法依賴的package包/類
/**
* Get content from {@link java.nio.file.Path} using UTF8
*
* @param path
* @return
* @throws CommandException
*/
public String getFileContent(Path path) throws CommandException {
try {
File file = path.toFile();
BOMInputStream inputStream = new BOMInputStream(FileUtils.openInputStream(file), false, boms);
String fileContent;
if (inputStream.hasBOM()) {
fileContent = IOUtils.toString(inputStream, inputStream.getBOMCharsetName());
} else {
fileContent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
return fileContent;
} catch (IOException e) {
throw new CommandException("Cannot get file content for path: " + path.toString(), e);
}
}
示例2: openCsvListReader
import org.apache.commons.io.input.BOMInputStream; //導入方法依賴的package包/類
/**
* Opens a CSV file.
*
* If the given file ends with "gz", then the file is decompressed before using a {@link GZIPInputStream}.
*
* @param importFile
* the csv file
* @return a list reader
* @throws IOException
* on io exception
*/
@SuppressWarnings("resource")
protected CsvListReader openCsvListReader(final File importFile) throws IOException {
// Open file
InputStream fileStream = new FileInputStream(importFile);
// Check for compressed file
if (importFile.getName().toLowerCase().endsWith(".gz")) {
fileStream = new GZIPInputStream(fileStream);
}
// Guess the encoding
final BOMInputStream inputStream = new BOMInputStream(fileStream, false, ByteOrderMark.UTF_8,
ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);
final String charset;
if (inputStream.hasBOM()) {
charset = inputStream.getBOMCharsetName();
log.info("BOM detected. Using {} as encoding", charset);
} else {
charset = getDefaultEncoding().toString();
log.info("No BOM detected. Assuming {} as encoding", charset);
}
final Reader reader = new InputStreamReader(inputStream, charset);
return new CsvListReader(reader, new CsvPreference.Builder(CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE)
.skipComments(new CommentMatches("(//|/\\*|#|;).*")).build());
}
示例3: writeFileContent
import org.apache.commons.io.input.BOMInputStream; //導入方法依賴的package包/類
/**
* Writes the content into a file using same format as source file
*
* @param content content to be written
* @param path path to the file
* @param sourceFileMatch
* @throws CommandException
*/
public void writeFileContent(String content, Path path, FileMatch sourceFileMatch) throws CommandException {
try {
File outputFile = path.toFile();
BOMInputStream inputStream = new BOMInputStream(FileUtils.openInputStream(sourceFileMatch.getPath().toFile()), false, boms);
if (inputStream.hasBOM()) {
FileUtils.writeByteArrayToFile(outputFile, inputStream.getBOM().getBytes());
FileUtils.writeByteArrayToFile(outputFile, content.getBytes(inputStream.getBOMCharsetName()), true);
} else {
FileUtils.writeStringToFile(outputFile, content, StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new CommandException("Cannot write file content in path: " + path.toString(), e);
}
}
示例4: loadFile
import org.apache.commons.io.input.BOMInputStream; //導入方法依賴的package包/類
private void loadFile(File file) {
try (FileInputStream inputStream = new FileInputStream(file)) {
BOMInputStream bomIn = new BOMInputStream(inputStream, false,
ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE,
ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE);
String charsetName;
if (bomIn.hasBOM()) {
bom = bomIn.getBOM();
charsetName = bom.getCharsetName();
} else {
// No BOM found
bom = null;
charsetName = null;
}
String fileContents = StringTools.readStream(bomIn, charsetName);
textArea.setText(fileContents);
currentFile = file;
updateTitle();
if(recentFiles.contains(file.getAbsolutePath())) {
recentFiles.remove(file.getAbsolutePath());
}
recentFiles.add(file.getAbsolutePath());
localStorage.saveProperty("recentFiles", recentFiles);
updateRecentFilesMenu();
} catch (IOException e) {
Tools.showError(e);
}
}
示例5: getInputStreamReader
import org.apache.commons.io.input.BOMInputStream; //導入方法依賴的package包/類
private InputStreamReader getInputStreamReader(String filename, String encoding) throws IOException {
String charsetName = encoding != null ? encoding : Charset.defaultCharset().name();
InputStream is = System.in;
if (!isStdIn(filename)) {
is = new FileInputStream(new File(filename));
BOMInputStream bomIn = new BOMInputStream(is, true, ByteOrderMark.UTF_8,
ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE,
ByteOrderMark.UTF_32BE,ByteOrderMark.UTF_32LE);
if (bomIn.hasBOM() && encoding == null) {
charsetName = bomIn.getBOMCharsetName();
}
is = bomIn;
}
return new InputStreamReader(new BufferedInputStream(is), charsetName);
}