java 做记事本保存功能时 如何实现换行

public void save(){
JFileChooser fd = new JFileChooser();
int n=fd.showSaveDialog(null);
if (n==fd.APPROVE_OPTION) {
File fns=fd.getSelectedFile();
if(fns.exists()){
int dia=JOptionPane.showConfirmDialog(this,"文件已存在,是否覆盖?","提示",JOptionPane.YES_NO_OPTION);
if(dia==JOptionPane.YES_OPTION);
else return;
}
try {
FileWriter osw=new FileWriter(fns);
osw.write(text1.getText());//主要是这里 保存之后的东西不会换行。。。个人感觉是 java和xp对于换行字符的要求不一样 ,请问这里如何实现换行
osw.close();
} catch (IOException ioe) {
}
}

}// 保存的算法

楼上的无法编译..我整理了一下,代码如下:
import java.awt.*;
import java.io.*;

public class MyClipboard extends Frame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 5541943532647624059L;

private TextArea editor = new TextArea();

private MyFile mf = new MyFile(this);

private MyClipboard1 cb =new MyClipboard1();

private MyFindDialog findDlg = new MyFindDialog(this, editor);

public MyClipboard(String title) {
super(title);
MyMenuBar mb = new MyMenuBar(this);
mb.addMenus(new String[] );
mb.addMenuItems(0, new String[] );
mb.addMenuItems(1, new String[] );
mb.addMenuItems(2, new String[] );
mb.addMenuItems(3, new String[] );
add(editor);
mb.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MyClipboard.this.dispose();
}
});
}

public void actionPerformed(ActionEvent e) {
String selected = e.getActionCommand();
if (selected.equals("新建"))
editor.setText("");
else if (selected.equals("打开")) {
try {
editor.setText(mf.getData());
} catch (IOException ie) {
}
} else if (selected.equals("保存")) {
try {
mf.setData(editor.getText());
} catch (IOException ie) {
}
} else if (selected.equals("退出")) {
dispose();
} else if (selected.equals("剪贴")) {
cb.setData(editor.getSelectedText());
editor.replaceRange("", editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("复制")) {
cb.setData(editor.getSelectedText());
} else if (selected.equals("粘贴")) {
String str = cb.getData();
editor.replaceRange(str, editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("清除")) {
editor.replaceRange("", editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("全选")) {
editor.setSelectionStart(0);
editor.setSelectionEnd(editor.getText().length());
} else if (selected.equals("查找")) {
findDlg.showFind();
} else if (selected.equals("查找替换")) {
findDlg.showReplace();
}
}

public static void main(String[] args) {
MyClipboard memo = new MyClipboard("我的记事本");
memo.setSize(500, 500);
memo.setLocation(239, 120);
memo.setVisible(true);
}
}

class MyClipboard1 {
private Clipboard cb;

public MyClipboard1() {
cb = Toolkit.getDefaultToolkit().getSystemClipboard();
}

public void setData(String data) {
cb.setContents(new StringSelection(data), null);
}

public String getData() {
Transferable content = cb.getContents(null);
try {
return (String) content.getTransferData(DataFlavor.stringFlavor);
} catch (Exception ue) {
}
return null;
}
}

class MyFile {
private FileDialog fDlg;

public MyFile(Frame parent) {
fDlg = new FileDialog(parent, "", FileDialog.LOAD);
}

private String getPath() {
return fDlg.getDirectory() + "\\" + fDlg.getFile();
}

public String getData() throws IOException {
fDlg.setTitle("打开");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br = new BufferedReader(new FileReader(getPath()));
StringBuffer sb = new StringBuffer();
String aline;
while ((aline = br.readLine()) != null)
sb.append(aline + '\n');
br.close();
return sb.toString();
}

public void setData(String data) throws IOException {
fDlg.setTitle("保存");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw = new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}

class MyFindDialog extends Dialog implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 4380007102323378083L;

private Label lFind = new Label("查找字符串:");

private Label lReplace = new Label("替换字符串:");

private TextField tFind = new TextField(10);

private TextField tReplace = new TextField(10);

private Button bFind = new Button("查找");

private Button bReplace = new Button("替换");

private TextArea ta;

public MyFindDialog(Frame owner, TextArea ta) {
super(owner, "查找", false);
this.ta = ta;
setLayout(null);
lFind.setBounds(10, 30, 80, 20);
lReplace.setBounds(10, 70, 80, 20);
tFind.setBounds(90, 30, 90, 20);
tReplace.setBounds(90, 70, 90, 20);
bFind.setBounds(190, 30, 80, 20);
bReplace.setBounds(190, 70, 80, 20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MyFindDialog.this.dispose();
}
});
}

public void showFind() {
setTitle("查找");
setSize(280, 60);
setVisible(true);
}

public void showReplace() {
setTitle("查找替换");
setSize(280, 110);
setVisible(true);
}

private void find() {
String text = ta.getText();
String str = tFind.getText();
int end = text.length();
int len = str.length();
int start = ta.getSelectionEnd();
if (start == end)
start = 0;
for (; start <= end - len; start++) {
if (text.substring(start, start + len).equals(str)) {
ta.setSelectionStart(start);
ta.setSelectionEnd(start + len);
return;
}
}
ta.setSelectionStart(end);
ta.setSelectionEnd(end);
}

private void replace() {
String str = tReplace.getText();
if (ta.getSelectedText().equals(tFind.getText()))
ta.replaceRange(str, ta.getSelectionStart(), ta.getSelectionEnd());
else
find();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == bFind)
find();
else if (e.getSource() == bReplace)
replace();
}
}

class MyMenuBar extends MenuBar implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2311645080753585104L;

public MyMenuBar(Frame parent) {
parent.setMenuBar(this);
}

public void addMenus(String[] menus) {
for (int i = 0; i < menus.length; i++)
add(new Menu(menus[i]));
}

public void addMenuItems(int menuNumber, String[] items) {
for (int i = 0; i < items.length; i++) {
if (items[i] != null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else
getMenu(menuNumber).addSeparator();
}
}

public void addActionListener(ActionListener al) {
for (int i = 0; i < getMenuCount(); i++)
for (int j = 0; j < getMenu(i).getItemCount(); j++)
getMenu(i).getItem(j).addActionListener(al);
}
}

如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!

vaela
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-10-10
FileWriter osw=new FileWriter(fns);
osw.write(text1.getText());//主要是这里 保存之后的东西不会换行。。。个人感觉是 java和xp对于换行字符的要求不一样 ,请问这里如何实现换行
osw.write("\n"); //添加这一行试试,如果不行,将\n改成\r\n
osw.close();本回答被提问者采纳
相似回答