• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1040
  • Last Modified:

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.
0
kodcanavari
Asked:
kodcanavari
  • 2
  • 2
1 Solution
 
omarfaridCommented:
are you looking for tools? on unix / linux you can use commands like unix2dos and dos2unix
0
 
kodcanavariAuthor Commented:
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?
0
 
dainokCommented:
You can use iconv.
iconv --from-code=ISO-8859-1 --to-code=UTF-8 myfile > newfile

Open in new window

0
 
kodcanavariAuthor Commented:
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

0
 
dainokCommented:
Maybe you can substitute all Windows Carriage-Return (ASCII 13) with Unix Line Feed (ASCII 10) or viceversa. Or better  you can give a try to CRLF Class at http://tanmaykm.tripod.com/work/javawork.html.

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now