public class Customer {
public static void main(String[] args) throws IOException {
FileOutputStream a = new FileOutputStream("customer.txt");
ObjectOutputStream b = new ObjectOutputStream(a);
human Iman = new human("Iman",5000);
human reda = new human("reda",5555);
b.writeObject(Iman); //prints random symbols.
b.writeObject(reda);
}
}
class human implements Serializable{
private String name;
private double balance;
public human(String n,double b){
this.name=n;
this.balance=b;
}
}
What do these random symbols represent?
Yes, you are trying to store the object itself and hence binary format is getting stored.
To actually store the data in text format, use below code BufferedWriter as below:
public void writeHumanStateToFile(Human human){
try{
File file = new File("filename.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(human.getName);
bw.write(human.getBalance);
bw.newLine();
bw.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
I am assuming you want to persist the state of Human object.
You're using ObjectOutputStream. That doesn't produce text - it produces a binary serialized version of the data. If you really need a text representation, you'll need to use a different approach.
If you're fine with it being binary data though, leave it as it is - but perhaps change the filename to be less misleading. You can read the data again with ObjectInputStream.
The data format is described in the Object Serialization Stream Protocol document. As you've noted, it's not human-readable.
If you want to serialize in a readable format, you might be able to use java.beans.XMLEncoder, or something like Pojomatic.
You are serializing the object. It is not meant to be readable in plain text, but to be a binary format that makes it easy to read the object and recreate it in a later execution of the program.
If you want to store your objects in plain text, then you need to write the individual fields of your object to the file.
Related
I am writing a file in java with BufferedWriter. Here is my code -
public static void main(String[] args) {
try(BufferedWriter writer = Files.newBufferedWriter(filePath, Charset.forName("CP1252"))) {
while (thereIsContentToWrite){
writer.write(content);
}
}catch (CharacterCodingException ce){
//get content of writer's current buffer (java.io.BufferedWriter.cb)
}catch (IOException e) {
e.printStackTrace();
}
}
I want to report out the content of the current buffer of writer as it contains the "Unmappable character" as per cp1252.
Currently, we are trying to maintain another buffer to hold the content but I would like to know whether there is a better approach to achieve the same.
You can write a CustomBufferedWriter which is similar to BufferedWriter and add a getter for cb in your implementation.
If I understand correctly, you want to report the contents of the BufferedWriter buffer. You can do that, using the Reflection API.
The following code should retrieve the buffer contents:
// BufferedWriter buffer is the private field "cb"
Field bufferField = writer.getClass().getDeclaredField("cb");
// We make this field accessible so we can retrieve its contents
bufferField .setAccessible(true);
// We can now get the data
char[] buffer = (char[]) bufferField.get(writer);
I am trying to write data to a binary file and am having difficulty. When I run this method I don't get any output to the file. Also when it comes to writing my "Date" object, I can't seem to find a write method that takes it as a parameter. The object consists of an int month, day, and year. How can I write it into a binary file properly?
Also, does "File" work for binary as well? I have only previously used it for regular .txt files and I'm not sure if it can be used the same way in this situation. Thanks!
Here is my write method:
private void writeBinary(){
//String fileName = getUserInput();
String fileTest = "BinaryMonster.bin";
File file = new File(fileTest);
DataOutputStream out;
try{
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file, true)));
if(!(file.exists())){
file.createNewFile();
System.out.println("New file created...");
}
for(int i = 0; i < monsterAttacks.size(); i++){
out.writeInt(monsterAttacks.get(i).getID());
out.write(monsterAttacks.get(i).getDate()); //getting error
out.writeUTF(monsterAttacks.get(i).getName() + monsterAttacks.get(i).getLocation() + monsterAttacks.get(i).getReporter());
}
} catch(IOException e) {
e.printStackTrace();
}
}
It is giving error because you are writing whole object of date into the file using DataOutputStream, which don't allow you to do that.
Write it in the form of String into the file. It will be better.
out.writeUTF(monsterAttacks.get(i).getDate().toString());
But if you want to save the whole object into the file, then you need to use ObjectOutputStream which write whole serialized objects into the file.
And it is better approach to flush and close the file.
out.flush();
out.close();
I have this piece of code, just to try to write it to a file. But when I compile it, it doesn't display any errors, but text in my file is unreadable, some Unicode codes etc... I use eclipse IDE. What could be the reason for this?
public static void main(String[] args) {
String s = "Hello world!";
int i = 143141141;
try
{
//create new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("test.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
//write something in a file
oout.writeObject(s);
oout.writeObject(i);
//close the stream
oout.close();
//create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("test.txt"));
//read and print what we wrote before
System.out.println("" + (String) ois.readObject());
System.out.println("" + ois.readObject());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
Since you are using ObjectOutputStream and ObjectInputStream , it will write in Object code , which is not readable , and as well when u read from file , it will come up as an Object so again as an Object ,
Use BufferedReader or Writer to write String into file , which can be read
FileReader f=new FileReader(new File("test.txt"));
BufferedReader f1=new BufferedReader(f)
;
With an ObjectOutputStream, you're using Serialization to write your objects to a file. Serialization is using an encoding system, and you use correctly an ObjectInputStream in your program to decode these objects. But you won't be able to read the information in the file created by the Serialization process.
You should use PrintWriter to write text files, ObjectOutputStream writes binary data.
Java ObjectOuputStream writes objects in a binary non human readable format which can be read only with ObjectInputStream.
Your code is working fine for me. If I understand it correctly when look at the contents of file by opening it in editor (say notepad or eclipse) you see characters stored as binary content in it. As you are using ObjectInputStream and ObjectOutputStream the behavior is correct.
You are not writing values of your String and Integer objects but their object representations in binary format. That is called object-serialization. That is some how encoded to represent all the information associate with the object not only its value That is only displayed when decoded in the same way as we encoded them. So, normal text editor cannot display the information as you expected.
If you want to save the string representation only, use the classes such as PrintWriter.
I tried your code. It's working perfectly for me. See the attached image.
Try cleaning your workspace. If it doesn't work, try creating a new Java project and copy the same code posted here and try. It should work.
You're making the folly of writing the output string through an ObjectOutputStream which serializes the String and Integer objects in your code and saves the Object state along with the value of the object. This is the reason why you see encoded text when you open the file. The following excerpt sums up the values which are stored when an Object is serialized:
The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.(ObjectOutputStream)
The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it.
Primitive data, excluding serializable fields and externalizable data, is written to the ObjectOutputStream in block-data records. A block data record is composed of a header and data. The block data header consists of a marker and the number of bytes to follow the header. (ObjectOutputStream javadoc)
The possible problem with your code is you are not flushing the output data. So that it might not get written to the output file.
Try the below code.
public static void main(String[] args) {
String s = "Hello world!";
int i = 143141141;
try
{
//create new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("test.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
//write something in a file
oout.writeObject(s);
oout.flush();
oout.writeObject(i);
oout.flush();
//close the stream
out.close();
oout.close();
//create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
//read and print what we wrote before
System.out.println("" + (String) ois.readObject());
System.out.println("" + ois.readObject());
ois.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
And also if you want to read your written objects into the file then you can't because they are written as serialized objects. For textual operation with files you can consider BufferedReader or PrintWriter. see the following code.
public class WriteToFileExample {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("c:\\desktop\\filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
After this you can open your text file and can see the written content in the human readable form and it is good practice to not to give "txt" formats when you are writing objects to the file. It's misleading.
Hope this helps.
I want to write the numbers to a file. The programs runs without any errors but when I open the file that I wrote to, the contents of the file is something like "!"#$%&'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈ".
I cross-checked by reading the contents of the file using FileInputStream and printing the contents. The IDE prints the desired output but the integers in the file are not stored in readable form.
Can anyone tell me why the integers are not populating in the file as intended?
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
try{
FileOutputStream fos = new FileOutputStream("Exercise_19.txt");
for(int i = 0;i<=200; i++){
fos.write((int)i);
}
fos.close();
}
catch(IOException ex){
ex.printStackTrace();
}
}
Quite why you're getting that data in the file, I'm not sure, but the FileOutputStream is for writing raw bytes of data rather than human readable text. Trying using a FileWriter instead.
fos.write(int) writes a byte using the lowest 8-bits of the integer.
if you want to write text to a file, a simpler option is to use PrintWriter (note PrintWriter can use FileOutputStream so it can be done, but you really have to know what you are doing.)
public static void main(String... args) throws FileNotFoundException {
PrintWriter pw = new PrintWriter("Exercise_19.txt");
for (int i = 0; i <= 200; i++)
pw.println(i);
pw.close();
}
FileOutputStream Class's Write() method will write the data in Byte format. so you can not read it in normal Notepad format. However you can read it using FileInputStream Class.
Syntax for Writing is as follows, look at this link
write(int b)
Writes the specified byte to this file output stream.
A Complete Writing and Reading Example you can find here.
I'm trying to create a file name that uses the date and time from another class to name it, problem is the class is called every so often and when it is a new file is created. I just wanted to create the file once and then write to it all the time. Is it possible to do this as I can't work out how to?
Many thanks for any help in advance!
public void fileOutputToFile(String hex) throws Exception{
dateAndTime dat = new dateAndTime();
String date = dat.currentDateAndTime();
String fileInfo = hex;
String fileName = (date+".tsv");
try{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true)));
out.print(fileInfo);
out.print("\t");
out.close();
}catch (IOException e){
}
}
Use a date formatter like SimpleDateFormat to format the date to a string, e.g. "yyyy" to make a file 2011.tsv. Note that this requires a Date object to be returned.
If you don't want to use a file based on date, store the filename somewhere. But why would you then use the date as the filename in the first place?
Edit: For a new file every hour, use a format like this: yyyy-MM-dd_HH (would result in 2011-03-29_17.tsv for example).
On the first call, you could store the filename as a member of the class.
You can append to the file on subsequent calls by using the FileWriter constructor:
public FileWriter( String fileName,
boolean append)
If you write frequently, an alternative is to keep the file open, storing the PrintWriter as a member. However, this may preclude some external interactions with the file in between writes.
Can you save the file name in session? if the file name exists in session use that other wise create it from another clases..
The problem with using the time alone is you can have still have multiple creates created at the same time.
You can do the following.
// not thread safe.
private static final DateFormat DF = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS")
File file;
do {
file = new File(DF.format(new Date())+".tsv");
} while(!file.createNewFile());
FileUtils.writeStringToFile(file, hex+"\t");
The other problem is that you will giving the file a new name on every update. If youw ant to keep the same file name you need to set this in a different method of lazy initialise the value from a field.
In order to avoid creating a new file with a new date and time every time a client application invokes method fileOutputToFile(), move all lines up to and including the creation of the PrintWriter to the constructor so that the constructor opens the file just once and method fileOutputToFile() appends fileInfo to the output stream.
Make sure to add a method close() that closes the PrintWriter stream by invoking out.close().
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class MyLoggingClass {
private PrintWriter out;
public MyLoggingClass() throws IOException {
dateAndTime dat = new dateAndTime();
String date = dat.currentDateAndTime();
String fileName = (date + ".tsv");
out = new PrintWriter(
new BufferedWriter(new FileWriter(fileName, true)));
}
public void fileOutputToFile(String hex) {
String fileInfo = hex;
out.print(fileInfo);
out.print("\t");
}
public void close() {
out.close();
}
}