简体   繁体   English

当多行作为JTextarea的输入时,如何在文件中写入多行?

[英]How to write multiple lines in a file when this multiple lines take as a input from a JTextarea?

I take multiple lines as a input from a JTextarea, If I write it in a file, then I get that the multiple lines are write in a one line in file 我从JTextarea获取多行作为输入,如果我在一个文件中写入,那么我得到多行在文件中的一行写入

Examples: 例子:

In JTextArea: 在JTextArea中:

I
am
a
student

Means: variable.text="I'\\n'am'\\n'a'\\n'student"; 意思是:variable.text =“我'\\''''\\ n'a'\\ n'student”; When I write the string s in file I get: 当我在文件中写字符串s时,我得到:

I am a student

But I want that the file will contain the same things as I give as a input means---> 但我希望文件包含与我作为输入方式相同的东西--->

I
am
a
student

This is the code of writing a file : 这是编写文件的代码:

      BufferedWriter out = new BufferedWriter(
       new OutputStreamWriter(
                  new FileOutputStream(file), "UTF16"));
      int size=1;
      for(Tableclass variable:tablevector)
      {
            out.write(variable.Text); 
            out.newLine();
            size++;

      }
      out.close();

Slighty better version would be : Slighty更好的版本将是:

    try {
        PrintWriter fstream = new PrintWriter(new FileWriter("log.txt"));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


 for(String word : jTextAreaName.getText().split("\n"))  {    
    fstream.println(word); 
} 
     fstream.flush();

Use out.newLine(); 使用out.newLine();

   BufferedWriter out = new BufferedWriter( 
   new OutputStreamWriter( 
              new FileOutputStream(file), "UTF16")); 
  int size=1; 
  for(Tableclass variable:tablevector) 
  { 
        out.write(variable.Text); 
        out.newLine();          
        size++; 

  } 
  out.close(); 

在你的字符串char(10)或char(13)中找到这个字符

int index = textarea.firstIndexOf(CHAR(10));

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

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