[英]Replace squares with \n
I have a method to get text from a JTextArea and save in a txt file. 我有一种从JTextArea获取文本并将其保存在txt文件中的方法。 It all works fine but text is saved in a straight line with little empty squares instead of \\n. 一切正常,但是文本保存在一条直线上,几乎没有空格,而不是\\ n。 I would like to replace these squares with \\n 我想用\\ n替换这些正方形
My code is: 我的代码是:
public void createTxt(){
TxtFilter txt = new TxtFilter();
JFileChooser fSave = new JFileChooser();
fSave.setFileFilter(txt);
int result = fSave.showSaveDialog(this);
if(result == JFileChooser.APPROVE_OPTION){
File sFile = new File(fSave.getSelectedFile()+ ".txt");
String file_name = sFile.getName();
String file_path = sFile.getParent();
try{
if(!sFile.exists()){
BufferedWriter out = new BufferedWriter(new FileWriter(sFile));
FileReader fr = new FileReader(jTextArea1.getText());
BufferedReader br = new BufferedReader(fr);
String line = "";
while((line = br.readLine())!= null){
line = line.replace("o", "\n");
out.write(line);
}
out.close();
JOptionPane.showMessageDialog(null, "Warning file • " + file_name + " • created succesfully in \n" + file_path);
}
else{
There must be something wrong because it creates the txt file but it is always empy, no text is get from JTextArea and I always have Exception as from 一定有问题,因为它创建了txt文件,但它始终是empy,JTextArea没有文本,并且我总是从Exception获得
catch(IOException e){
System.out.println("Error");
}
Where is the error? 错误在哪里? How to modify the code? 如何修改代码?
PS I have wrote 'o' instead of empty square because I do not know what to write as first string (or char) to be replaced. PS我写了'o'而不是空的正方形,因为我不知道要替换的第一个字符串(或char)写什么。
Thanks 谢谢
Added:PrintStackTrace 补充:的printStackTrace
java.io.FileNotFoundException: sdfdsf (Impossibile trovare il file specificato)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at provegrafica.ProvaFramePop.createTxt(ProvaFramePop.java:154)
at provegrafica.ProvaFramePop.jMenuSaveActionPerformed(ProvaFramePop.java:129)
at provegrafica.ProvaFramePop.access$100(ProvaFramePop.java:17)
at provegrafica.ProvaFramePop$2.actionPerformed(ProvaFramePop.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6289)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6054)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4652)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 19 seconds) 成功构建(总时间:19秒)
Added: Else part of the code 补充:代码的其他部分
else{
String message = "File • " + file_name + " • already exist in \n" + file_path + ":\n" + "Do you want to overwrite?";
String title = "Warning";
int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if(reply == JOptionPane.YES_OPTION){
sFile.delete();
BufferedWriter out = new BufferedWriter(new FileWriter(sFile));
out.write(jTextArea1.getText());
out.close();
JOptionPane.showMessageDialog(null, "File • " + file_name + " • overwritten succesfully in \n" + file_path);
}
}
Don't reinvent the wheel. 不要重新发明轮子。
All text components support a write(...)
method. 所有文本组件都支持write(...)
方法。 This method will use the proper newline string when the text is written to the file. 当文本写入文件时,此方法将使用正确的换行符字符串。 So all you do is: 因此,您要做的就是:
textArea.write(...);
Those are \\n's. 这些是 \\ n。 You are opening in Notepad. 您正在记事本中打开。
You either need to print out a \\r before each \\n (which here can be done simply by using a buffered writer which has a printLine method), or open in eg Wordpad. 您要么需要在每个\\ n之前打印一个\\ r(这可以简单地通过使用具有printLine方法的缓冲编写器来完成),或者在例如Wordpad中打开。
replace 更换
line = line.replace("o", "\n");
with 同
line = line.replaceAll("o", System.getProperty("line.separator"));
What are you doing in else part your program is not able to find the specified file. 您在其他部分正在做什么,您的程序无法找到指定的文件。
your condition in if says 你的情况如果说
if(!sFile.exists()){ // don't use not here
// code to write into the file
}
where as it should be 应该在哪里
if(sFile.exists()){
// code to write into the file
}
Use following to write to the file 使用以下内容写入文件
while((line = br.readLine())!= null){
out.append(line); //can also use write in place of append
out.append("\r\n");
}
out.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.