Java 将换行符写入文件

Java 将换行符写入文件,java,file,newline,Java,File,Newline,考虑到以下功能 private static void GetText(String nodeValue) throws IOException { if(!file3.exists()) { file3.createNewFile(); } FileOutputStream fop=new FileOutputStream(file3,true); if(nodeValue!=null) fop.write(nodeValue.getBy

考虑到以下功能

private static void GetText(String nodeValue) throws IOException {

   if(!file3.exists()) {
       file3.createNewFile();
   }

   FileOutputStream fop=new FileOutputStream(file3,true);
   if(nodeValue!=null)
       fop.write(nodeValue.getBytes());

   fop.flush();
   fop.close();

}
要添加什么使其每次写入下一行

例如,我希望给定字符串中的每个单词都以单独的直线表示,例如:

i am mostafa
写为:

 i
 am
 mostafa
换行

if(nodeValue!=null)
    fop.write(nodeValue.getBytes());

fop.flush();

更新以解决您的编辑问题

为了将每个单词写在不同的行上,您需要拆分输入字符串,然后分别写每个单词

private static void GetText(String nodeValue) throws IOException {

    if(!file3.exists()) {
        file3.createNewFile();
    }

    FileOutputStream fop=new FileOutputStream(file3,true);
    if(nodeValue!=null)
        for(final String s : nodeValue.split(" ")){
            fop.write(s.getBytes());
            fop.write(System.getProperty("line.separator").getBytes());
        }
    }

    fop.flush();
    fop.close();

}
您需要在每次写入的末尾添加换行符。

您可以通过

将文本(而不是原始字节)写入文件中,您应该考虑使用。您还应该将其包装在一个文件中,然后该文件将为您提供方法

要将每个单词写在新行上,请使用将文本拆分为单词数组

下面是对您的需求的一个简单测试:

public static void main(String[] args) throws Exception {
    String nodeValue = "i am mostafa";

    // you want to output to file
    // BufferedWriter writer = new BufferedWriter(new FileWriter(file3, true));
    // but let's print to console while debugging
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    String[] words = nodeValue.split(" ");
    for (String word: words) {
        writer.write(word);
        writer.newLine();
    }
    writer.close();
}
输出为:

i
am
mostafa

其他答案应该有效。不过我想提一提

从java文档:

FileWriter用于写入字符流。写作 原始字节流,考虑使用<强>文件输出流< /强> ./p> 阅读方法代码时,您将要向文件中写入字符串,您所做的是将字符串转换为原始字节,然后写入,因此我认为使用FileWriter不是一个坏主意


对于换行符问题,Writer有一个方法。write(String),它使用起来很方便。

或者您可以使用以下类似的方法。请注意“\n”:


如果您使用的是windows操作系统,请使用\r\n作为结束行。

解决方案:

在WINDOWS中,您应该为新行编写
“\r\n”

Files.write(Paths.get(filepath),texttobewrittentofile,StandardOpenOption.APPEND ,StandardOpenOption.CREATE);    
这将创建一个不存在的文件 如果存在文件,则使用现有文件并追加文本 如果要将everyline写入下一行,请将换行符的lineSepartor添加到文件中

String texttobewrittentofile = text + System.lineSeparator();

@Mostafa您使用什么程序查看文件?另外,您是否正在使用
System.getProperty(“line.separator”)
?(我编辑了我的答案。)@Mostafa你是在写
“\n”.getBytes()
还是
System.getProperty(“line.separator”).getBytes()
?当写字板和office word仍然显示与notpad相同的答案时,结果是一样的,你是否意识到你声明了s但实际上没有使用it@Mostafa哎呀。那是个错误。我已经修好了,但关键是我想让它把字符串的每个“是”都写在一行,而不是每个字符串…你明白我的意思了吗?你想把每个字节都写在一行吗?如果是这样的话,请在你的问题中说明清楚。我刚才做了,很抱歉,但它给出的结果与我在问题中的函数相同:(哦~不,我使用的是windows vista!哦,你用什么来振动它们?你可以删除
f.exists()/f.createNewFile()
东西。
new FileOutputStream()
已经这样做了。如果您使用windows,那么换行符应该是“\r\n”。@OlleSöderström您应该编写此解决方案作为答案!非常感谢。我不认为这比
System.lineSeparator()
System.getProperty(“line.separator”)
更好。
i
am
mostafa
/**
 * Created by mona on 3/26/16.
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
public class FileExample {


    public static void main (String[] args) throws java.io.IOException {

        File newFile = new File("tweet.txt");
        FileWriter fileWriter = new FileWriter(newFile);
        fileWriter.write("Mona Jalal");
        fileWriter.append("\nMona Mona");
        fileWriter.close();
        FileReader fileReader = new FileReader(newFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        fileReader.close();
        bufferedReader.close();



    }
}
Files.write(Paths.get(filepath),texttobewrittentofile,StandardOpenOption.APPEND ,StandardOpenOption.CREATE);    
String texttobewrittentofile = text + System.lineSeparator();