2017-01-16 107 views
0

當我從.txt文件讀取文件時,它正在製作.txt文件的另一個副本,並向名稱中添加「.txt」。從文本文件中讀取,創建另一個文件

例如:從「hello.txt」中讀取並創建「hello.txt.txt」..我發現問題是包含FileWriter flwrtr = new FileWriter(fl.getPath()+".txt");的行,但是如果刪除字符串將無法工作任何人都知道方案?

String path=""; 

    JFileChooser fileopenchooser = new JFileChooser(); 
    fileopenchooser.setDialogTitle("Open Quiz"); 
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt"); 
    fileopenchooser.setFileFilter(filter); 

    int getvlue = fileopenchooser.showOpenDialog(fileopenchooser); 
    if(getvlue == JFileChooser.APPROVE_OPTION){ 

     File fl = fileopenchooser.getSelectedFile(); 
     try{ 

     FileWriter flwrtr = new FileWriter(fl.getPath()+".txt"); 
     path = fl.getPath(); 
     flwrtr.close(); 

     } 
     catch(Exception e){ 
     JOptionPane.showMessageDialog(null,"Problem Saving File!","ERROR",JOptionPane.WARNING_MESSAGE); 
     } 
+0

你可以使用子字符串。這是對的?這就是它的樣子。我想你可以在C中使用字符串索引'str [0:4]' –

回答

1

這是Java,不C.你得到一個新的文件「hello.txt.txt」因爲你在新的FileWriter呼叫添加名爲「.txt」。你說你想讀取文件,那麼爲什麼要創建一個FileWriter來寫入文件,而不是讀取。如果你想閱讀,使用FileReader。

0

您是否注意到getPath方法正在返回包含文件名的文件的路徑?我知道這可能是誤導,這就是爲什麼你有2 txt。也許你應該做一些字符串操作的路徑一樣減少3炭等

如:fl.getPath().substring(0, fl.getPath().length()-3)

0
再次

,我一直在想,...

更重要的是,如果你的目標是測驗文檔在測驗系統中打開爲測驗參與者分發的文件,然後他們打開它,然後他們可以用答案填寫表格(例如Pdf表格)並簽名(或自動簽名爲您的登錄名)並提交/保存使系統具有所有有效/合法測試/問答文件的文件,可以查詢和法官/年級,我認爲這是如何去:

型號:

package com.emerlard.test.temp.test.model; 

import java.io.Serializable; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

/** 
* 
* @author eddhie 
*/ 
@Entity 
public class Document implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Document)) { 
      return false; 
     } 
     Document other = (Document) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "com.emerlard.test.temp.test.model.Document[ id=" + id + " ]"; 
    } 

    private String Name; 

    private User CreatedBy; 


    //todo:create a directory system/model 
    private Directory directory; 

    ` 


}----- 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.emerlard.test.temp.test.model; 

import java.io.Serializable; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

/** 
* 
* @author eddie 
*/ 
@Entity 
public class QuestionDocument extends Document implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof QuestionDocument)) { 
      return false; 
     } 
     QuestionDocument other = (QuestionDocument) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "com.emerlard.test.temp.test.model.QuestionDocument[ id=" + id + " ]"; 
    } 





} /* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.emerlard.test.temp; 

import java.beans.PropertyChangeListener; 
import java.beans.PropertyChangeSupport; 
import java.io.File; 
import javax.swing.JFileChooser; 
import javax.swing.filechooser.FileNameExtensionFilter; 

/** 
* 
* @author eddhie 
* 
*/ 
public class JQuizFileChooser extends JFileChooser implements IJQuizFileChooser { 
    //todo:the event still using property change call so maybe need to be regular agent or not. but it is quite standard for this java beans 
    public static final String PROP_FILE_CHOOSEN_EVENT = "FileChoosenEvent"; 

    private String FileChoosenEvent; 

    private PropertyChangeSupport propertySupport; 


    @Override 
    public String getFileChoosenEvent() { 
     return FileChoosenEvent; 
    } 

    @Override 
    public void setFileChoosenEvent(String value) { 
     String oldValue = FileChoosenEvent; 
     FileChoosenEvent = value; 
     propertySupport.firePropertyChange(PROP_FILE_CHOOSEN_EVENT, oldValue, FileChoosenEvent); 
    } 

    //todo:what to do woith mutltipel file seleantion 
    @Override 
    public void setSelectedFile(File file) { 
     super.setSelectedFile(file); //To change body of generated methods, choose Tools | Templates. 
     //todo:what aobut mamignt eh proeety hcangei envet is not sring but drectoyr fpeorty 
     setFileChoosenEvent("Selected File Changed , do your setting of your hander to fill the containter"); 

    } 



    public void addPropertyChangeListener(PropertyChangeListener listener) { 
     propertySupport.addPropertyChangeListener(listener); 
    } 

    public void removePropertyChangeListener(PropertyChangeListener listener) { 
     propertySupport.removePropertyChangeListener(listener); 
    } 

    public JQuizFileChooser() { 
       propertySupport = new PropertyChangeSupport(this); 
     this.setFileFilter(new FileNameExtensionFilter("Text File", "txt")); 

    } 



} 

而且界面THA tcompoent

package com.emerlard.test.temp; 

/** 
* 
* @author eddhie 
*/ 
public interface IJQuizFileChooser { 

    String getFileChoosenEvent(); 

    void setFileChoosenEvent(String value); 

} 

SEEE:

在這裏,您可以在JquisFileCooser的compojnet相關(incude使用注射例如界面春天,例如。

你然後(儘管也可以僅僅指剛財產inection用豆)

主代碼

@autowire 
    IQuizFileChooser 

這將導致這個文件選擇的內容或處理

然後你就可以趕上在主代碼中選擇的文件事件將會像

private void newBean11PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_newBean11PropertyChange 
     // TODO add your handling code here: 

    }//GEN-LAST:event_newBean11PropertyChange 

凡EVT通訊員您的活動

,並填寫要顯示的容器內

保存時,那麼你使用一些的FileWriter寫有正確的ID名稱正確的目錄

您只需連接易於應用的型號記錄

GOTIT?

所以最後你只需要這一切

@autowire Iquizzfilechooser

@autowire Containner //如果你想做到這一點, 您可以立即根據你想要去的地方運行如果,其他是注射或如果你想添加更多的其他東西取決於你然後

很酷的權利?

相關問題