简体   繁体   English

java中的文件类型(Windows,unix)

[英]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. 我的input.txt类型是dos \\ Windows,如图所示。 My generated output.txt type is UNIX. 我生成的output.txt类型是UNIX。 Also their sizes are different. 它们的尺寸也不同。 Why are they stored in different format? 为什么它们以不同的格式存储? I used, bufferedReader, fileWriter to implement this code. 我使用bufferedReader,fileWriter来实现这段代码。

code.java: 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: input.txt中:

xatfasfghjnvxzsdfgbsc dedd xatfasfghjnvxzsdfgbsc dedd

output.txt : output.txt:

aabcddddefffgghjnssst vxxz aabcddddefffgghjnssst vxxz

SS-s SS-S 在此输入图像描述

在此输入图像描述

How can I generate output file as windows format(Also,their sizes should be same)? 如何以Windows格式生成输出文件(另外,它们的大小应该相同)?

The phenomenon you are experiencing is a difference in end-of-line characters between UN*X systems and Microsoft Windows systems. 您遇到的现象是UN * X系统和Mi​​crosoft Windows系统之间的行尾字符的差异。 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) UN * X系统使用LF(换行)字符(ASCII中为\\n ,0x0A)
  • Windows systems use a CR (carriage return) and a LF (line feed) character ( \\r\\n , 0x0D and 0x0A in ASCII) Windows系统使用CR(回车)和LF(换行)字符(ASCII中的\\r\\n ,0x0D和0x0A)

You state that you want to use the Windows variant. 您声明要使用Windows变体。 In that case, you should not be appending "\\n" to every line in the new file. 在这种情况下,您不应将"\\n"附加到新文件中的每一行。 The naive approach would be to use "\\r\\n" , but there is a better way: 天真的方法是使用"\\r\\n" ,但有更好的方法:

Java provides you with the ability to get your current platform's preferred end-of-line character sequence. Java使您能够获得当前平台首选的行尾字符序列。 You can get your platform's end-of-line character sequence by calling System.getProperty("line.separator") (< Java 7) or System.lineSeparator() (≥ Java 7). 您可以通过调用System.getProperty("line.separator") (<Java 7)或System.lineSeparator()System.lineSeparator()来获取平台的行尾字符序列。

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. Windows上的行结尾与其他平台上的行结尾不同。 You're always writing "\\n" which is the Unix line ending. 你总是写"\\n"这是Unix行的结尾。

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. 虽然您可以简单地将其硬编码到Windows行结尾( "\\r\\n" ),但如果您希望代码可以在任何地方使用,则应使用平台行分隔符。 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替换FileWriter:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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