gedit uses UTF encoding. Therefore, if we store binary data in the file (like through DataOutputStream's writeInt() or writeByte(), etc.), gedit would rightly complain, but if we write something through the writeUTF() function, then gedit should be able to open it as the data is written is UTF which it recognizes. But it does not. It complains :

gedit has not been able to detect a character encoding. Please check if you are not trying to open a binary file.

The following was the code :

import java.io.*;
class first
{
	public static void main(String aa[])
	{
                try{
		FileOutputStream n=new FileOutputStream("anjana.txt");
		DataOutputStream k=new DataOutputStream(n);
		k.writeUTF("2");
		//k.writeUTF("3");
		
		FileInputStream a=new FileInputStream("anjana.txt");
		DataInputStream b=new DataInputStream(a);
		System.out.println(b.readUTF());
		}
		catch (IOException e){System.out.println("error : "+e);}
	}
}

The javadoc of DataOutputStream.writeUTF() method says the following
"First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow"

anjana.txt would have a short (2 bytes of data) followed by the UTF String. gedit would NOT know how to interpret the first 2 bytes. IMHO using DataOutputStream to a text file and opening it using gedit will NOT work. Use a FileWriter instead of DataOutputStream.

Thanks. It definitely doesn't use UTF.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.