2011-03-09 171 views
2

我是Java新手,尝试将多行字符串保存到文本文件。Java将多行字符串保存到文本文件

现在,它在我的应用程序中起作用。就像,如果我从我的应用程序保存文件,然后从我的应用程序打开它,它确实在行之间放置一个空格。但是,如果我从我的应用程序保存该文件,然后在记事本中打开它,它全部在一行上。

有没有办法让它在所有程序上显示多行?这是我现在的代码:

public static void saveFile(String contents) { 

    // Get where the person wants to save the file 
    JFileChooser fc = new JFileChooser(); 

    int rval = fc.showSaveDialog(fc); 

     if(rval == JFileChooser.APPROVE_OPTION) { 

      File file = fc.getSelectedFile(); 

      try { 
       //File out_file = new File(file); 
       BufferedWriter out = new BufferedWriter(new FileWriter(file)); 
       out.write(contents); 
       out.flush(); 
       out.close(); 
      } catch(IOException e) { 
       messageUtilities.errorMessage("There was an error saving your file. IOException was thrown.", "File Error"); 
      } 
     } 

     else { 
      // Do nothing 
      System.out.println("The user choose not to save anything"); 
     } 
} 
+0

什么是一圈? – Daniel 2011-03-09 23:36:47

+0

@丹尼尔我的意思是“应用程序”。 – 2011-03-31 23:18:13

回答

4

根据你如何构造你的字符串,你可能会碰到行结束问题。记事本不支持unix行结尾(\ n),它只支持行结尾(\ n \ r)。尝试使用更强大的编辑器打开保存的文件,和/或确保您的平台使用适当的行结尾。 java的系统属性(System.getProperty(“line.separator”))将为您获取代码运行平台的适当行结束。

在构建要保存到文件中的字符串时,而不是为行结尾明确指定“\ n”或“\ n \ r”(或在Mac上为“\ r”),您会而是追加该系统属性的值。

像这样:

String eol = System.getProperty("line.separator"); 

... somewhere else in your code ... 

String texttosave = "Here is a line of text." + eol; 

... more code.. optionally adding lines of text ..... 
// call your save file method 
saveFile(texttosave); 
+0

在这种情况下,wordpad将是一个“更强大的编辑器”。您也可以使用像unix2dos这样的程序来更改行结束符,以便记事本可以识别它们。 – 2011-03-10 00:07:38

+0

好的。我不知道如何应用这个。我怎么能使它使用正确的线分隔符? – 2011-03-10 00:22:24

+0

已更新的答案,以解释行结束方法 – 2011-03-10 00:43:09

0

是啊,因为以前的答案提到System.getProperty( “line.seperator”)。

你的代码没有显示你如何创建字符串内容,但既然你说你是新来的java我想我会提到,在Java中串联字符串是不好的,因为它创建一个。如果你是这样构建字符串:

String contents = "" 
contents = contents + "sometext" + "some more text\n" 

然后考虑使用java.lang.StrinBuilder代替

StringBuilder strBuilder = new StringBuilder(); 
strBuilder.append("sometext").append("somre more text\n"); 
... 
String contents = strBuilder.toString(); 

另一种方法是流什么都你打算写一个文件,而不是建立一个大的字符串,然后输出。

+0

我从文本区域获取字符串。我正在制作一个类似于记事本的程序。 – 2011-03-10 00:43:03

0

你可以添加类似:

contents = contents.replaceAll("\\n","\\n\\r"); 

如果记事本不能正确显示。但是,你可能遇到一个不同的问题:在每次保存/加载时,你会得到多个\ r字符。然后为了避免在加载时你必须调用上面相同的代码,但使用相反的参数。这实际上是一个非常难看的解决方案,只是为了让文本在记事本中正确显示。

0

我曾经有过这样的问题,我的家伙朋友,经过多次思考和研究,我甚至找到了解决方案。

您可以使用ArrayList将TextArea的所有内容作为例子,并通过调用save作为参数发送,因为writer只是写了字符串行,然后我们使用“for”逐行写我们的ArrayList到最后我们将在txt文件中内容TextArea。 如果事情没有意义,我很抱歉是谷歌翻译和我不会说英语的人。

观看Windows记事本,它不总是跳线,并在一行中显示全部,使用写字板好。


私人无效SaveActionPerformed(java.awt.event.ActionEvent中EVT){

String NameFile = Name.getText(); 
    ArrayList<String> Text = new ArrayList<String>(); 

    Text.add(TextArea.getText()); 

    SaveFile(NameFile, Text); 
} 

公共无效SAVEFILE(字符串名称,ArrayList的<字符串>消息){

path = "C:\\Users\\Paulo Brito\\Desktop\\" + name + ".txt"; 

    File file1 = new File(path); 

    try { 

     if (!file1.exists()) { 

      file1.createNewFile(); 
     } 


     File[] files = file1.listFiles(); 


     FileWriter fw = new FileWriter(file1, true); 

     BufferedWriter bw = new BufferedWriter(fw); 

     for (int i = 0; i < message.size(); i++) { 

      bw.write(message.get(i)); 
      bw.newLine(); 
     } 

     bw.close(); 
     fw.close(); 

     FileReader fr = new FileReader(file1); 

     BufferedReader br = new BufferedReader(fr); 

     fw = new FileWriter(file1, true); 

     bw = new BufferedWriter(fw); 

     while (br.ready()) { 

      String line = br.readLine(); 

      System.out.println(line); 

      bw.write(line); 
      bw.newLine(); 

     } 
     br.close(); 
     fr.close(); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
     JOptionPane.showMessageDialog(null, "Error in" + ex);   
} 
相关问题