필자는 문제의 파일이 DOS/MAC 또는 UNIX 줄 끝으로 포맷되었는지 여부를 판단하기 위해 다음 방법을 작성했습니다.파일 형식을 결정하는 방법은 무엇입니까? DOS/Unix/MAC
적어도 1 개의 분명한 문제가 있습니다. 1. 첫 번째 실행에서 EOL을 얻으려고합니다. 첫 번째 1000 바이트 내로 말하면됩니다. 이것은 일어날 수도 있고 아닐 수도 있습니다.
이 내용을 검토하고 코드를 강화하고보다 일반적인 것으로 만드는 개선 사항을 제안합니다.
감사합니다.
new FileFormat().discover(fileName, 0, 1000);
다음
public void discover(String fileName, int offset, int depth) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
FileReader a = new FileReader(new File(fileName));
byte[] bytes = new byte[(int) depth];
in.read(bytes, offset, depth);
a.close();
in.close();
int thisByte;
int nextByte;
boolean isDos = false;
boolean isUnix = false;
boolean isMac = false;
for (int i = 0; i < (bytes.length - 1); i++) {
thisByte = bytes[i];
nextByte = bytes[i + 1];
if (thisByte == 10 && nextByte != 13) {
isDos = true;
break;
} else if (thisByte == 13) {
isUnix = true;
break;
} else if (thisByte == 10) {
isMac = true;
break;
}
}
if (!(isDos || isMac || isUnix)) {
discover(fileName, offset + depth, depth + 1000);
} else {
// do something clever
}
}
FileReader a로 무엇을하고 계십니까? 오프셋은 무엇입니까? –
아무 것도. 그것을 삭제하는 것을 잊었습니다. – JAM