Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have PCM samples in a short array. What is the best way to play this out?
The format is 8000Hz, Mono, 16 bit, big endian.
(The PCM samples are generated in the code and not read through some file)
Thanks
With the javax.sound.sampled package it's pretty much straightforward, but you have to use some boilerplate.
Here's a good tutorial on that: www.wikijava.org/wiki/Play_a_wave_sound_in_Java
Basically you have to create an InputStream from your array and use that to create an AudioInputStream. There you have to specify the format of your audio data.
Then you open an output stream (SourceDataLine) and copy the bytes from the audio stream into that stream.
Check out this article - http://download.oracle.com/javase/tutorial/sound/playing.html.
More specifically, read about SourceDataLine and how to set up AudioFormat.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I was trying to convert a ".jpg" image to binary and then change its binary value to hide some data. But couldn't find anything. Any ideas anyone?
If I understand the question correctly, you want to get the single bytes of the jpg-file, which can be read with a DataInputStream:
File imageFile;
DataInputStream dis = new DataInputStream(new FileInputStream(imageFile));
int input = dis.read();
dis.close();
input then holds the first byte of the file, if you invoke read again (before dis.close()), you can read the subsequent bytes. Next, you would have to manipulate them and finally, you can write them to this or another file with a DataOutputStream that works just like the corresponding input stream. Just do NOT forget to close the streams after you are done reading or writing, so that system resources are freed and the files are closed. Otherwise the written data could be lost.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to read the content of a large file. For that I Googled on it and found so many methods and resources. But I'm still confused which is the method to read the large files (Factors need to be consider in my case are memory allocation, performance, large file )
Using FileChannel
using Files.readAllLines
using BufferedReader
Can anyone guide on this?
Your best option is to read the file lazily. Fetch each line one at a time and process.
Example:-
Stream<String> lines = Files.lines(Paths.get("C:/files", "yourfile.txt"));
Then process the lines afterwords.
From the official documentation:-
public static Stream<String> lines(Path path, Charset cs) throws IOException
Read all lines from a file as a Stream. Unlike readAllLines, this
method does not read all lines into a List, but instead populates
lazily as the stream is consumed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I always get confused when to process my input data how, which process. Different times i find different solutions. I am also not clear about their Hierarchy.
InputStream is parent class of all input streams and readers. Classes that have Stream keyword will work with bytes whereas classes which have Reader keyword will work with characters.
Buffer is wrapper around these streams to decrease the system calls and increase performance and speed of reading. Non buffered streams return single byte each time whereas Bufferd stream will not return until the buffer gets full. For example if you take BufferedReader you can read a whole line using readLine() but in non buffered stream you must read single character using read() method.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have several matrices in Java that I would like to transfer to Python as efficiently as possible, without requiring anything but standard libraries on both the Java and Python sides.
Currently I serialize them to file using the writeDouble function to write the entries out one by one, and writeInt to write the dimensions of the matrices. Now I would like to read these matrices back into Python. I can get the integers using struct.unpack, but Java's serialization of doubles does not correspond to an algorithm that struct.unpack can implement.
How can I decode a Java double in the binary format that writeDouble uses? I have trouble even finding a specification for the encoding that writeDouble uses.
You're overengineering it; DataOutputStream.writeDouble() and related methods are for manually serializing a Java Object, so it can be re-read as a Java Object. If all you need is to transfer data, you can simply write them out as text (or bytes), then read them back in. Common formats are CSV, JSON, XML, and ProtoBuf.
If all you're doing is trying to transfer a list of doubles, you can even just write them out one per line, and read them right back in with Python.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to write my own desktop keyboard, that can be used to work with some programs like excel. I've already written all graphical stuff, but i can't find any information about redirecting program output to another program. I found Apache POI, but it can be used only to work with Microsoft documents. Is there, for example a buffer, where can I send my letters or chars in ASCII? And next to be shown in my document or note?
Is there, for example a buffer, where can I send my letters or chars in ASCII?
Yes. It's called the Clipboard. You can call Toolkit.getDefaultToolkit() and then use Toolkit.getSystemClipboard() to get it.
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents("Some ASCII Text", this);
Note that this is the same buffer used for copy (ctrl-c) and paste (ctrl-v) (or their platform specific counterparts).