应该是系统的问题。
在JTextArea中的换行可能是\r\n
而在txt文件中可能是\n或者相反。。所以你只要自己试着替换一下就可以了
在JTextArea中的换行可能是\r\n
而在txt文件中可能是\n或者相反。。所以你只要自己试着替换一下就可以了
解决方案 »
- Java 窗体监听问题
- java 工作 工资
- 关于Java操作图片旋转的问题。
- 一个在constructor里调用动态绑定的方法的问题
- Panel 的使用...不会呀........弄了半天.用Frame就对了.我改成Panel全错...郁闷死啦!!!!
- @@@@@@@@@@illegal start of expression ...
- 关于五子棋的问题
- xp下装了JBUILDER8鼠标不能点菜单和按钮,只能用键盘?
- 问一个基本的
- Applet为什么能在JBuiler5中调试,而不能在浏览器中显示
- 新手的问题,很简单!!!!!!!!!!!!!!!!
- 请问motoA6288+的汉字编码 100分马上给
是不是也有一个个的小方框呢!!!!!
你用记事本打开就会看到一个方,用word等浏览工具就不会。
记事本好象不认识此符号,根本不会换行。
JTextArea jtFunction=new JTextArea();
String sFunction=jtFunction.getText();
File fJsFile=new File(fDir,sFileName);
fJsFile.createNewFile();
FileOutputStream fileWrite=new FileOutputStream(fJsFile);
DataOutputStream dataWrite=new DataOutputStream(fileWrite);
dataWrite.writeBytes(sFunction);
Thank you!
// import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;class JEnterDemo extends JFrame
{
JEnterDemo(String strTitle) {
super(strTitle); openButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent evt) {
// open a text file
int returnVal = chooser.showOpenDialog(openButton);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File fin = chooser.getSelectedFile();
if (fin.exists()) {
try
{
view.setText("");
// read and display the text content
BufferedReader in = new BufferedReader(new FileReader(fin));
String strBuffer = null; while ((strBuffer = in.readLine()) != null) {
view.append(strBuffer + newline);
}
in.close();
}
catch (IOException e) {
}
}
}
}
}); saveButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent evt) {
// save to a file
int returnVal = chooser.showSaveDialog(saveButton);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File fout = chooser.getSelectedFile();
try {
// no line separator swaping (\n -> \r\n)
// so when you open the saved file with notepad, it would be ... :(
// however, open and save operation is this application is consistent :)
BufferedWriter out = new BufferedWriter(new FileWriter(fout));
String strBuffer = view.getText();
out.write(strBuffer, 0, strBuffer.length());
out.flush();
out.close();
}
catch (IOException e) {
} }
}
});// build UI
view.setMargin(new Insets(5,5,5,5));
view.setEditable(true);
view.setTabSize(4);
view.setFont(new Font("Courier New", Font.PLAIN, 12)); JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(openButton, BorderLayout.NORTH);
contentPane.add(new JScrollPane(view), BorderLayout.CENTER);
contentPane.add(saveButton, BorderLayout.SOUTH);
contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
setContentPane(contentPane);
} public static void main(String[] args) {
JFrame frame = new JEnterDemo("MyApplication");
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName()
);
} catch(Exception e) {
e.printStackTrace();
}
frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack();
frame.setVisible(true);
} private JTextArea view = new JTextArea(20, 40);
private JButton openButton = new JButton("Open"),
saveButton = new JButton("Save"); private JFileChooser chooser= new JFileChooser();
final private String newline = "\n";
}
JTextArea jtFunction=new JTextArea();
String sFunction=jtFunction.getText();
PrintWriter out=new PrintWriter(new FileWriter(fDir+"\\"+sFileName));
out.println( sFunction );
out.close();