【问题标题】:write newline into a file将换行符写入文件
【发布时间】:2012-01-19 11:20:12
【问题描述】:

考虑以下功能

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

【问题讨论】:

  • 您可以删除 f.exists()/f.createNewFile() 的东西。 new FileOutputStream() 已经这样做了。

标签: java file newline


【解决方案1】:
Files.write(Paths.get(filepath),texttobewrittentofile,StandardOpenOption.APPEND ,StandardOpenOption.CREATE);    

这会创建一个文件,如果它不存在 如果文件存在,则使用现有文件并附加文本 如果您希望将每一行写入下一行,请将换行符的 lineSepartor 添加到文件中。

String texttobewrittentofile = text + System.lineSeparator();

【讨论】:

    【解决方案2】:

    解决方案:

    在 WINDOWS 中你应该写 "\r\n" 换行。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我看不出这比System.lineSeparator()System.getProperty("line.separator") 更好。
      【解决方案4】:

      或者你可以使用类似下面的东西。注意“\n”:

      /**
       * 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();
      
      
      
          }
      }
      

      【讨论】:

        【解决方案5】:

        换行

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

        if(nodeValue!=null) {
            fop.write(nodeValue.getBytes());
            fop.write(System.getProperty("line.separator").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();
        
        }
        

        【讨论】:

        • @Mostafa 你用什么程序来查看文件?另外,您使用的是System.getProperty("line.separator") 吗? (我编辑了我的答案。)
        • @Mostafa 你写的是"\n".getBytes() 还是System.getProperty("line.separator").getBytes()
        • wordpad 和 office word 仍然显示与 notpad 显示相同的答案
        • 结果是一样的,你有没有意识到你声明了 s 但实际上并没有使用它
        • @Mostafa 哎呀。那是个错误。我已经修好了。
        【解决方案6】:

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

        要将每个单词写在新行上,请使用 String.split 将您的文本分成单词数组。

        所以这里是对您的要求的简单测试:

        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
        

        【讨论】:

        • 但重点是我希望它在新行中写入字符串的每个字节,而不是每个字符串...你明白我的意思了吗?
        • 您希望每个字节都换行吗?如果是这样,请在您的问题中明确说明。
        • 我刚刚做了,抱歉
        • 但它给出了与我在问题中的函数相同的结果:(哦~
        • 不,我使用的是 windows vista!哦,你用什么来观察它们?
        【解决方案7】:

        其他答案应该有效。但是我想提一下

        来自 java 文档:

        FileWriter 用于写入字符流。写作用 原始字节流,请考虑使用 FileOutputStream

        阅读您的方法代码,您即将将 String 写入文件,您所做的是将 String 转换为原始字节,然后写入,所以我认为使用 FileWriter 不是一个坏主意。

        而且对于换行问题,Writer有方法.write(String),使用起来很方便。

        【讨论】:

          【解决方案8】:

          您可以通过PrintStream 打印。

          PrintStream ps = new PrintStream(fop);
          ps.println(nodeValue);
          ps.close();
          

          【讨论】:

            【解决方案9】:
            if(!file3.exists()){
                file3.createNewFile();
            }
            FileOutputStream fop=new FileOutputStream(file3,true);
            if(nodeValue!=null) fop.write(nodeValue.getBytes());
            
            fop.write("\n".getBytes());
            
            fop.flush();
            fop.close();
            

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

            【讨论】:

            • 如果您使用的是 Windows,则新行将是 "\r\n"。
            • @OlleSöderström 你应该写这个解决方案作为答案!非常感谢。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-04-18
            • 1970-01-01
            • 2018-10-02
            • 2019-09-23
            • 2013-12-13
            • 1970-01-01
            相关资源
            最近更新 更多