두 가지 방법, 둘 다 FileInputStream 객체를 사용합니다. 첫 번째 값은 예상 값을 반환합니다. 이 방법은 잘 작동합니다. 그러나 두 번째 방법은 아무 것도 반환하지 않습니다. 두 번째 메소드에 전달 된 값이 null이 아닙니다. 메서드에 전달 된 파일의 16 진수 형식을 가져와야합니다. 왜 그렇습니까? 친절하게 설명하십시오. 여기 b
이후FileInputStream이 null을 반환합니다.
public String binaryFile1(File file1){
try{
stringBuilder1=new StringBuilder();
is1=new FileInputStream(file1);
while(b!=-1){
counter++;
b=is1.read();
String s = Integer.toHexString(b).toUpperCase();
if (s.length() == 1) {
stringBuilder1.append('0');
}
if(counter%5==0){
stringBuilder1.append(s).append("\n");
counter=0;
}else
stringBuilder1.append(s).append(' ');
}
is1.close();
}catch(Exception e){
e.printStackTrace();
}
return stringBuilder1.toString();
}
public String binaryFile2(File file2){
try{
stringBuilder2=new StringBuilder();
is2=new FileInputStream(file2);
while(b!=-1){
counter++;
b=is2.read(); //Here b does not get any content assigned.
String s = Integer.toHexString(b).toUpperCase();
if (s.length() == 1) {
stringBuilder2.append('0');
}
if(counter%5==0){
stringBuilder2.append(s).append("\n");
counter=0;
}else
stringBuilder2.append(s).append(' ');
}
is2.close();
}catch(Exception e){
e.printStackTrace();
}
return stringBuilder2.toString(); //Here stringBuilder2 is null
}
과 같이
binaryFile2
을 쓸 수 있습니다. 정확하게 목적은 무엇입니까? –null을 돌려 줄 수있는'FileInputStream' 메서드는 존재하지 않습니다. 여기서 유일한 문제는 잘못된 try/catch 구조입니다. – EJP
try-catch 구조가 제대로 작동하고 있습니다. Elliot Frisch가 문제를 올바르게 잡았습니다. –