Link to home
Start Free TrialLog in
Avatar of kodcanavari
kodcanavari

asked on

How can i convert a file in ASCii/Dos format to ASCii/Unix format in Java ?

I write an application that takes file urls and charset names and than converts them into specified charset.
How can i make the conversion between ASCII/Dos -> ASCII/Unix et vice versa.

Regards.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

are you looking for tools? on unix / linux you can use commands like unix2dos and dos2unix
Avatar of kodcanavari
kodcanavari

ASKER

No , i wanna code it. If would be good if i can pass charset type to the stream but i don't know if there is a charset encoding for ascii/ unix in java. Does anyone knows?
You can use iconv.
iconv --from-code=ISO-8859-1 --to-code=UTF-8 myfile > newfile

Open in new window

thanks daniok, i've leared about iconv tool by this occasion but i really ned a java solution.
I have already converted other types of charset like ascii -> epcdic with the code below but i don't know how to do the same between ascii/dos and ascii/windows.
      try 
      {
         File fichierSource = new File(p_UrlFichierSource);
         FileInputStream streamFichierSource = new FileInputStream(fichierSource);
         Reader readerStreamFichierSource = new InputStreamReader(streamFichierSource, "ASCII");
         BufferedReader bReader = new BufferedReader(readerStreamFichierSource);
 
         File fichierDestination = new File(p_UrlFichierDestination);
         FileOutputStream streamFichierDestination = new FileOutputStream(fichierDestination);
         Writer writerStreamFichierDestination = new OutputStreamWriter(streamFichierDestination, "CP500");
         BufferedWriter bWriter = new BufferedWriter(writerStreamFichierDestination);
         
         int ch;
         while ((ch = bReader.read()) > -1) 
         {
            bWriter.write((char) ch);
         }
         
         bReader.close();
         bWriter.close();
 
      } 
      catch (Exception e) 
      {
         e.printStackTrace();
      }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of dainok
dainok
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial