2017-01-16 113 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 //如果你想做到这一点, 您可以立即根据你想要去的地方运行如果,其他是注射或如果你想添加更多的其他东西取决于你然后

很酷的权利?

相关问题