简体   繁体   中英

File type in java (Windows,unix)

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:

xatfasfghjnvxzsdfgbsc dedd

output.txt :

aabcddddefffgghjnssst vxxz

SS-s 在此输入图像描述

在此输入图像描述

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

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());

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM