How to write content on a text file using java?

前端 未结 3 1110
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 09:58


i am having a program in java.which system.out some strings,i need to save each of them in a text file
it is showing in a format
ruo1 row2 row3
i want it in

相关标签:
3条回答
  • 2021-01-15 10:50

    Very quick code. I apologize if there are compile errors.

     import java.io.FileWriter;
      import java.io.IOException;  
      public class TestClass {
          public static String newLine = System.getProperty("line.separator");
          public static void main(String[] a) {
            FileWriter writer;
            try {
              writer = new FileWriter("test.txt");
              for(int i=0;i<3;i++){
                writer.write(row+i+newLine);
              }
              writer.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
    
          }
        }
    
    0 讨论(0)
  • 2021-01-15 10:53

    u can use PrintWriter pw;
    pw.println(row+i) in above instead of hard coding newLine

    0 讨论(0)
  • 2021-01-15 10:59

    how about adding a new line character "\n" to each row ?

    0 讨论(0)
提交回复
热议问题