维多利亚vic119:关于JAVA保存文件的问题
请问这个问题应该怎么样解决??
只需要将所有的"\n"换成"\r\n",我写了段测试代码:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Save extends JFrame
{
public Save()
{
setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton saveButton=new JButton("Save");
saveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
FileOutputStream fos=new FileOutputStream(new File("1.txt"));
OutputStreamWriter osw=new OutputStreamWriter(fos);
String saveString=text.getText().replaceAll("\n","\r\n");
osw.write(saveString);
osw.flush();
osw.close();
}catch(Exception ee)
{
ee.printStackTrace();
}
}
}
);
add(saveButton,BorderLayout.NORTH);
add(text,BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[]args) throws Exception
{
new Save();
}
private JTextArea text=new JTextArea();
}