本文整理汇总了Java中java.io.FileInputStream.read方法的典型用法代码示例。如果您正苦于以下问题:Java FileInputStream.read方法的具体用法?Java FileInputStream.read怎么用?Java FileInputStream.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.FileInputStream
的用法示例。
在下文中一共展示了FileInputStream.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: copyFile
import java.io.FileInputStream; //导入方法依赖的package包/类
public static void copyFile(File f1, File f2) throws Exception {
if (!f2.getParentFile().exists()) {
if (!f2.getParentFile().mkdirs()) {
throw new Exception("Create file '" + f2.getName() + "' directory error !");
}
}
try {
int length = 2097152;
FileInputStream in = new FileInputStream(f1);
FileOutputStream out = new FileOutputStream(f2);
byte[] buffer = new byte[length];
while (true) {
int ins = in.read(buffer);
if (ins == -1) {
in.close();
out.flush();
out.close();
} else
out.write(buffer, 0, ins);
}
} catch (Exception e) {
}
}
示例2: readFile
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* readFile
*
* @param destination
* @return
* @throws IOException
*/
public static byte[] readFile(String destination) throws IOException {
File temp = new File(destination);
if (temp.exists()) {
FileInputStream fis = new FileInputStream(temp);
byte[] data = new byte[(int) temp.length()];
try {
fis.read(data);
}
finally {
fis.close();
}
return data;
}
return new byte[0];
}
示例3: compressGzipFile
import java.io.FileInputStream; //导入方法依赖的package包/类
public static String compressGzipFile(String file, String gzipFile) {
try {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(gzipFile);
GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer)) != -1){
gzipOS.write(buffer, 0, len);
}
//close resources
gzipOS.close();
fos.close();
fis.close();
System.out.println("A json.gz file was created: " + gzipFile);
} catch (IOException e) {
e.printStackTrace();
}
return gzipFile;
}
示例4: a
import java.io.FileInputStream; //导入方法依赖的package包/类
public static String a(File file) {
byte[] bArr = new byte[1024];
try {
if (!file.isFile()) {
return "";
}
MessageDigest instance = MessageDigest.getInstance(CommonUtils.MD5_INSTANCE);
FileInputStream fileInputStream = new FileInputStream(file);
while (true) {
int read = fileInputStream.read(bArr, 0, 1024);
if (read != -1) {
instance.update(bArr, 0, read);
} else {
fileInputStream.close();
BigInteger bigInteger = new BigInteger(1, instance.digest());
return String.format("%1$032x", new Object[]{bigInteger});
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例5: test3
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* 通过Unsafe的defineClass方法在内存中创建一个Class对象
*
* @throws Exception
*/
@Test
public void test3() throws Exception {
//读取Class文件
File file = new File("E:\\classes\\Test.class");
FileInputStream input = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
input.read(data);
input.close();
//创建Class对象
//Class clazz = unsafe.defineClass("Test", data, 0, data.length);//这个方法在Java 8 没了。
Class clazz = unsafe.defineClass("Test", data, 0, data.length, null, null);
//通过反射创建示例调用方法
Object instance = clazz.newInstance();
Method method = clazz.getMethod("say", null);
method.invoke(instance);//Hello
}
示例6: storeFile
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* 存储缓存文件 返回文件绝对路径
* @param file
* 要存储的文件
* @param type
* 文件的类型
* IMAGE = "imgae"; //图片
* VIDEO = "video"; //视频
* VOICE = "voice"; //语音
* = "voice"; //语音
* @return 存储文件的绝对路径名
* 若SDCard不存在返回null
*/
public static String storeFile(File file, String type) {
if(!hasSDCard()) {
return null;
}
String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
byte[] data = null;
try {
FileInputStream in = new FileInputStream(file);
data = new byte[in.available()];
in.read(data, 0, data.length);
in.close();
} catch (IOException e) {
Log.e(TAG, "storeFile try { FileInputStream in = new FileInputStream(file); ... >>" +
" } catch (IOException e) {\n" + e.getMessage());
}
return storeFile(data, suffix, type);
}
示例7: calculateDigest
import java.io.FileInputStream; //导入方法依赖的package包/类
public static Map<String, Object> calculateDigest(File file) throws IOException {
Map<String, Object> map = new HashMap<String, Object>();
FileInputStream is = new FileInputStream(file);
long size = 0;
String digest = StringUtils.EMPTY;
try {
MessageDigest md = MD5Utils.getDigest();
byte[] buffer = new byte[1024];
while (true) {
int len = is.read(buffer);
if (len < 0) {
break;
}
size += len;
md.update(buffer, 0, len);
}
byte[] digests = md.digest();
digest = MD5Utils.encodeHexString(digests);
map.put("size", size);
map.put("digest", digest);
} finally {
is.close();
}
return map;
}
示例8: read
import java.io.FileInputStream; //导入方法依赖的package包/类
public void read(FileInputStream fs) throws IOException {
final int bflen = 14;
final byte bf[] = new byte[bflen];
fs.read(bf, 0, bflen);
final int bilen = 40;
final byte bi[] = new byte[bilen];
fs.read(bi, 0, bilen);
iSize = constructInt(bf, 2);
ibiSize = constructInt(bi, 2);
iWidth = constructInt(bi, 4);
iHeight = constructInt(bi, 8);
iPlanes = constructShort(bi, 12);
iBitcount = constructShort(bi, 14);
iCompression = constructInt(bi, 16);
iSizeimage = constructInt(bi, 20);
iXpm = constructInt(bi, 24);
iYpm = constructInt(bi, 28);
iClrused = constructInt(bi, 32);
iClrimp = constructInt(bi, 36);
}
示例9: zipIt
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Zip it
*/
public void zipIt(){
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream(OUTPUT_ZIP_FILE);
ZipOutputStream zos = new ZipOutputStream(fos);
//System.out.println("Output to Zip : " + zipFile);
for(String file : this.fileList){
//System.out.println("File Added : " + file);
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER.getAbsolutePath() + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
//System.out.println("Done");
} catch (IOException ex) {
ex.printStackTrace();
}
}
示例10: uploadAudio
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Request to upload the specified audio for speech recognition.
*
* @param identifier - Identifier CookieSet.
* @param filePath - The path of the audio file.
* Wave Header is required if it is a Wave audio.
* @param audioType - Audio type:
* AUDIO_TYPE_PCM_RAW for PCM raw data.
* AUDIO_TYPE_PCM_WAVE for Wave audio.
* @param isFinalAudio - TRUE if this is the last audio of a speech input.
* @return API response with the audio uploading status.
* @throws IOException File handling or HTTP connection failed, or other exceptions.
* @throws NoSuchAlgorithmException Failed to create signature.
*/
public APIResponse uploadAudio(
CookieSet identifier,
String filePath,
int audioType,
boolean isFinalAudio
) throws IOException, NoSuchAlgorithmException {
File file = new File(filePath);
if (file == null || (!file.exists())) {
throw new FileNotFoundException("File not found: " + filePath);
}
FileInputStream fileIn = new FileInputStream(file);
byte[] fileData = new byte[(int) file.length()];
fileIn.read(fileData);
fileIn.close();
return uploadAudio(identifier, fileData, audioType, isFinalAudio);
}
示例11: PackDirectory
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Packs all files in the given directory into the given ZIP stream.
* Also recurses down into subdirectories.
*/
public static void PackDirectory( String startingDirectory,
File theDirectory,
ZipOutputStream theZIPStream )
throws java.io.IOException
{
File[] theFiles = theDirectory.listFiles();
File stDirectory = new File(startingDirectory);
System.out.println("Path="+stDirectory.getPath()+";length="+stDirectory.getPath().length() + "==>"+theFiles[0]);
int j = stDirectory.getPath().length();
for ( int i=0 ; i<theFiles.length ; i++ )
{
String sRelPath = theFiles[i].getPath().substring(j);
if ( theFiles[i].isDirectory() )
{
// create a directory entry.
// directory entries must be terminated by a slash!
ZipEntry theEntry = new ZipEntry("."+sRelPath+"/" );
theZIPStream.putNextEntry(theEntry);
theZIPStream.closeEntry();
// recurse down
PackDirectory( startingDirectory, theFiles[i], theZIPStream );
}
else // regular file
{
File f = theFiles[i];
ZipEntry ze = new ZipEntry("."+sRelPath);
FileInputStream in = new FileInputStream(f);
byte[] data = new byte[(int) f.length()];
in.read(data);
in.close();
theZIPStream.putNextEntry(ze);
theZIPStream.write(data);
theZIPStream.closeEntry();
}
}
}
示例12: writeToFromFile
import java.io.FileInputStream; //导入方法依赖的package包/类
private void writeToFromFile(OutputStream os, RequestParams.FileWrapper wrapper)
throws IOException {
// Send the meta data.
writeMetaData(os, wrapper.file.getName(), wrapper.contentType);
int bytesRead, bytesWritten = 0, totalSize = (int) wrapper.file.length();
// Open the file for reading.
FileInputStream in = new FileInputStream(wrapper.file);
// Upload the file's contents in Base64.
Base64OutputStream bos =
new Base64OutputStream(os, Base64.NO_CLOSE | Base64.NO_WRAP);
// Read from file until no more data's left to read.
while ((bytesRead = in.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
bytesWritten += bytesRead;
progressHandler.sendProgressMessage(bytesWritten, totalSize);
}
// Close the Base64 output stream.
AsyncHttpClient.silentCloseOutputStream(bos);
// End the meta data.
endMetaData(os);
// Safely close the input stream.
AsyncHttpClient.silentCloseInputStream(in);
}
示例13: readBlob
import java.io.FileInputStream; //导入方法依赖的package包/类
private byte[] readBlob(FileInputStream in) throws IOException {
int length = readInteger(in);
byte[] blobBytes = new byte[length];
in.read(blobBytes, 0, blobBytes.length);
return blobBytes;
}
示例14: verifyMac
import java.io.FileInputStream; //导入方法依赖的package包/类
private void verifyMac(File file, Mac mac, Optional<byte[]> theirDigest)
throws FileNotFoundException, InvalidMacException
{
try {
MessageDigest digest = MessageDigest.getInstance("SHA256");
FileInputStream fin = new FileInputStream(file);
int remainingData = Util.toIntExact(file.length()) - mac.getMacLength();
byte[] buffer = new byte[4096];
while (remainingData > 0) {
int read = fin.read(buffer, 0, Math.min(buffer.length, remainingData));
mac.update(buffer, 0, read);
digest.update(buffer, 0, read);
remainingData -= read;
}
byte[] ourMac = mac.doFinal();
byte[] theirMac = new byte[mac.getMacLength()];
Util.readFully(fin, theirMac);
if (!MessageDigest.isEqual(ourMac, theirMac)) {
throw new InvalidMacException("MAC doesn't match!");
}
byte[] ourDigest = digest.digest(theirMac);
if (theirDigest.isPresent() && !MessageDigest.isEqual(ourDigest, theirDigest.get())) {
throw new InvalidMacException("Digest doesn't match!");
}
} catch (IOException | ArithmeticException e1) {
throw new InvalidMacException(e1);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}
示例15: getApiFileCache
import java.io.FileInputStream; //导入方法依赖的package包/类
public static String getApiFileCache(Context context, String cacheName) {
if (context == null || TextUtils.isEmpty(cacheName)) {
return null;
}
FileInputStream fis = null;
try {
fis = context.openFileInput(cacheName);
if (fis != null) {
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
String str = new String(buffer);
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return str;
} else if (fis == null) {
return null;
} else {
try {
fis.close();
return null;
} catch (Exception e2) {
e2.printStackTrace();
return null;
}
}
} catch (Exception e22) {
e22.printStackTrace();
if (fis == null) {
return null;
}
try {
fis.close();
return null;
} catch (Exception e222) {
e222.printStackTrace();
return null;
}
} catch (Throwable th) {
if (fis != null) {
try {
fis.close();
} catch (Exception e2222) {
e2222.printStackTrace();
}
}
}
}