[英]How to read a file byte by byte using BufferReader class in java
我想读取一个一个字节一个字节的大文件,而我目前正在使用此类读取文件:
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()
:
这是退货单:
return null;
哪个-“上尉在这里”返回null
。 因此secret
为null
并抛出IllegalArgumentException
。
如果您绝对希望坚持使用BufferedReader
-solution,那么应该可以解决此问题:
byte[] readingTheFile(){
byte[] result = null;
try(BufferedReader br = new BufferedReader(new FileReader(path))){
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null)
sb.append(line).append(System.getProperty("line.separator"));
result = sb.toString().getBytes();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
一些一般建议:
BufferedReader
没有内置的读取文件的目的byte
的byte
反正。 例如'\\n'
将被忽略,因为您正在逐行阅读。 这可能会导致您在加载时破坏数据。 下一个问题:您只关闭了readingTheFile()
的FileReader
,而不是BufferedReader
。 始终关闭ToplevelReader,而不是基础的。 通过使用try-with-resources
,您可以节省很多工作,并且FileReader
编码错误的情况下保持FileReader
打开的危险。
如果要从文件读取字节,请改用FileReader
。 这样就可以将整个文件加载为byte[]
:
byte[] readingTheFile(){
byte[] result = new byte[new File(path).length()];
try(FileReader fr = new FileReader(path)){
fr.read(result , result.length);
}catch(IOException e){
e.printStackTrace();
}
return result;
}
或者,甚至更简单:使用java.nio
byte[] readingTheFile(){
try{
return Files.readAllBytes(FileSystem.getDefault().getPath(path));
}catch(IOException e){
e.printStackTrace();
return null;
}
}
如下所示更改类“文件”,这将为您提供所需的机制。 我修改了与您编写的相同的课程。
public class File {
public byte[] readingTheFile() throws IOException {
java.io.File file = new java.io.File("/Users/user/Desktop/altiy.pdf");
/*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();
*/
FileInputStream fin = null;
try {
// create FileInputStream object
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int) file.length()];
// Reads bytes of data from this input stream into an array of
// bytes.
fin.read(fileContent);
// returning the file content in form of byte array
return fileContent;
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading file " + ioe);
} finally {
// close the streams using close method
try {
if (fin != null) {
fin.close();
}
} catch (IOException ioe) {
System.out.println("Error while closing stream: " + ioe);
}
}
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.