Search code examples
javabinaryfiles

How to write from .data file to .txt file In Java?


I have written some information into a .data file

PrintWriter out;
DataOutputStream binaryFile= null;
public WriteToFile(String file,String text ) throws IOException {

     binaryFile= new DataOutputStream(
                new FileOutputStream(file,true));
     binaryFile.writeInt(2); 
     binaryFile.writeDouble(3);
     binaryFile.writeChar(2);
     binaryFile.writeUTF(text);
     binaryFile.close();  
}

I'm looking for a way to make a function that takes a .data file and writes each and every value into a corresponding text file

So Far I've tried this :

public void writeToTextFile(String myFile)
{
    DataOutputStream fichIn = new DataOutputStream(new FileOutputStream(myFile));;
    try {
        //How to take information from .data file and writethem in text file ? 
    } catch (IOException e) {
        System.out.println("Error");
    }

    // Some Code
}

Solution

  • You need to read your binary file with a DataInputStream and the readXXX() methods corresponding to the writeXXX() methods you used to write it, and then use the methods of PrintWriter to write to the new text file.