【发布时间】:2016-05-24 13:24:14
【问题描述】:
我想逐字节读取我的文件,我目前正在使用这个类来读取文件:
public class File {
public byte[] readingTheFile() throws IOException {
FileReader in = new FileReader("/Users/user/Desktop/altiy.pdf");
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
in.close();
return null;
}
} //close class
现在在我的主要方法所在的主类中,我试图读取文件,然后尝试将其作为参数传递给另一个类的另一个方法,如下所示:
public class myMainClass {
// some fields here
File f = new File ();
public static void main (String [] a) {
try {
byte[] secret = five.readingTheFile(); // We call the method which read the file
byte[][] shar = one.calculateThresholdScheme(secret, n,k);
// some other code here . Note n and k i put their values from Eclipse
} catch (IOException e) {
e.printStackTrace();
} // close catch
} // close else
} // close main
} // close class
现在在calculateThresholdScheme所在的班级中
public class performAlgorithm {
// some fields here
protected byte[][] calculateThresholdScheme(byte[] secret, int n, int k) {
if (secret == null)
throw new IllegalArgumentException("null secret");
// a lot of other codes below.
但是一旦我抛出这个 IllegalArgumentException("null secret"); 我的执行就会停止这意味着我的文件尚不可读。我想知道这里出了什么问题,但我仍然没有弄清楚
【问题讨论】:
-
也许是
readingTheFile()无论如何都返回null?也许,我的意思是肯定是这样的。 -
BufferedReader不是用于读取字节,而是用于读取文本。使用InputStream。
标签: java file bufferedreader