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

Posted on 2008-11-14
Last Modified: 2013-12-29
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.

Question by:kodcanavari
    LVL 40

    Expert Comment

    are you looking for tools? on unix / linux you can use commands like unix2dos and dos2unix
    LVL 1

    Author Comment

    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?
    LVL 5

    Expert Comment

    You can use iconv.
    iconv --from-code=ISO-8859-1 --to-code=UTF-8 myfile > newfile

    Open in new window

    LVL 1

    Author Comment

    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.
             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 = > -1) 
                bWriter.write((char) ch);
          catch (Exception e) 

    Open in new window

    LVL 5

    Accepted Solution

    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


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    scores100 challenge 3 67
    userCompare  challenge 3 55
    Modeling a class in java 5 23
    Sed question 2 27
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now