웨이브 파일의 바이트를 배열로 읽고 싶습니다. 읽은 바이트 수는 웨이브 파일의 크기에 따라 다르므로 최대 크기가 1000000 인 바이트 배열을 만듭니다. 그러나 배열 끝에 빈 값이 표시됩니다. 그래서 동적으로 증가하는 배열을 만들고 싶었고 ArrayList가 해결책이라는 것을 알았습니다. 그러나 AudioInputStream 클래스의 read() 함수는 바이트를 바이트 배열로만 읽습니다! 대신 ArrayList에 값을 전달합니까?바이트의 ArrayList 만들기
답변
당신이 좋아하는 바이트의 배열을 가질 수 있습니다
List<Byte> arrays = new ArrayList<Byte>();
이 배열
Byte[] soundBytes = arrays.toArray(new Byte[arrays.size()]);
로 다시 변환하려면 (그런 다음
byte[]
에
Byte[]
를 변환하는 변환기를 작성해야합니다).
편집 : 당신은 List<Byte>
잘못을 사용하고, 당신이 얼마나 간단 ByteArrayOutputStream
와 AudioInputStream
을 읽을 보여 단지 것이다. IOException
frameSize
경우는 슬로우
AudioInputStream ais = ....;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read;
while((read = ais.read()) != -1) {
baos.write(read);
}
byte[] soundBytes = baos.toByteArray();
PS1
은 동일하지 않다. 따라서,과 같이, 데이터를 판독하는 바이트 버퍼를 사용
AudioInputStream ais = ....;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = ais.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
byte[] soundBytes = baos.toByteArray();
*****. java : 41 : 기호를 찾을 수 없습니다. 기호 : 메서드 읽기 (java.util.List
이것은 컴파일되지 않습니다 :'List
@ Joachim Sauer, 네 말이 맞아. Byte에서 Byte로 변환해야한다는 걸 잊어 버렸다. 답변 수정. –
ArrayList
는 용액 아니다 ByteArrayOutputStream 솔루션이다. ByteArrayOutputStream
바이트를 작성한 다음 toByteArray()
을 호출하여 바이트를 가져옵니다. 이 같은
in = new BufferedInputStream(inputStream, 1024*32);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] dataBuffer = new byte[1024 * 16];
int size = 0;
while ((size = in.read(dataBuffer)) != -1) {
out.write(dataBuffer, 0, size);
}
byte[] bytes = out.toByteArray();
뭔가 수행해야합니다 : 코드가 어떻게 보일지의
예
코드가 조금 잘못되면List<Byte> myBytes = new ArrayList<Byte>();
//assuming your javax.sound.sampled.AudioInputStream is called ais
while(true) {
Byte b = ais.read();
if (b != -1) { //read() returns -1 when the end of the stream is reached
myBytes.add(b);
} else {
break;
}
}
죄송합니다. 나는 잠시 동안 자바를하지 않았다. 그리고 여기 때마다 더 바이트를 읽어 그것을하는 또 다른 방법 :
int arrayLength = 1024;
List<Byte> myBytes = new ArrayList<Byte>();
while(true) {
Byte[] aBytes = new Byte[arrayLength];
int length = ais.read(aBytes); //length is the number of bytes read
if (length == -1) { //read() returns -1 when the end of the stream is reached
break; //or return if you implement this as a method
} else if (length == arrayLength) { //Array is full
myBytes.addAll(aBytes);
} else { //Array has been filled up to length
for (int i = 0; i < length; i++) {
myBytes.add(aBytes[i]);
}
}
}
당신이 잠시 동안 (사실) 루프 :
편집으로 구현하는 경우도
주의 두 read() 메소드는 모두 IOException을 던집니다. 이것은 처리기로 남겨져 있습니다!그는 'Byte'의'List'를 사용해서는 안되며 바이트 단위로 읽는 것은 매우 느립니다. – Kaj
그는 배열이 아닌'List'를 원합니다. 또한 청크로 읽는 대안적인 예를 제공했습니다. –
그는 크기를 알지 못하기 때문에'List '를 원합니다. 이것은 올바른 해결책입니다. 심지어 'List'도 사용하지 않습니다. –
- 1. ArrayList 만들기 및 항목 추가
- 2. 바이트의 LSB를 다른 바이트의 LSB로 바꾸는 방법
- 3. 바이트의 패턴을 결정하십시오.
- 4. NSString에서 보유한 바이트의 UIImage
- 5. 2048 바이트의 파일 읽기
- 6. ArrayList 또는 IList를 사용해야합니까?
- 7. 재귀, Arraylist
- 8. Collections -ArrayList
- 9. ArrayList 문제
- 10. 테이블에 arraylist
- 11. PHP에서 바이트의 배열을 정의하는 방법
- 12. Java에서 2 바이트의 값을 찾으십시오.
- 13. 소금없이 16 바이트의 AES 암호화
- 14. 8 바이트의 비트 단위 변환
- 15. ArrayList <String> NullPointerException
- 16. Blackberry JDE ArrayList?
- 17. Int SSIS의 Arraylist 변수
- 18. 모양의 ArrayList 정렬
- 19. ArrayList 및 List와의 차이점
- 20. ArrayList Search .net
- 21. 정렬 arraylist 오름차순
- 22. arrayadapter에 새 Arraylist 추가
- 23. 벡터 대 Collections.synchronizedList (ArrayList)
- 24. 문제 : ArrayList 크기가 늘어납니다
- 25. jsp에서 거대한 arraylist 표시
- 26. C#의 ArrayList
- 27. ArrayList 요소를 읽으십시오
- 28. ArrayList 관련 디버깅 질문
- 29. Java - 모듈의 ArrayList - 어떻게해야합니까
- 30. ArrayList 요소가 존재하는지 확인
바이트 배열로 수행 할 단계는 무엇입니까? 어쩌면 커다란 임시 버퍼가 필요하지 않을 수도 있습니다. – pcjuzer