簡體   English   中英

用\\ n替換方塊

[英]Replace squares with \n

我有一種從JTextArea獲取文本並將其保存在txt文件中的方法。 一切正常,但是文本保存在一條直線上,幾乎沒有空格,而不是\\ n。 我想用\\ n替換這些正方形

我的代碼是:

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{

一定有問題,因為它創建了txt文件,但它始終是empy,JTextArea沒有文本,並且我總是從Exception獲得

catch(IOException e){
       System.out.println("Error");
   }

錯誤在哪里? 如何修改代碼?

PS我寫了'o'而不是空的正方形,因為我不知道要替換的第一個字符串(或char)寫什么。

謝謝

補充:的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)

成功構建(總時間:19秒)

補充:代碼的其他部分

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);

            }
        }

不要重新發明輪子。

所有文本組件都支持write(...)方法。 當文本寫入文件時,此方法將使用正確的換行符字符串。 因此,您要做的就是:

textArea.write(...);

這些 \\ n。 您正在記事本中打開。

您要么需要在每個\\ n之前打印一個\\ r(這可以簡單地通過使用具有printLine方法的緩沖編寫器來完成),或者在例如Wordpad中打開。

更換

line = line.replace("o", "\n");

line = line.replaceAll("o", System.getProperty("line.separator"));

您在其他部分正在做什么,您的程序無法找到指定的文件。

你的情況如果說

if(!sFile.exists()){ // don't use not here
// code to write into the file
}

應該在哪里

if(sFile.exists()){
// code to write into 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM