1

How do I make a class instance and then export it to a file? Hope you can help. Here is my code:

import java.io.*;

public class Testing {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Testing t = new Testing();

    String fileName = ("C:/Users/Brandon/Desktop/Java/copy.java");

     try {
            FileWriter fileWriter =
                new FileWriter(fileName);

            BufferedWriter bufferedWriter =
                new BufferedWriter(fileWriter);

            bufferedWriter.write(t);

            bufferedWriter.close();
        }
        catch(IOException ex) {
            System.out.println(
                "Error writing to file '"
                + fileName + "'");

I get this error message:

The method write(int) in the type BufferedWriter is not applicable for the arguments (Testing).

How do I fix this?

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
brandon Whe
  • 206
  • 1
  • 14

2 Answers2

0

You cannot just write a Java object to a file. The process to do so is called serialization and it imposes certain requirements on the object. Look at ObjectOutputStream and Serializable to get started.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • Even if its another Java file? – brandon Whe Jul 23 '14 at 01:43
  • There is __absolutely__ no point in exporting an instance of a Java class to a `.java` (as in Java source code) file - I think you are maybe mis-understanding something here. Please explain further what you're trying to do, maybe we can dis-entangle it then. – Alexander Gessler Jul 23 '14 at 01:48
  • I'm trying to get it to create it self in a different program – brandon Whe Jul 23 '14 at 01:50
  • can I write it using FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345); oos.writeObject(t)); oos.close(); – brandon Whe Jul 23 '14 at 01:56
0

this is how your code may be looking for that part:

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Testing implements Serializable {

private static final long serialVersionUID = 1L;

public static void main(String args[]) {
    Testing t = new Testing();
    String fileName = new String("copy.ser");
    t.write(t, fileName);
}

public void write(Testing t, String fileName) {

    try {

        FileOutputStream fout = new FileOutputStream("copy.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(t);
        oos.close();
        System.out.println("Done");

    } catch (Exception ex) {
        System.out.println(
            "Error writing to file '"
            + fileName + "'");
    }
}
}
JurijG
  • 16
  • Thanks for the input but in the file this is what it gives me: ¬í sr Testing xp What I'm looking to do is create a program that can create itself in another file, I'm starting to think this isn't the way to do it. – brandon Whe Jul 23 '14 at 02:40
  • May be this will be helpful then http://stackoverflow.com/questions/4002462/how-can-i-write-a-java-application-that-can-update-itself-at-runtime It is on updating itself, but it can be used to create new file – JurijG Jul 23 '14 at 02:59