Java 读取文本文件,创建另一个文件
当我从一个.txt文件读取时,它正在制作另一个.txt文件的副本,并在名称中添加“.txt” 例如:从“hello.txt”读取,它会创建“hello.txt.txt”。。我发现问题出在包含Java 读取文本文件,创建另一个文件,java,Java,当我从一个.txt文件读取时,它正在制作另一个.txt文件的副本,并在名称中添加“.txt” 例如:从“hello.txt”读取,它会创建“hello.txt.txt”。。我发现问题出在包含FileWriter flwrtr=newfilewriter(fl.getPath()+“.txt”)但是如果我删除字符串,它将不起作用。有人知道解决方案吗 String path=""; JFileChooser fileopenchooser = new JFileChooser(); f
FileWriter flwrtr=newfilewriter(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);
}
这是Java,不是C。您将获得一个新文件“hello.txt.txt”,因为您在新的FileWriter调用中添加了“.txt”。你说你想读取文件,那么为什么要创建一个文件写入器,它是用来写入文件的,而不是用来读取。如果要读取,请使用FileReader 您是否注意到getPath方法正在返回包含文件名的文件路径?我知道这可能是误导,这就是为什么你有2 txt。也许你应该对路径做一些字符串操作,比如减少3个字符等等
e、 g:fl.getPath().substring(0,fl.getPath().length()-3)
我一直在想
更好的是,如果你在一个测验系统中瞄准一个测验文档,该系统打开为测验参与者分发的文件,然后他们打开它,那么他们可以用答案填写表格(如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 T组件的接口
package com.emerlard.test.temp;
/**
*
* @author eddhie
*/
public interface IJQuizFileChooser {
String getFileChoosenEvent();
void setFileChoosenEvent(String value);
}
见:
在这里,您可以关联jquisfilecouser的compojnet(包括使用spring eg的注入eg接口)。
(尽管jsut可以将bean用于属性INAction)
然后在你的主代码上
@autowire
IQuizFileChooser
这将导致此文件选择器或句柄的内容
然后您可以在主代码中捕获filecchoosen事件,如下所示
private void newBean11PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_newBean11PropertyChange
// TODO add your handling code here:
}//GEN-LAST:event_newBean11PropertyChange
evt在哪里与您的活动相对应
并将容器装满,以便在
保存时,使用一些filewriter以正确的id名称写入正确的目录
您只需将模型的记录连接到一个简单的应用程序模型
明白了吗
所以最终你需要的只是这些
@自动连线
iquizfilechooser
@自动连线
Container//如果要执行此操作
你可以根据你想要的立即运行,如果,其他的是注射,或者如果你想添加更多的其他东西取决于你
很酷吧?你可以使用子字符串。这是C,对吗?它看起来就是这样。我认为你可以在C中使用字符串索引,str[0:4]