2015-11-22 48 views
0

我写了下面的代码:java.lang.Exception.file正确创建

import java.io.IOException; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileInputStream; 
public class Steerable { 
    public static byte[] aReinclude = {97, 98, 99, 100, 101, 102, 103, 104, 105, 
49, 45, 50, 51, 52}; 
    public static long mCopatron(String name) 
     throws IOException, FileNotFoundException { 
     int k; 
     byte[] c = new byte[1126]; 
     long d = 0; 
     try { 
      String path = 
       "C:\\Users\\KingOfTheFeed\\Desktop\\java_programming\\exercises\\"; 
      FileOutputStream f = new FileOutputStream(path + name); 
      for (int i = 0; i < 1126; i++) { 
       k = (int) Math.random() * 9; 
       c[i] = aReinclude[k]; 
       if (i < 645) { 
        d = d + c[i]; 
       } 
      } 
      f.write(c); 
      f.close(); 
      return d; 
     } catch (IOException e) { 
      System.err.println("Error printing byte"); 
     } finally { 
      return d; 
     } 

    } 

    public static int[] mGentrice(String name1) 
     throws IOException, FileNotFoundException { 
     String path = 
      "C:\\Users\\KingOfTheFeed\\Desktop\\java_programming\\exercises\\"; 
     FileInputStream c = new FileInputStream(path + name1); 
     int k = 0; 
     int[] pin = new int[8]; 
     for (int i = 0; i < 8; i++) { 
      pin[i] = 0; 
     } 
     byte[] b = new byte[c.available()]; 
     pin[0] = c.available(); 
     c.read(b); 
     for (int i = 0; i < pin[0]; i++) { 
      k = b[i]; 
      switch(k) { 
       case 97: 
        pin[1]++; 
        break; 
       case 98: 
        pin[2]++; 
        break; 
       case 99: 
        pin[3]++; 
        break; 
       case 100: 
        pin[4]++; 
        break; 
       case 101: 
        pin[5]++; 
        break; 
       case 102: 
        pin[6]++; 
        break; 
       case 103: 
        pin[7]++; 
        break; 
       case 104: 
        pin[8]++; 
        break; 
       case 105: 
        pin[9]++; 
        break; 
       case 49: 
        pin[10]++; 
        break; 
       case 45: 
        pin[11]++; 
        break; 
       case 50: 
        pin[12]++; 
        break; 
       case 51: 
        pin[13]++; 
        break; 
       case 52: 
        pin[14]++; 
        break; 
      } 
     } 
     return pin; 
    } 
} 

它没有compillation错误,但jarpeb显示了这个消息: 错误:java.lang.Exception的:没有创建正确的文件mCopatron方法(错误的大小,应该是1126字节)

任何想法?

+1

你确定创建了文件,如file.txt和正确的路径正确的扩展? – Abdelhak

+1

请编辑您的文章并添加运行程序时生成的完整堆栈跟踪。 –

+0

好吧,我改变了路径,现在jarpeb显示这个消息:java.lang.ArrayIndexOutOfBoundsException:8 – user5510402

回答

0

销是大小的阵列8

int[] pin = new int[8]; 

所以只能访问引脚[0]到引脚[7]。这条线会抛出ArrayIndexOutOfBoundsException:

pin[8]++;