import java.io.*;
public class Test {
  public static void main(String[] args) throws Exception {
    File sourcefile = new File("Test.txt");
    FileInputStream instream = new FileInputStream(sourcefile);
    int length = (int) sourcefile.length();
    byte[] sign = new byte[length];
    instream.read(sign, 0, length);
    instream.close();
    for (int i = 0; i < sign.length; i++) {
      System.out.print(sign[i]+",");
    }
  }
}

解决方案 »

  1.   

    byte[] buffer = new byte[16384];
    InputStream in = new FileInputStream(new File(filename));
    int byteCount;
    if ( (byteCount = in.read(buffer)) == -1 ) {
    //buffer is what you want
    } else {
             //buffer is not large enough
    }
    in.close();
      

  2.   

    为什么老是出这种情况:
    我在IE的地址栏里输入:“http://community.csdn.net/Expert/topic/3286/3286010.xml?temp=.6665308”,可是显示的是下面的结果:秋秋 一级(初级) user1 100 3286010 关于输入流问题(在线等,来就有分)--如果再没人回复我这辈子是不来这个论坛了--太伤心了 838032 qiuqiu_luck Java J2SE / 基础类 1 2004-08-18 12:55:53 100 0 5401 0 我要从一个文件输入流中获得一个字节数组。也就是说把输入流中的数据全都读进一个字节数组中,我最后要的就是这种形式:byte[] sign;我要代码! 三星堆五级(中级)user5100232821163286010527122ntzls0import java.io.*; public class Test { public static void main(String[] args) throws Exception { File sourcefile = new File("Test.txt"); FileInputStream instream = new FileInputStream(sourcefile); int length = (int) sourcefile.length(); byte[] sign = new byte[length]; instream.read(sign, 0, length); instream.close(); for (int i = 0; i < sign.length; i++) { System.out.print(sign[i]+","); } } }2004-08-18 13:06:00
      

  3.   

    public byte[] getFileByte(FileInputStream in,size){
    byte[] sign = new byte[length];
    in.read(sign, 0, size);
    in.close();
    return sign;}
      

  4.   

    谢谢大家的关心我是用一楼的方法,不过我验证时还是出错
    import java.security.*;
    import java.io.*;
    import javax.crypto.*;public class GengeratorPairsTest {
      public static void main(String []args){
        try {
          if(args[0].equals("-genkeypairs")){
            KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
            SecureRandom random = new SecureRandom();
            keygen.initialize(1024, random);
            KeyPair keypairs = keygen.generateKeyPair();
            PublicKey pubKey = keypairs.getPublic();
            PrivateKey priKey = keypairs.getPrivate();//PublicKey OutPut
            ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream(args[1]));
            out.writeObject(pubKey);
            out.flush();
            out.close();
    //PrivateKey outPut
            out = new ObjectOutputStream(
                new FileOutputStream(args[2]));
            out.writeObject(priKey);
            out.close();
          }else if(args[0].equals("-sign")){
            ObjectInputStream inKey = new ObjectInputStream(
                new FileInputStream(args[3]));
            PrivateKey priKey = (PrivateKey) inKey.readObject();
            inKey.close();
            InputStream inSigFile = new FileInputStream(args[1]);
            String sigFile = inSigFile.toString();
            inSigFile.close();
            Signature sig = Signature.getInstance("SHA1withDSA","BC");
            sig.initSign(priKey);
            sig.update(sigFile.getBytes());
            byte[] siged = sig.sign();
            OutputStream out = new FileOutputStream(args[2]);
            out.write(siged);
            out.close();
          }else if(args[0].equals("-verSign")){
            ObjectInputStream inKey = new ObjectInputStream(
                new FileInputStream(args[3]));
            PublicKey pubKey = (PublicKey) inKey.readObject();
            inKey.close();
            InputStream inSigFile = new FileInputStream(args[1]);
            int Ch;
          
    File sourcefile = new File(args[2]);
            FileInputStream instream = new FileInputStream(sourcefile);
            int length = (int) sourcefile.length();
            byte[] sign = new byte[length];
            instream.read(sign, 0, length);
            instream.close();
            
            Signature verSig = Signature.getInstance("SHA1withDSA","BC");
            verSig.initVerify(pubKey);
            while((Ch=inSigFile.read())!=-1){
              verSig.update( (byte) Ch);
            }
            inSigFile.close();
          if (verSig.verify(sign)) {
                System.out.println("Verify Success");
              }
              else {
                System.out.println("Verify Fail");
              }      }else if(args[0].equals("-help")){
            System.out.println(
                "用法:-genkeypairs publicKeyFileName privateKeyFileName!");
            System.out.println("-encrypt plainFile encryptFile PublicKey");
            System.out.println("-dscrypt encryptFile PrivateKey");
            System.out.println("-sign orgFile signFile PrivateKey");
            System.out.println("-versign orgFile signFile PublicKey");
              System.exit(0);
          }    }
        catch (IOException exception)
               {
                  exception.printStackTrace();
               }
               catch (GeneralSecurityException exception)
               {
                  exception.printStackTrace();
               }
               catch (ClassNotFoundException exception)
               {
                  exception.printStackTrace();
               }  }
     }
      

  5.   

    import java.io.File;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;/**
     * Created by IntelliJ IDEA.
     * User: Administrator
     * Date: 2004-8-18
     * Time: 13:33:15
     * To change this template use File | Settings | File Templates.
     */
    public class ReadByte {
        public static void main(String[] args) {
            try {
                byte[] result;
                File file = new File("ReadByte.java");
                FileChannel fc = new RandomAccessFile(file, "r").getChannel();
                ByteBuffer bf = ByteBuffer.allocate((int) fc.size());            fc.read(bf);
                result = bf.array();            for (int i = 0; i < result.length; i++) {
                    System.out.print((char) result[i]);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }顺便问下楼主受什么刺激了?
    为何"太伤心了"?