简体   繁体   English

用Java编写文件时如何创建换行器?

[英]How do i create a line breaker when writing a file in Java?

I'm working on a code that takes in a couple of inputs and re-write them into a .txt file of my creation but to my dismay the output file can not contain a line-breaker even with the appropriate formatting. 我正在处理一个需要几个输入并将它们重新写入到我创建的.txt文件中的代码,但令我沮丧的是,即使使用适当的格式,输出文件也不能包含换行符。 I'm using Formatter(java.util) 我正在使用Formatter(java.util)

public void file(Scanner s, Formatter f){
    String s1 = s.nextLine();
    if (!"end".equals(s1)){
        f.format("%s\n%s", s1, "  ");
        file(s,f);
    }
    f.close();
}

public void file(Scanner input,Formatter f,int state){
    String s1 = input.nextLine();
    int loopStop = 0;
    if (state == 1){
        file(input, f);
    } else {
        while (!"end".equals(s1) && loopStop<11){
            f.format("%s\n\n%s", s1, "  ");
            s1 = input.nextLine();
            loopStop++;
        }
        f.close();
    }
}

对平台特定的行分隔符使用'%n':

f.format("%s%n%n%s", s1, "  ");

Try with System.getProperty("line.separator") 尝试使用System.getProperty("line.separator")

Read it here about System Properties with complete list of System properties. 在此处阅读有关系统属性以及完整的系统属性列表。

The System class maintains a Properties object that describes the configuration of the current working environment. System类维护一个Properties对象,该对象描述当前工作环境的配置。

"line.separator" - Sequence used by operating system to separate lines in text files “ line.separator”-操作系统用于分隔文本文件中的行的序列


Try with System.lineSeparator() as well that states: 尝试使用System.lineSeparator()并指出:

Returns the system-dependent line separator string. 返回系统相关的行分隔符字符串。 It always returns the same value - the initial value of the system property line.separator. 它总是返回相同的值-系统属性line.separator的初始值。

On UNIX systems, it returns "\\n"; 在UNIX系统上,它返回“ \\ n”; on Microsoft Windows systems it returns "\\r\\n". 在Microsoft Windows系统上,它返回“ \\ r \\ n”。

Assuming that you're using Windows, you need to use \\r\\n which is the proper combination of line-break in Windows or simply use System.getProperty("line.separator"); 假设您使用的是Windows,则需要使用\\r\\n这是Windows中换行符的正确组合),或者只需使用System.getProperty("line.separator"); to get the newline combination. 获得换行组合。

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

相关问题 在用Java编写由opencsv生成的文件时,如何创建无标题的文件? - When writing a file produced by opencsv in java how do I create a file with no heading? 写入 CSV 文件时如何跳过第一行 - how do I skip the first line when writing to a CSV file 如何在java中读取文件时跳过回车符作为换行符 - How to skip carriage return as line breaker while reading a file in java 如何在CompletableFutures中使用Akka Java断路器? - How do i use Akka java circuit breaker with CompletableFutures? 如何使用Java在属性文件中创建换行? - How do I create a new line in a properties file using java? 在Java中,如何从字符串创建文件而不将其写入磁盘? - In Java, how do I create a File from a String without writing it to disk? 写入文本文件Java时换行 - New line when writing to a text file Java 在 Java 中,使用 DataOutputStream 写入文件时,如何定义正在写入的数据的 Endian? - In Java, when writing to a file with DataOutputStream, how do I define the Endian of the data being written? 我在Java中使用Writer类进行UTF8编码输出。 编写时如何插入新行? - I am using Writer class in Java for UTF8 encoding output. How do I insert new line when writing? 将字符串写入文件时如何换行? - How can I make a line break when writing a string to a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM