2

I implemented a code that takes input file from command line. Then, sorts this input. Then write output to current directory. My code works but I am wondering that type of file. My input.txt type is dos\Windows as seen in the picture. My generated output.txt type is UNIX. Also their sizes are different. Why are they stored in different format? I used, bufferedReader, fileWriter to implement this code.

code.java:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.FileWriter;

public class code{

    public static void main(String[] args) {


        try (BufferedReader br = new BufferedReader(new FileReader(args[0])))
        {

            int lines = 0;
            while (br.readLine() != null) lines++; // to get text's number of lines 

            String sCurrentLine;
            BufferedReader br2 = new BufferedReader(new FileReader(args[0])); //to read and sort the text

            String[] array; //create a new array
            array = new String[lines];

            int i=0;
            while ((sCurrentLine = br2.readLine()) != null) {//fill array with text content
                array[i] = sCurrentLine;
                i++;
            }
            Arrays.sort(array); //sort array


            FileWriter fw = new FileWriter("output.txt");

            for (i = 0; i < array.length; i++) { //write content of the array to file
                fw.write(array[i] + "\n");
            }
            fw.close();


            System.out.println("Process is finished.");


        } catch (IOException e) {
            e.printStackTrace();
        } 

    }
}

input.txt:

x a t f a s f g h j n v x z s d f g b s c d e d d

output.txt :

a a b c d d d d e f f f g g h j n s s s t v x x z

SS-s enter image description here

enter image description here

How can I generate output file as windows format(Also,their sizes should be same)?

2
  • Their sizes will not be the same, DOS and Unix use different line endings. On Unix systems, the line ending is \n; while on DOS/Windows it is \r\n. Commented May 5, 2016 at 22:45
  • My desire is to make sizes same. Yes they are different. Commented May 5, 2016 at 22:47

2 Answers 2

5

The phenomenon you are experiencing is a difference in end-of-line characters between UN*X systems and Microsoft Windows systems. These systems prefer to use different sequences of characters to signal a end of line.

  • UN*X systems use the LF (line feed) character (\n, 0x0A in ASCII)
  • Windows systems use a CR (carriage return) and a LF (line feed) character (\r\n, 0x0D and 0x0A in ASCII)

You state that you want to use the Windows variant. In that case, you should not be appending "\n" to every line in the new file. The naive approach would be to use "\r\n", but there is a better way:

Java provides you with the ability to get your current platform's preferred end-of-line character sequence. You can get your platform's end-of-line character sequence by calling System.getProperty("line.separator") (< Java 7) or System.lineSeparator() (≥ Java 7).

So, to sum this up, you should change the following line:

fw.write(array[i] + "\n");

to

fw.write(array[i] + System.lineSeparator());
Sign up to request clarification or add additional context in comments.

1 Comment

The program is dangerously written to use the "system default" character set and encoding. (It's almost certainly not ASCII.) You explain the observed effect well but with different data, differences in encoding would show up, too. Files should be UTF-8 unless there is a good reason not to be. System default might be a requirement but such files should not be transferred between systems.
3

Line endings are different on Windows than on other platforms. You're always writing "\n" which is the Unix line ending.

While you could simply hard-code it to the Windows line ending ("\r\n"), if you want your code to work everywhere, you should use the platform line separator. One way is to get it is from the system properties:

fw.write(array[i] + System.getProperty("line.separator"));

A slightly more readable approach is to replace your FileWriter with a Formatter:

Formatter fw = new Formatter("output.txt");

for (i = 0; i < array.length; i++) { //write content of the array to file
    fw.format("%s%n", array[i]);
}
fw.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.