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.
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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a definition of a Stream such as: "They are wrappers around a data source, allowing us to operate with that data source and making bulk processing convenient and fast. "
Can someone give an example and just a basic explanation of how it works such that Stream makes "bulk processing convenient and fast"?
Thank you!
Files.newBufferedReader("/tmp/foo").lines().map(...)...collect(...);
// or
BufferedReader reader = Files.newBufferedReader("/tmp/foo");
Stream<String> stream = reader.lines();
Collection<String> result = stream.map(...)...collect(...);
Is a convenient way to process a text file using a Stream.
But the work of making it fast/efficient is being done by the BufferedReader, not the Stream.
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 8 years ago.
Improve this question
I was making a file-transfer program and it takes forever to transmit this 800kb test picture I made. Clearly I don't want to spend 30 minutes for every mb of data, so I was curious if there was any way to speed up my code or Java in general. I had heard about allocating extra RAM, but never looked into it.
If anyone can find a better algorithm for my code that would be appreciated, otherwise I may have to step to C++ or C and try again.
Server code block:
if(file.exists()){
for(long l=0;l<file.length();l++){
out.writeByte(fIS.read());
}
}
where fIS is the FileInputStream and out is the outgoing DataOutputStream. The rest of the code is basically sending lengths and a flag, but I can add if requested.
Client Code:
b = new byte[len];
for(int x=0;x<len;x++){
b[x] = dIS.readByte();
}
fOS.write(b);
where len is the read length of the file expected from the Server, b is the array of bytes that make up the file, dIS is the input stream from the server, and fOS is the FileOutputStream.
I also just attempted using Readers and readLine() with writeChars() and readChar(), but this seems to equate to about the same thing.
Reading a file a byte at a time and sending over the network one byte at a time is going to be slow in any language :)
Consider using a BufferedInputStream for reading and BufferedOutputStream for writing.
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.