I need to write a file stream to database. The file content must be readable only through
the program. Manual open file should not display the readable content. I decided to use
ObjectOutput stream as it is the binary writing mechanism in java. But I can see the string
content when I open the file.
Writing to stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject("HIIIIIIIIIIIIIIIIIIIIII HOW ARE YOU");
The created content is look like
’ t #HIIIIIIIIIIIIIIIIIIIIII HOW ARE YOU
How to get complete binary stream output?
The file content must be readable only through the program. Manual open file should not display the readable content.
So you need some security.
I decided to use ObjectOutput stream as it is the binary writing mechanism in java.
That's (a) a non sequitur, and (b) security by obscurity: i.e. it is no security at all.
You should use encryption.
Related
File file = new File(directory + player.getUsername() + ".dat");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream outFile = new FileOutputStream(file);
DataOutputStream write = new DataOutputStream(outFile);
write.writeUTF(player.getUsername());
write.writeUTF(player.getPassword());
write.writeInt(player.getStaffRights());
write.writeInt(player.getPosition().getX());
write.writeInt(player.getPosition().getY());
write.writeInt(player.getPosition().getZ());
write.writeInt(player.getGender());
Ok so pretty much what this code above does is it makes new character files for this game im working with. But the problem im having is that the character information that this code is putting into a .dat I cant read when I try and open in lets say notepad its just gibberish. I need to be able to open these .dats and be able to read/edit the text in english. Any help?
When you save data with a DataOutputStream, it will be saved in Java's native binary serialization format, not as plain text which you can read with for example Notepad.
If you want to write plain text to a file, use one of the subclasses of java.io.Writer to write to the file instead of DataOutputStream - for example PrintWriter.
PrintWriter out = new PrintWriter(file);
out.println(player.getUsername());
// etc...
// Also, don't forget to close when you are done
out.close();
Have the player object implement Serilizable and as long as all of its properties are serilizable as well, such as strings and ints, the serialization will be done.
Refer to the serialization tutorial:
Tutorial
I have to use a method to write a model, from an api.
mode.write(OutputStream out);
At the moment I am doing that to see my output on my console:
model.write(System.out);
However, I would like to specify a file path to write the output to a file.
Any recommendations how I could take this OutputStream and change it into a FileOutputStream to write it to a file?
I appreciate your answer!
model.write(new BufferedOutputStream(new FileOutputStream(file)));
This is for binary data. If text is wanted (as using System.out suggests), look whether there is a
model.write(Writer out);
Then use a Writer to convert java text (Unicode) to binary data (bytes) having some encoding.
You may also omit the encoding for the default platform encoding, i.e. for a computer local file.
String encoding = "UTF-8";
model.write(new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file), encoding)));
A FileOutputStream is an OutputStream, so you can simply do
model.write(new FileOutputStream("filename"));
Note that you will have to add some exception handling (FileOutputStream() can throw a FileNotFoundException).
In the web page, it is "Why don't we" as follows:
But when I parse the webpage and save it to a text file, it becomes this under eclipse:
Why don鈥檛 we
More information about my implementation:
The webpage is: utf-8
I use jSoup to parse, the file is saved as a txt.
I use FileWriter f = new FileWriter() to write to file.
UPDATE:
I actually solve the display problem in eclipse by changing eclipse's encoding to utf-8.
FileWriter is a utility class that uses the default current platform encoding. That is non-portable, and probably incorrect.
BufferedWriter f = new BufferedWriter(New OutputStreamWriter(
new FileOutputStream(file), StandardCharsets.UTF_9));
f,Write("\uFEFF"); // Redundant BOM character might be written to be sure
// the text is read as UTF-8
...
I'm retrieving a file from a FTP Server. The file is encoded as UTF-8
ftpClient.connect(props.getFtpHost(), props.getFtpPort());
ftpClient.login(props.getUsername(), props.getPassword());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
inputStream = ftpClient.retrieveFileStream(fileNameBuilder
.toString());
And then somewhere else I'm reading the input stream
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
But the file is not getting read as UTF-8 Encoded!
I tried ftpClient.setAutodetectUTF8(true); but still doesn't work.
Any ideas?
EDIT:
For example a row in the original file is
...00248090041KENAN SARÐIN 00000000015.993FAC...
After downloading it through FTPClient, I parse it and load in a java object, one of the fields of the java object is name, which for this row is read as "KENAN SAR�IN"
I tried dumping to disk directly:
File file = new File("D:/testencoding/downloaded-file.txt");
FileOutputStream fop = new FileOutputStream(file);
ftpClient.retrieveFile(fileName, fop);
if (!file.exists()) {
file.createNewFile();
}
I compared the MD5 Checksums of the two files(FTP Server one and the and the one dumped to disk), and they're the same.
I would separate out the problems first: dump the file to disk, and compare it with the original. If it's the same as the original, the problem has nothing to do with UTF-8. The FTP code looks okay though, and if you're saying you want the raw binary data, I'd expect it not to mess with anything.
If the file is the same after transfer as before, then the problem has nothing to do with FTP. You say "the file is not getting read as UTF-8 Encoded" but it's not clear what you mean. How certain are you that it's UTF-8 text to start with? If you could edit your question with the binary data, how it's being read as text, and how you'd expect it to be read as text, that would really help.
Try to download the file content as bytes and not as characters using InputStream and OutputStream instead of InputStreamReader. This way you are sure that the file is not changed during transfer.
I need to send image from android app to java app. Basically, I need a byte array from the image to send to rf module which transmits.Another rf module receives and sends the byte array to java app which must make the image .
Android code:
FileInputStream fis = new FileInputStream(myFile);
byte[] b=new byte[(int)myFile.length()];
fis.read(b);server.send(b);
Java code:
FileOutputStream fwrite = new FileOutputStream(new File("my_xml"),true);
fwrite.write(bb);//bb is a byte from rf using input stream as soon as a byte comes it is read to file. This is necessary for some other reasons
fwrite.flush();
fwrite.close();
After getting full file:
FileInputStream fir=new FileInputStream("my_xml");
final BufferedImage bufferedImage = ImageIO.read(fir);
ImageIO.write(bufferedImage, "bmp", new File("image.bmp"));
fir.close();
I am getting error javax.imageio.IIOException: Bogus Huffman table definition
The rf is working fine because text file is being sent perfectly.Please help.Even without ImageIo code is not giving image even after changing extension to jpeg
The error means that the image file cant be read because the format is wrong.That is some bytes are missing or wrong or out of proper position and therefore file cant be decoded. My rf transfer does not have protocols like tcp/ip therefore some bytes are lost due to error in communication channel and hence the error.
You don't need to use ImageIO just to copy a file. Just read and write the bytes.
Your code has other problems:
You are assuming that read(byte[]) fills the buffer. It doesn't. Check the Javadoc.
You are also assuming that the file length fits into an int. If it does, fine. If it doesn't, you are hosed.
You appear to be opening and closing the FileOutputStream on every byte received. This could not be more inefficient. Open it once, write everything, close it.
flush() before close() is redundant.
You are storing the image in a file called 'my_xml'. This is only going to cause confusion, if it hasn't already.
You don't even need the file. Just load the image directly from the input stream.