Java를 사용하여 먼저 문자열 시퀀스를 작성한 다음 바이트 시퀀스를 파일에 기록하려고합니다. 바이트 배열 때문에 FileOutputStream
을 사용하기 시작했습니다. API를 검색 한 후 FileOutputStream
은 String
을 쓸 수없고 int
과 byte
s이므로 DataOutputStream
으로 전환 함을 깨달았습니다. 프로그램을 실행하면 예외가 발생합니다. 왜?어떻게 문자열의 시퀀스를 작성한 다음 파일에 바이트 배열을 쓸 수 있습니까?
여기 내 코드의 일부입니다 :
라인try {
// Create the file
FileOutputStream fos;
DataOutputStream dos; // = new DataOutputStream("compressedfile.ecs_h");
File file= new File("C:\\MyFile.txt");
fos = new FileOutputStream(file);
dos=new DataOutputStream(fos);
/* saves the characters as a dictionary into the file before the binary seq*/
for (int i = 0; i < al.size(); i++) {
String name= al.get(i).name; //gets the string from a global arraylist, don't pay attention to this!
dos.writeChars(name); //saving the name in the file
}
System.out.println("\nIS SUCCESFULLY WRITTEN INTO FILE! ");
dos.writeChars("><");
String strseq;
/*write all elements from the arraylist into a string variable*/
strseq= seq.toString();
System.out.println("sTringSeq: " + strseq);
/*transpose the sequence string into a byte array*/
byte[] data = new byte[strseq.length()/8];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) Integer.parseInt(strseq.substring(i * 8, (i + 1) * 8), 2);
dos.write(data[i]);
}
dos.flush();
//Close the output stream
dos.close();
} catch(Exception e){}
예외가 포함 된 질문을 작성할 때마다 예외가 무엇인지 * 알려주십시오. –
미안하지만 나는 모른다. 나는 try catch를 사용하고있다! – elena
문제는 빈 캐치 블록을 사용하고 있다는 것입니다. 나쁜 생각입니다. 예외가 무엇인지 알기 위해 스택 추적을 인쇄해야합니다. – javanna