This question already has answers here:
Inserting text into an existing file via Java
(8 answers)
Closed 6 years ago.
In my project, we are writing a file using DataOutputStream. We are writing different data types like short, byte, int and long and we are using respective methods in DataOutputStream like writeShort(), writeByte() etc.
Now, I want to edit one record in this file at a particular offset. I know the offset from which that record starts but I am not sure what is the right approach of writing to the file because only method in DataOutputStream supporting offset is the one which takes byte[].
I want to write the whole record which is a combination of different data types as mentioned above.
Can someone please tell me what is the correct approach for this?
In your case, you should use RandomAccessFile in order to read and/or write some content in a file at a given location thanks to its method seek(long pos).
For example:
try (RandomAccessFile raf = new RandomAccessFile(filePath, "rw")) {
raf.seek(offset);
// do something here
}
NB: The methods writeShort(), writeByte() etc. and their read counterparts are directly available from the class RandomAccessFile so using it alone is enough.
Related
This question already has answers here:
writeBytes(str) vs write(str) in DataOutputStream
(2 answers)
Closed 3 years ago.
When using DataOutputStream to push Strings, I normally do the following:
DataOutputStream dout;
String str;
dout.write(str.getBytes());
I just came across the writeBytes() method of DataOutputStream, and my question is whether the above is equivalent to:
dout.writeBytes(str);
If not, what is difference and when should it be used?
No, it is not equivalent.
The Javadocs for writeBytes say
Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits.
So this will not work well except for ASCII Strings.
You should be doing
dout.write(str.getBytes(characterSet));
// remember to specify the character set, otherwise it become
// platform-dependent and the result non-portable
or
dout.writeChars(str);
or
dout.writeUTF(str);
Note that only the last method also writes the length of the String, so for the others, you probably need to know exactly what you are doing if you intend to later read it back.
The bigger question is why you need to use such a low-level protocol as DataOutputStream directly.
This question already has answers here:
Should I use DataInputStream or BufferedInputStream
(7 answers)
Closed 5 years ago.
I'm confused the above mentioned classes. When to use what? From my perspective every thing that comes in, is in the form of stream in java right? so which one is to use in what case to make the input more efficient? Also answer please can I use DataInputStream or BufferedInputStream in case of reading content from files?
FileInputStream
Is used for reading from files.
See the JavaDoc:
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. [...]
DataInputStream
Is used for reading in primitive Java types (that you might have written using a DataOutputStream) and provides convenience methods for that purpose, e.g. writeInt().
See the JavaDoc:
A data input stream lets an application read primitive Java data types
from an underlying input stream in a machine-independent way. [...]
BufferedInputStream
Is used to do buffered block reads from an InputStream (instead of single bytes) and increases performance if reading small chunks of data. Most of the time you want to use it for text processing.
See the JavaDoc:
A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input[...].
Of course you can combine those as they are following the Decorator Pattern.
Example of writing primitive Java types to a file:
FileOutputStream write = new FileOutputStream
DataOutputStream out = new DataOutputStream(write);
out.writeInt(10);
write.close();
This question already has answers here:
What's the difference between InputStream and ByteArrayInputStream?
(4 answers)
Closed 6 years ago.
What is the difference between FileInputStream and ByteArrayInputStream?
When we should use them?
What is the benefits of using one over other
Streams
byte oriented stream (8 bit)
good for binary data such as a Java .class file, images etc.
good for "machine-oriented"
Readers/Writers
char (utf-16) oriented stream (16 bit)
one character at a time
good for text such as a Java source
good for "human-oriented" data
Buffered
many bytes/characters at a time
always useful unless proven otherwise
Both ByteArrayInputStream and FileInputStream serve the same purpose i.e. reading binary data and both implement a common abstract superclass InputStream.So it is really tough find out a sharp contrast among the two.But common logic dictates that arrays are already present in the memory hence they can be accessed much faster as compared to files which are present in the file system. Also if you go through the documentation of the two classes you will find out that read function of ByteArrayInputStream cannot block whereas the read function in FileInputStream blocks if no input is yet available.
So if you are confused about what to use when then think about these :
In what form my data is present, file or array?
Is speed really a requirement? If it is, then go for ByteArray. But remember you cannot store too much information in ByteArray as they are present in the memory.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to append text to an existing file in Java
I ave a file already made in C:\myfile.txt and it has some data in it.. Now when I try to use File files = new File("C:\myfile.txt"); , It overwrites th orignal data and inserts the new data in it.. Is there a mechanism to aviod overwriting of old data?...
You could use append mode in one of the file writer classes:
FileWriter writer = new FileWriter("myfile.txt", true);
use the method new FileOutputStream(File,true) to append to an existing file.
Provide true in the constructor to append the file as told above
For more detailed control, use RandomAccessFile
This question already has answers here:
How can I upload files to a server using JSP/Servlet?
(14 answers)
Closed 3 years ago.
i'm writing a servlet that receives a xml file, gives it to another class and gives a html file with some comments back to the client.
I'm getting the input-xml with something like:
input = request.getInputStream();
but this input is a ServletInputStream and the other class(for the comments) needs a FileInputStream.
If i give the XMLEventReader(in the other class) the ServletInputStream, i get a parsing error:
ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
And i think this is because of the servletinputstream instead of the fileinputstream.
Greetings
i hope somebody is able to help me:)
I think it's unlikely that the latter problem is due to it being a ServletInputStream. The parser shouldn't care about the source of the data.
I think it's rather more likely that the data in the input stream is incorrect.
Your class which currently requires FileInputStream should be refactored to work with InputStream if at all possible... otherwise you'll have to write the data to an actual file first, which is obviously not ideal.
you should read the data from the ServletInputStream, and dump it into a FileOutputStream. this way you can look at the data that is being sent, then test that data separately using FileInputStream with the class you described that needs it. my guess is that the same thing will happen as is happening now since like the previous poster suggested, the data is probably in the wrong format.
Okay, i am now a bit smarter:)
The problem is: In the ServletInputStream is at the beginning(and one line at the end) some header information (as content-type, etc..).
Is there a smart solution for cutting this information?
greetings