當前位置: 首頁>>代碼示例>>Java>>正文


Java FileInputStream.read方法代碼示例

本文整理匯總了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) {
	}
}
 
開發者ID:venwyhk,項目名稱:i18n-maven-plugin,代碼行數:24,代碼來源:FileUtil.java

示例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];
}
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:26,代碼來源:IOHelper.java

示例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;
}
 
開發者ID:michael-hll,項目名稱:BigQueryStudy,代碼行數:21,代碼來源:GZipHelper.java

示例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;
    }
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:24,代碼來源:bu.java

示例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
}
 
開發者ID:lihengming,項目名稱:java-codes,代碼行數:22,代碼來源:UnsafeTest.java

示例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);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:32,代碼來源:DataKeeper.java

示例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;
}
 
開發者ID:hexiaohong-code,項目名稱:LoginCrawler,代碼行數:26,代碼來源:MD5Utils.java

示例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);
}
 
開發者ID:zylo117,項目名稱:SpotSpotter,代碼行數:21,代碼來源:BMPReader.java

示例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();   
    }
}
 
開發者ID:Panzer1119,項目名稱:JAddOn,代碼行數:28,代碼來源:JAppZip.java

示例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);
	
}
 
開發者ID:olami-developers,項目名稱:olami-java-client-sdk,代碼行數:36,代碼來源:SpeechRecognizer.java

示例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();           
      }
   }
}
 
開發者ID:ser316asu,項目名稱:SER316-Ingolstadt,代碼行數:42,代碼來源:ProjectPackager.java

示例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);
}
 
開發者ID:benniaobuguai,項目名稱:android-project-gallery,代碼行數:32,代碼來源:JsonStreamerEntity.java

示例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;
}
 
開發者ID:CableIM,項目名稱:Cable-Android,代碼行數:8,代碼來源:TextSecureSessionStore.java

示例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);
  }
}
 
開發者ID:XecureIT,項目名稱:PeSanKita-lib,代碼行數:37,代碼來源:AttachmentCipherInputStream.java

示例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();
            }
        }
    }
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:53,代碼來源:FileUtils.java


注:本文中的java.io.FileInputStream.read方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。